P2983 [USACO10FEB] buy chocolate
answer
Note entitled to open long long
Greedy strategy: price from low to high, enough to buy up
Evidence to the contrary: If there is a remaining K ", is smaller than K, then the exchange can not lose
So, before buying K, all cheaper than he can buy finished
Code
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<cstring> #include<cstdlib> #include<queue> using namespace std; #define ll long long inline ll read() { ll ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } ll n,b,ans=0; struct node { ll cost,cow; }candy[100010]; bool cmp(node x,node y) { return x.cost <y.cost ; } int main() { n=read();b=read(); for(ll i=1;i<=n;i++) { candy[i].cost =read(); candy[i].cow =read(); } sort(candy+1,candy+n+1,cmp); for(ll i=1;i<=n;i++) { if(candy[i].cow==0) continue; if(b==0) { printf("%lld",ans); return 0 ;} IF (Candy [I] .cow <= B / Candy [I] .cost) // directly multiply the explode { B - = Candy [I] .cost * Candy [I] .cow; ANS + = Candy [I] .cow; Continue ; } the else { ANS + = B / Candy [I] .cost; // not a save enumeration time the printf ( " % LLD " , ANS); return 0 ; } } the printf ( " % LLD " , ANS); return 0; }