All pastes #253330 Raw Edit

Someone

public text v1 · immutable
#253330 ·published 2006-11-20 17:23 UTC
rendered paste body
#include <iostream.h>

int arr[11];
int n;

int* val;//holds the lexicographic values


int getIndex(int x)
{
	for(int i = 0;i < n;i++)
		if(val[i] == x)
			return i;
	return 0;
}

void print()
{
	cout << "\n";
	for(int i = 0;i < n;i++)
		cout << val[i] << " " ;
}

int findans()
{
	int i = 0,j = 0;
	int index;
	int a,b;
	int ans = -1,count = 0;
	int aind,bind;
	for(j = 0;j < n;j++)
	{
		for(i = 1,index = j;i <= n;i++,index++)
		{
			if(index >= n)
				index %= n;
			val[index] = i;
		}

		print();//for debugging purpose

		//find the number of inversions
		count = 0;

		//no of elements to the right of "a" tat r smaller than "a" i.e elements < "a" to the right of "a"
		for(a = 0;a < n;a++)//for each element in arr
		{
			aind = getIndex(arr[a]);//get its position in the cyclic sorted order
			for(b = a+1;b < n;b++)//elements to the right of a
			{
				bind = getIndex(arr[b]);//get its position in the cyclic sorted order
				if(bind < aind)//b is < a
					count++;
			}
		}
		ans = ans == -1 || ans > count ? count : ans;
			
	}
	return ans;
}

int main()
{
	cin >> n;
	val = new int[n];
	for(int i = 0;i < n;i++)
		cin >> arr[i];
	int ans = 0;
	ans = findans();
	cout << ans;
	return 0;
	
}