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

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务