Protecting the Flowers(3262)
来源:互联网 发布:软件利息的会计 编辑:程序博客网 时间:2024/05/13 17:37
http://poj.org/problem?id=3262
题解:
考虑当前选哪个放在开头。
设放在开头的为(d1,t1) 放在第二个的为(d2,t2)。
那么放在开头的牛满足:无论第二个是什么,交换第一和第二总是不会变优。
设当前剩余牛的d值和为ds。
那么有:
(ds-d1)*t1+(ds-d1-d2)*t2<(ds-d2)*t2+(ds-d2-d1)*t1
化简得:
t2/d2>t1/d1。
也就是说,当t[i]/d[i]>t[j]/t[j]时,j放在i前面更优。
若开头的是t[i]/d[i]中的最小值,无论后面是什么,它永远是最优的。
那么我们可以确定开头了。
而注意到开头确定后,剩下的正好是n-1规模的子问题,可以用一样的方式解决。
所以我们可以按t[i]/d[i]排序,从小取到大就是最终序列了。
复杂度:O(nlogn)。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct s{ int d, t;}pp[100100];bool cmp(s a, s b){ return a.d/(a.t*1.0) < b.d/(b.t*1.0); }int n;int main(){ int i; while(scanf("%d", &n) == 1){ long long sum = 0; for(i = 1; i <= n; i++){ scanf("%d%d", &pp[i].d, &pp[i].t); sum += pp[i].t; } sort(pp+1, pp+1+n, cmp); long long ans = 0; for(i = 1; i <= n; i++){ sum -= pp[i].t; ans += pp[i].d * 2 * sum; } printf("%I64d\n", ans); } return 0;}
0 0
- Protecting the Flowers(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
- 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
- cdev 与inode ,file_operations,kobject map的关系
- Java核心技术卷I:基础知识(原书第8版):13.1 集合接口
- CString用法总结 .
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- 文件归档保存
- Protecting the Flowers(3262)
- 关闭win7的休眠功能
- PCM数据基本格式
- 提问的智慧
- Spring AOP 实现原理
- 详解MySQL中EXPLAIN解释命令
- sybase ASE日志溢出解决方式探究
- 对于程序员说的话,项目经理们是这样理解的
- IOS AFNetworking基本学习