zoukankan      html  css  js  c++  java
  • *[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";
    	}
    };
    

      

  • 相关阅读:
    石墨文档地址
    Emacs
    HDU
    田忌赛马(贪心
    poj 3040 Allowance (贪心
    cr545
    雕塑 ( 离散化,bfs-floodfill
    求m个不相交子段的和(复杂dp
    doing home work(dp-二进制法枚举
    非常可乐(多参数bfs模拟
  • 原文地址:https://www.cnblogs.com/lautsie/p/3886445.html
Copyright © 2011-2022 走看看