P2376 [USACO09OCT]津贴Allowance
P2376 [USACO09OCT]津贴Allowance
一开始想的是多重背包,但是实践不了。实际是贪心,让多c尽可能少,所以先放大的,最后让小的来弥补。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<cstring> 9 #define inf INT_MAX 10 #define For(i,a,b) for(register int i=a;i<=b;i++) 11 #define p(a) putchar(a) 12 #define g() getchar() 13 //by war 14 //2017.10.27 15 using namespace std; 16 struct money 17 { 18 int cnt; 19 int v; 20 bool operator<(const money&aa)const 21 { 22 return v<aa.v; 23 } 24 }a[30]; 25 int ans; 26 int x,y,n,c,cnt,now; 27 28 void in(int &x) 29 { 30 int y=1; 31 char c=g();x=0; 32 while(c<'0'||c>'9') 33 { 34 if(c=='-') 35 y=-1; 36 c=g(); 37 } 38 while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); 39 x*=y; 40 } 41 void o(int x) 42 { 43 if(x<0) 44 { 45 p('-'); 46 x=-x; 47 } 48 if(x>9)o(x/10); 49 p(x%10+'0'); 50 } 51 52 int main() 53 { 54 in(n),in(c); 55 For(i,1,n) 56 { 57 in(x); 58 in(y); 59 if(x>=c) 60 ans+=y; 61 else 62 { 63 a[++cnt].cnt=y; 64 a[cnt].v=x; 65 } 66 } 67 sort(a+1,a+cnt+1); 68 while(1) 69 { 70 now=c; 71 for(register int i=cnt;i>=1;i--) 72 while(now>=a[i].v&&a[i].cnt>0) 73 { 74 now-=a[i].v; 75 a[i].cnt--; 76 } 77 if(now>0) 78 For(i,1,cnt) 79 if(now<a[i].v&&a[i].cnt>0) 80 { 81 now-=a[i].v; 82 a[i].cnt--; 83 break; 84 } 85 if(now>0) 86 break; 87 ans++; 88 } 89 o(ans); 90 return 0; 91 }