CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C-Voltage Keepsake
来源:互联网 发布:mac ladybug是什么意思 编辑:程序博客网 时间:2024/06/16 19:44
ACM模版
描述
题解
贪心可解,二分可解,前者更快更好,后者精度问题需要格外注意,另外需要强调的是要用long long
。
我一开始用的二分,一直存在精度问题解决不了,谁知道并不是我的精度错了,而是没有用long long
,结果当时没有找到这个问题,而选择用了贪心做,当然,同样的问题依然没有避免,经过仔细查找后发现是因为int
溢出了,用long long
后成功 AC。
说到贪心,这里用到了一个小小的数学变换:
设时间为t
,总能量为pz
,总消耗速度为v
,初始能量为pc
,则 t = pz / v = (pc + t * p) / (v - p + p) = pc / (v - p)
相当于分母消去了p
,分子消去了t * p
,没有什么难的,就是一个小技巧而已~~~
代码
#include <iostream>#include <algorithm>using namespace std;const int MAXN = 1e5 + 10;const int INF = 0x3f3f3f3f;struct device{ int a, b; double c; bool operator < (const device &b) const { return c < b.c; }} dev[MAXN];int n, p;int main(int argc, const char * argv[]){ cin >> n >> p; long long sum = 0; for (int i = 0; i < n; i++) { scanf("%d%d", &dev[i].a, &dev[i].b); dev[i].c = dev[i].b * 1.0 / dev[i].a; sum += dev[i].a; } if (p >= sum) { cout << "-1\n"; return 0; } sort(dev, dev + n); dev[n].c = INF; long long sumA = 0, sumB = 0; for (int i = 0; i < n; i++) { sumA += dev[i].a; sumB += dev[i].b; if (sumA <= p) { continue; } if (sumB * 1.0 / (sumA - p) <= dev[i + 1].c) { printf("%.10lf\n", sumB * 1.0 / (sumA - p)); return 0; } } printf("%.10lf\n", sumB * 1.0 / (sumA - p)); return 0;}
0 0
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C. Voltage Keepsake
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C-Voltage Keepsake
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-A-Vicious Keyboard
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-B-Valued Keys
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A -- D
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A. Vicious Keyboard
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) B. Valued Keys
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) D. Volatile Kite
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) ABCD
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- 【解题报告】Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- 【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- dubbo源码分析-RPC远程调用模块与Remoting通讯模块协作细节
- 对象混合
- B. Odd sum
- 关于获得时间的Date用法
- 贪心法——C语言实现最小代价生成树
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C-Voltage Keepsake
- python正则表达式入门
- SEM经验-3
- maven--私服的搭建(Nexus的使用)和注意的问题
- 455
- 第六弹:微信小程序开发思考总结—“文章阅读器和电影信息”项目实践---项目结束和补充总结
- mysql关键字 exists 与 in
- 【剑指】01-替换空格
- Java 8并发工具包漫游指南