POJ 3262 Protecting the Flowers 贪心
来源:互联网 发布:交换机端口抓包 编辑:程序博客网 时间:2024/05/21 13:23
我们要考虑的问题就是当有两头牛t1 d1和t2 d2我们应该先把谁先运走
假如先运1
最后的损失就是 2 * t1 * d2
假如先运2
最后的损失就是 2 * t2 * d1
假如先运1更好
也就是2 * t1 * d2 < 2 * t2 * d1
即 (t1 / d1) < (t2 / d2)
也就是我们优先运t / d小的牛
所以先按照t / d从小到大排序一次,从前往后计算即可
因为反复使用di + di+1 +......dn
所以我们用sum[i]表示上述式子,预处理出sum数组
#include <map>#include <cmath>#include <queue>#include <cstdio>#include <vector>#include <cstdlib>#include <iostream>#include <algorithm>#define MAX_N 100005using namespace std;typedef long long int ll;struct data{ ll t, d; bool operator < (const data& b) const { return (t * 1.0 / d) < (b.t * 1.0 / b.d); }};int main(){ //freopen("in.txt", "r", stdin); int N; data dat[MAX_N]; int sum[MAX_N]; scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%I64d%I64d", &dat[i].t, &dat[i].d); sort(dat, dat + N); sum[N - 1] = dat[N - 1].d; for (int i = N - 2; i >= 0; i--) sum[i] = sum[i + 1] + dat[i].d; ll ans = 0; for (int i = 0; i < N - 1; i++) ans += dat[i].t * 2 * sum[i + 1]; printf("%I64d\n", ans); return 0;}
0 0
- POJ 3262 Protecting the Flowers 贪心
- poj 3262 Protecting the Flowers (贪心)
- poj 3262 Protecting the Flowers(贪心)
- poj 3262 Protecting the Flowers 【贪心】
- poj 3262 Protecting the Flowers(贪心)
- Protecting the Flowers - POJ 3262 贪心
- poj 3262 Protecting the Flowers(贪心)
- POJ 3262 Protecting the Flowers【贪心】
- POJ 3262 - Protecting the Flowers(贪心)
- POJ 3262 Protecting the Flowers (贪心)
- POJ 3262 Protecting the Flowers(贪心)
- POJ 3262 Protecting the Flowers(贪心)
- Poj 3262 Protecting the Flowers【贪心】
- POJ 3262 Protecting the Flowers 贪心
- 【贪心】POJ 3262 Protecting the Flowers
- *Protecting the Flowers(POJ 3262, 贪心)
- POJ 3262 Protecting the Flowers(贪心,需要小小优化一下)
- POJ 3262-Protecting the Flowers(贪心+前缀和)
- Mac 10进制转换16进制
- 微信开发坑哭了自己Code、OpenID、WXConfig、TOKEN、TICKET处处是坑
- React生命周期详解
- Qcharts之饼图
- 欢迎使用CSDN-markdown编辑器
- POJ 3262 Protecting the Flowers 贪心
- hdu1233还是畅通工程
- Linux第一课的总结
- jedis(redis)整合spring,包括jedis客户端单机版,jedis集群版配置 ,连接池配置
- hql 语法与详细解释
- 借鉴面试总结
- 数据库中范式和反范式
- glusterFS的部署流程
- union-find算法