hdu 4310 Hero
这道题是道算是一道很简单的贪心题了,但是要注意排序的依据,这道题是按照dps/hp的从大到小排序的,然后计算总的sumhp即可。
#include"iostream" #include"stdio.h" #include"cmath" #include"string.h" #include"algorithm" #define mx 105 using namespace std; struct enemy { __int64 hp; __int64 dps; }e[mx]; bool cmp(const enemy a,const enemy b) { return a.dps*b.hp>b.dps*a.hp; } int main() { __int64 n,i; while(scanf("%I64d",&n)!=EOF) { __int64 sumdps=0,sumhp=0; for(i=0;i<n;i++) { cin>>e[i].dps>>e[i].hp; sumdps+=e[i].dps; } sort(e,e+n,cmp); for(i=0;i<n;i++) { sumhp+=sumdps*e[i].hp; sumdps-=e[i].dps; } cout<<sumhp<<endl; } return 0; }