UVA_10570_Meeting with Aliens

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#pragma warning(disable:4996)
#include<cstdio>
#include<cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
int exchange(vector<int>order)
{
	int n = 0;
	//升序
	for (size_t i = 0; i < order.size(); i++)
	{
		while (order[i] != i)
		{
			std::swap(order[i],order[order[i]]);
			n++;
		}
	}
	return n;
}
int main()
{
	//freopen("input.txt", "r", stdin);  
	//freopen("output.txt", "w", stdout);  
	int n;
	while (cin >> n&&n)
	{
		vector<int>order(n);
		for (int i = 0; i < n; i++)
		{
			cin >> order[i];
			order[i]--;
		}
		for (int i = 0; i < n; i++)
		{
			order.push_back(order[i]);
		}
		int count = 1000000000;
		for (int i = 0; i < n; i++)
		{
			vector<int>tmp;
			tmp.assign(order.begin()+i,order.begin()+i+n);
			count = std::min(count, exchange(tmp));
			std::reverse(tmp.begin(), tmp.end());
			count = std::min(count, exchange(tmp));
			if (!count)
			{
				break;
			}
		}
		cout << count << endl;
	}
	return 0;
}

你可能感兴趣的:(uva,greedy)