Friday, December 14, 2007

Andy's Shoes

Here's the problem statement: Andy's Shoes
Accepted in the 3rd try. The last one was with Dileep's help.

#include <iostream>
using namespace std;

void swap(int *arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++) {
int m;
cin >> m;
int arr[2*m], pair[10001];
for(int j = 0; j < 2*m; j++) {
cin >> arr[j];
pair[arr[j]] = j;
}

int s = 0;
for(int j = 0; j < 2*m; j+=2) {
if(arr[j] != arr[j+1]) {
if(pair[arr[j+1]] < pair[arr[j]])
pair[arr[j+1]] = pair[arr[j]];
swap(arr, pair[arr[j]], j+1);
s++;
}
}
cout << s << endl;
}
}

No comments:

Contributors