牛客竞赛 1030 保护那些花
题意

思路
还是贪心 , 考虑两头牛先把谁抱回来 , 两头牛为\(t_i,t_{i+1}\)和\(d_i,d_{i+1}\)
在抱回两头牛的过程中 , 只考虑这两头牛损耗了多少花呢 ?
\(2\times t_i \times d_{i+1}\)与\(2\times t_{i+1} \times d_{i}\) , 即先抱回第\(i\)头牛和先抱回第\(i+1\)头牛
然后就和国王的游戏一样显然啦
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long int
const int N = 1e5+10;
inline int read() {
int ans = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')f = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
ans = ans * 10 + ch - '0';
ch = getchar();
}
return ans * f;
}
struct Item {
int t,d;
bool operator<(const Item &x) const {
return t * x.d < x.t * d;
}
};
Item v[N];
signed main()
{
int n =read();
int sum = 0;
for (int i =1; i<= n; i++) {
v[i].t =read(),v[i].d= read();
sum += v[i].d;
}
sort(v+1,v+1+n);
int ans = 0;
for (int i =1; i<= n ;i++) {
sum -= v[i].d;
ans += 2 * v[i].t *sum;
}
cout<<ans;
return 0;
}

浙公网安备 33010602011771号