Contest Balloons

Contest Balloons

题目链接:http://codeforces.com/problemset/problem/725/D

贪心+枚举

每次在排名在自己前面的选出w-t值最小的送气球,更新rank即可。

(一直觉得cin比scanf不会慢太多,结果我跑出来是2000ms,队友300ms...)

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<queue>
 5 #define Min(x,y) (x<y?x:y)
 6 #define N 300005
 7 using namespace std;
 8 typedef long long LL;
 9 LL n,k,t,w,rk,ans,index;
10 struct nod{
11     LL t,w,diff;
12     nod(LL tt=0,LL ww=0){
13         t=tt,w=ww;
14         diff=w-t;
15     }
16     bool operator < (const nod &b)const{
17         return diff>b.diff;
18     }
19 }a[N];
20 bool cmp(nod a,nod b){
21     return a.t>b.t;
22 }
23 priority_queue<nod>pq;
24 int main(void){
25     scanf("%I64d",&n);
26     scanf("%I64d%I64d",&t,&w);
27     for(LL i=1;i<n;++i){
28         LL t,w;
29         scanf("%I64d%I64d",&t,&w);
30         if(t<=w)a[k++]=nod(t,w);
31     }
32     sort(a,a+k,cmp);
33     for(;index<k;++index){
34         if(a[index].t<=t)break;
35         pq.push(a[index]);
36     }
37     rk=ans=index+1;
38     while(!pq.empty()){
39         nod temp=pq.top();
40         pq.pop();
41         t-=temp.diff+1;
42         if(t<0)break;
43         LL i=0;
44         for(;i+index<k;++i){
45             if(a[i+index].t<=t)break;
46             pq.push(a[i+index]);
47         }
48         rk+=i-1;
49         index+=i;
50         ans=Min(ans,rk);
51     }
52     printf("%I64d
",ans);
53 }
原文地址:https://www.cnblogs.com/barrier/p/5990077.html