【每日一题】Protecting the Flower

Protecting the Flowers

https://ac.nowcoder.com/acm/problem/25043

题意

一共有 只牛在花坛旁边,第 头牛每分钟破坏 朵花,把第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;
}
全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务