优先队列(挑程)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  } 

猜你喜欢

转载自www.cnblogs.com/Msmw/p/10441608.html