Codeforces_474B算法

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct ints
{
	int number;
	int id;
	int group;
};

bool operator < (const ints& va1, const ints & va2)
	{
    return va1.number < va2.number;
}

class Comparers
	{
	public:
	
		    bool operator () (const ints & va1, const ints & va2)
		  {
	        return va1.id < va2.id;
	   }
};



int main()
{
	int n, a, sums[100001], m;
	cin >> n;
	cin >> a;
	sums[0] = a;
	for (int i = 1; i < n; i++)
	{
		cin >> a;
		sums[i] = sums[i - 1] + a;
	}
	cin >> m;
	ints t;
	vector ww;
	for (int i = 0; i < m; i++)
	{
		cin >> t.number;
		t.id = i;
		t.group = 0;
		ww.push_back(t);
	}
	sort(ww.begin(), ww.end());
	int k = 0;
	for (int j = 0; j < m; j++)
	{
		if (ww[j].number <= sums[k])
			ww[j].group = k + 1;
		else
		{
			while (ww[j].number > sums[k])
				k++;
			ww[j].group = k + 1;
		}
	}
	sort(ww.begin(), ww.end(), Comparers());
	for (int i = 0; i < m; i++)
	{
		cout << ww[i].group << endl;
	}

}

终于突破了474B,这道题一开始最简单的思路会导致m*n的时间复杂度,因此会卡死在test32上,一直time out。所以只能换思路。

换思路时,卡死在了struct的sort上。需要对struct排序咋办呢?参考了一下文章 http://blog.csdn.net/byrsongqq/article/details/5683597 及 http://www.jb51.net/article/36021.htm

搞定,比较爽。一次过。

关键是又突破了自我

你可能感兴趣的:(Codeforces题目集)