P2376 [USACO09OCT]津贴Allowance
一开始想的是多重背包,但是实践不了。实际是贪心,让多c尽可能少,所以先放大的,最后让小的来弥补。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<cstring>
#define inf INT_MAX
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.27
using namespace std;
struct money
{
int cnt;
int v;
bool operator<(const money&aa)const
{
return v<aa.v;
}
}a[];
int ans;
int x,y,n,c,cnt,now; void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} int main()
{
in(n),in(c);
For(i,,n)
{
in(x);
in(y);
if(x>=c)
ans+=y;
else
{
a[++cnt].cnt=y;
a[cnt].v=x;
}
}
sort(a+,a+cnt+);
while()
{
now=c;
for(register int i=cnt;i>=;i--)
while(now>=a[i].v&&a[i].cnt>)
{
now-=a[i].v;
a[i].cnt--;
}
if(now>)
For(i,,cnt)
if(now<a[i].v&&a[i].cnt>)
{
now-=a[i].v;
a[i].cnt--;
break;
}
if(now>)
break;
ans++;
}
o(ans);
return ;
}