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 }

 

转载于:https://www.cnblogs.com/war1111/p/7743589.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值