贪心,刚做此题时,就意识到了是贪心,但贪心思路一直没找对,wa了n次,在纠结中终于想出正确的思路
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; struct node { int x,y; }; node f[25]; bool cmp(node a,node b) { return a.y*b.x<b.y*a.x; } int main() { int n; while(cin>>n) { int i; long long amount=0,tot=0; for(i=0;i<n;i++) { scanf("%d%d",&f[i].x,&f[i].y); amount+=f[i].x; } sort(f,f+n,cmp); for(i=0;i<n;i++) { tot+=amount*f[i].y; amount-=f[i].x; } cout<<tot<<endl; } return 0; }