题意
一共有 只牛在花坛旁边,第
头牛每分钟破坏
朵花,把第i头牛带回牛棚需要
这么多时间,每次只能带回一头牛,请问怎样能使得被破坏的花最少。
solution
以小化大,先考虑两头牛,先领 ,损失为:
,先领
,损失为
,故得到排序条件
,最后模拟得出答案。
Code
#include <bits/stdc++.h>
#define x first
#define y second
#define PI pair<int, int>
#define ll long long
using namespace std;
ll n, sum, res;
pair<int, int> p[200005];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;
sort(p + 1, p + n + 1, [](PI a, PI b) { return a.x * b.y < b.x * a.y; });
for (int i = 1; i <= n; i++) {
res += sum * p[i].y;
sum += p[i].x * 2;
}
cout << res << '\n';
return 0;
}
京公网安备 11010502036488号