USACO10FEB Chocolate buying

题目地址

题解

差点写成多重背包,好险好险
其实就是水贪心…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int n;
ll b;
pair<ll,ll> P[100005];
void init(){
scanf("%d%lld",&n,&b);
for(int i=0;i<n;i++)
scanf("%lld%lld",&P[i].first,&P[i].second);
}
void solve(){
ll ans=0;
sort(P,P+n);
for(int i=0;i<n;i++){
if(b<P[i].first)break;
ll res=min(b/P[i].first,P[i].second);
ans+=res;
b-=res*P[i].first;
}
printf("%lld\n",ans);
}
int main(){
init();
solve();
return 0;
}