【每日一题】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;
}
全部评论

相关推荐

野猪不是猪🐗:现在的环境就是这样,供远大于求。 以前卡学历,现在最高学历不够卡了,还要卡第一学历。 还是不够筛,于是还要求得有实习、不能有gap等等... 可能这个岗位总共就一个hc,筛到最后还是有十几个人满足这些要求。他们都非常优秀,各方面都很棒。 那没办法了,看那个顺眼选哪个呗。 很残酷,也很现实
点赞 评论 收藏
分享
05-12 17:28
已编辑
门头沟学院 硬件开发
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务