优先队列(挑程)poj 2431

每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑。还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa。

优先队列直接用stl就可以,简单实用。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <map>
 5 #include <set>
 6 #include <algorithm>
 7 #include <fstream>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <stack>
11 #include <queue>
12 using namespace std;
13 const double Pi=3.14159265358979323846;
14 typedef long long ll;
15 const int MAXN=10000+5;
16 const int dx[5]={0,0,0,1,-1};
17 const int dy[5]={1,-1,0,0,0};
18 const int INF = 0x3f3f3f3f;
19 const int NINF = 0xc0c0c0c0;
20 const ll mod=1e9+7;
21 struct  node{
22     int p,d;
23 }a[MAXN];
24 bool cmp(node a,node b)
25 {
26     return a.d>b.d;
27 }
28 int main()
29 {
30     int n;scanf("%d",&n);
31     for(int i=1;i<=n;i++)
32     {
33         scanf("%d%d",&a[i].d,&a[i].p);
34     }
35     int L,P;
36     scanf("%d%d",&L,&P);
37     for(int i=1;i<=n;i++)
38     {
39         a[i].d=L-a[i].d;
40     }
41     sort(a+1,a+n+1,cmp);
42     /*for(int i=1;i<=n;i++)
43     {
44         cout <<a[i].d<<endl;
45     }*/
46     int ans=0;
47     priority_queue <int> pque;
48     if(n==0&&P-L<0)
49     {
50         puts("-1");
51         return 0;
52     }
53     for(int i=n;i>=1;i--)
54     {
55         int dis=a[i].d-a[i+1].d;
56         
57         if(dis>P)
58         {
59             while(dis>P&&!pque.empty())
60             {
61                 P+=pque.top();
62                 pque.pop();
63                 ans++;
64             } 
65             if(dis>P)
66             {
67                 puts("-1");
68                 return 0;
69             }else
70             {
71                 P=P-dis;
72             }
73         }
74         
75         else
76         {
77             P=P-dis;
78         }
79         pque.push(a[i].p);
80     }
81     int l=L-a[1].d;
82     while(!pque.empty()&&P<l)
83     {
84         P+=pque.top();
85         ans++;
86         pque.pop();
87     }
88     if(P<l) puts("-1"); 
89         else printf("%d\n",ans);
90     return 0;
91  } 

 

转载于:https://www.cnblogs.com/Msmw/p/10441608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值