HDU Saving HDU 2111
用了上个看电视的方法。如果体积不足,这时候就应该停止,否则如果n够大v会继续累加else的值,刚开始的WA就在这里
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; struct member{ int price; int num;; }Ar[110]; int cmp(member a,member b){ return a.price>b.price; } int main(){ int v,n,i; while(cin>>v&&v){ cin>>n; int ans=0; for(i=0;i<n;i++){ cin>>Ar[i].price>>Ar[i].num; } sort(Ar,Ar+n,cmp); for(i=0;i<n;i++){ if(v>=Ar[i].num){ ans+=Ar[i].num*Ar[i].price; v-=Ar[i].num; } else{ ans+=v*Ar[i].price; break; } } cout<<ans<<endl; } }