*[topcoder]IncrementingSequence

http://community.topcoder.com/stat?c=problem_statement&pm=12107

此题想了半天,当时瞥到了Greedy,所以就想着贪心,最后的方法又纸上画了一下应该是对的。就是排序后依次看是不是满足要求。证明就是如果对数字X,有a和b都能够通过增加k的倍数步得到X,那么使用小的a自然更好,因为b有更大机会为剩下的出力。

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class IncrementingSequence {
public:
	string canItBeDone(int k, vector <int> A)
	{
		sort(A.begin(), A.end());
		vector<bool> used(A.size());
		for (int i = 1; i <= A.size(); i++)
		{
			for (int j = 0; j < A.size(); j++)
			{
				if (used[j])
					continue;
				if (A[j] > i)
					return "IMPOSSIBLE";
				if ((A[j] - i) % k == 0)
				{
					used[j] = true;
					break;
				}
			}
		}
		return "POSSIBLE";
	}
};

  

posted @ 2014-08-02 08:33  阿牧遥  阅读(327)  评论(0编辑  收藏  举报