Codeforces 725D Contest Balloons

Source: Internet
Author: User
Tags min

Test instructions is easy to understand: Limak can give their own balloons to others, so that other people's balloons than weight. Thus allowing others to float on the ceiling, unable to participate in the rankings, thus making limak their rankings rise. This topic is a typical greedy, when limak his own balloon to others, his own hands of the balloon will be reduced, then his own ranking is likely to be reduced. So, we need to think about what kind of person Limak should give the balloon to, and his own ranking is likely to rise. In fact, very simple, he must be the balloon to the current ranking in front of the people, their ranking can rise. So, the question comes again, he has a lot of people in front of him, to whom. If you think about it, he just needs to give a minimum of one balloon to rise to the ceiling. Limak each time the balloon to others, his balloon number has changed, but also may lead to some previous rankings after Limak, now run to Limak front. In this way, every time limak a person up to the ceiling, you need to update the rank of each player. So, the key question comes, because of the large amount of data, each time how to quickly find a current ranking in front of the Limak, and rise to the ceiling of the minimum required balloon player. I used the priority queue (redefine the less-than sign, so that the minimum number of balloons to rise to the ceiling is at the top of the team) to save the currently larger player than the Limak, so that the search time is LG (n). The final answer is that, in the process, the number of elements in the queue is the least of the time, Limak ranked the most forward time. The specific implementation is as follows:

#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define LL Long Long

using namespace Std;

const int MAX = 300010;
	struct node{ll T, W, X;
	BOOL operator < (const node& args) const{return x > args.x;


}}a[max], B[max], first;
	void Merge_sort (int l, int r) {if (L >= r) {return;

	} int mid = (l+r)/2;
	Merge_sort (L, mid);

	Merge_sort (mid+1, R);

	int x = l, y = mid+1, i = l;
		while (x <= mid | | y <= r) {if (x <= mid && (Y > R | | a[x].t >= a[y].t)) {b[i++] = a[x++];
		}else {b[i++] = a[y++];
	}} for (i = l; i<=r; i++) {a[i] = B[i];

}} priority_queue<node> Q;

void Init () {while (!q.empty ()) Q.pop ();}
	int solve (int n) {int m, res = 0, ans = MAX;
			for (m = 0; m<n-1; m++) {if (a[m].t > first.t) {q.push (a[m]);
		res++;
		}else{break;

	}} ans = min (ans, res);

		while (!q.empty ()) {Node tmp = Q.top (); if (tmp.x <= first.t) {Q.POP ();
			FIRST.T-= tmp.x;

			res--;
					for (; m<n-1; m++) {if (a[m].t > first.t) {q.push (a[m]);
				res++;
				}else{break;
		}} ans = min (ans, res);
		}else{break;

	}} ans = min (ans, res);
return ans+1;
	} int main () {int n;

		while (scanf ("%d", &n)! = EOF) {init ();

		scanf ("%lld%lld", &first.t, &AMP;FIRST.W);
			for (int i = 0; i<n-1; i++) {scanf ("%lld%lld", &a[i].t, &AMP;A[I].W);
		a[i].x = a[i].w-a[i].t+1;
 

		} merge_sort (0, n-2);
	printf ("%d\n", Solve (n));




} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.