【每日一题】Protecting the Flower
Protecting the Flowers
https://ac.nowcoder.com/acm/problem/25043
题意:
思路:
#include <cstdio> #include <algorithm> using namespace std; typedef long long ll; const int N = 1e5 + 10; struct Node{ int t,d; }a[N]; int n,sum[N]; bool cmp(Node a,Node b){ return a.t * b.d < a.d * b.t; } int main(){ scanf("%d",&n); for(int i = 1;i <= n;i++){ scanf("%d%d",&a[i].t,&a[i].d); } sort(a+1,a+1+n,cmp); for(int i = 1;i <= n;i++) sum[i] = sum[i-1] + a[i].d; ll ans = 0; for(int i = 1;i <= n;i++){ ans += 2 * a[i].t * (sum[n] - sum[i]); } printf("%lld\n",ans); return 0; }