题目地址:​​点击打开链接​

思路:巧妙之处是用乘法表示了除法的比较关系,虽然用int类型也能过,但最好用__int64类型

AC代码:

#include <iostream>
#include<algorithm>

using namespace std;

struct ghost
{
int hp;
int dp;
}a[25];

bool cmp(ghost a,ghost b)
{
if(a.dp * b.hp != b.dp * a.hp)
return a.dp * b.hp > b.dp * a.hp;
else
return a.hp < b.hp;
}
int main()
{
int n,i;
__int64 sum,count;
while(cin>>n)
{
count = 0;
sum = 0;
for(i=0; i<n; i++)
{
cin>>a[i].dp>>a[i].hp;
count += a[i].dp;
}
sort(a,a+n,cmp);
for(i=0; i<n; i++)
{
sum += count * a[i].hp;
count -= a[i].dp;
}
cout<<sum<<endl;
}
return 0;
}