也是十分简单的贪心,是性价比贪心,策略:优先选择单价较低的奶农买奶
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct node { int a,b; }; node milk[5005]; int n,m,ans; inline bool cmp(node x,node y) { return x.a<y.a; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) scanf("%d%d",&milk[i].a,&milk[i].b); sort(milk+1,milk+m+1,cmp); for(int i=1;i<=m;i++) { if(n>=milk[i].b) { n-=milk[i].b; ans+=milk[i].b*milk[i].a; } else { ans+=milk[i].a*n; break; } } printf("%d",ans); return 0; }