1. 程式人生 > >優先隊列(挑程)poj 2431

優先隊列(挑程)poj 2431

return -- fst ++ ostream sta 應該 truct priority

每次寫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 }

優先隊列(挑程)poj 2431