Purple Book Exercise 8-12 UVa 1153 (greed)

I originally thought that this question was a disjoint interval, but as a result, I specifically reviewed what I wrote earlier, and then found out whether the interval of this question is not


Fixed, is "sliding" within a range, as long as the right endpoint does not exceed the deadline, it is ok.


Then I first consider how to choose when there is a containment relationship, and then I find that when only one of the two ranges can be placed, the time is shorter and truncated


It is obviously better for a longer time . Then I try to compare the time and the back of each interval, if only one of the two intervals can be placed, and


The next interval is better, then the current one is not selected. Then after excluding these ranges, choose whatever you can.


Submit to WA. Then I found that the middle interval was excluded, but the preceding and following intervals may contradict, and also excluded


I can't think of any way to do it.


Later referenced https://blog.csdn.net/u013520118/article/details/48008741


I found that I had already thought about 80%, but I didn't think about how to realize the last 20%. The priority queue is used here to implement,


The priority queue stores all the intervals that have been put before, and then put them if they can, and move forward and have been put if they cannot.


If the interval is better, put the current interval and cancel the previous interval.


Just do it all the time. Why is it okay to do this, because it takes into account the "inclusive" relationship between all intervals


Let the worst in all intervals be discarded, so the final answer is the best.


Then I continued to think, why when Zishu talked about the interval in the front, the adjacent comparisons were discarded, and this should consider all the previous ones.


missed interval.


Then I found out that when choosing the interval on Zishu, the former one is better than the latter one.


However, in this question, the two adjacent intervals are not necessarily better than the previous one, because even if the latter one is worse, as long as


It can be released before the deadline.


Then this leads to the fact that the latter interval of this question is obviously not necessarily better than the former interval (preferably means shorter time and later deadline),


So there is no continuity, that is to say, the "worst" one in the current interval and all previous intervals should be considered, that is, the longest time required


One of them is optimal, so this proves the correctness of greed.


I think I'm speaking very abstractly. If you don't understand it, you can simply understand that you can put it away. If you can't put it, you can replace it with the worst one.


Definitely the best.


#include<cstdio>
#include<queue>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 812345;
struct node
{
	int d, q;
	bool operator < (const node& x) const
	{
		return q < x.q;
	}
} a [MAXN];

bool cmp(node a, node b)
{
	return a.d < b.d;
}

intmain()
{
	int T, n;
	scanf("%d", &T);
	
	while(T--)
	{
		priority_queue<int> Q;
		scanf("%d", &n);
		REP(i, 0, n) scanf("%d%d", &a[i].q, &a[i].d);
		sort(a, a + n, cmp);
		
		int start = 0;
		REP(i, 0, n)
		{
			if(start + a[i].q <= a[i].d) start += a[i].q, Q.push(a[i].q);
			else if(Q.size() && a[i].q < Q.top())
			{
				start += a[i].q - Q.top();
				Q.pop(); Q.push(a[i].q);
			}
		}
		
		printf("%d\n", Q.size());
		if(T) puts("");
	}
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325928633&siteId=291194637