hdu5935 Car 贪心 卡精度
来源:互联网 发布:未备案的域名 微信 编辑:程序博客网 时间:2024/05/16 13:49
Car
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 393 Accepted Submission(s): 143
Problem Description
Ruins is driving a car to participating in a programming contest. As on a very tight schedule, he will drive the car without any slow down, so the speed of the car is non-decrease real number.
Of course, his speeding caught the attention of the traffic police. Police recordN positions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 0 .
Now they want to know the minimum time that Ruins used to pass the last position.
Of course, his speeding caught the attention of the traffic police. Police record
Now they want to know the minimum time that Ruins used to pass the last position.
Input
First line contains an integer T , which indicates the number of test cases.
Every test case begins with an integersN , which is the number of the recorded positions.
The second line containsN numbers a1 , a2 , ⋯ , aN , indicating the recorded positions.
Limits
1≤T≤100
1≤N≤105
0<ai≤109
ai<ai+1
Every test case begins with an integers
The second line contains
Limits
Output
For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the minimum time.
Sample Input
136 11 21
Sample Output
Case #1: 4
贪心策略,倒着看,从后往前,后面的速度尽可能的大,这样才能保证总的时间最短
那么就确定了最后一段的时间为1,这样最后一段的速度就最大化了,把这个速度往前面传,用前一段距离除以后一段速度,得到时间,如果得到的时间不是整数,要向上取整(因为要保证前面的速度比后面的速度小),然后再用这个时间算当前段的实际速度,继续把新的速度往前面传……
本来想用double表示速度,ceil向上取整,结果WA,可能是精度损失,换成分数就过了,真坑
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>using namespace std;typedef long long ll;int T, n, kase, ans, t;int a[100005];int main(){cin >> T;kase = 0;while (T--) {scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);}a[0] = 0;ll fenzi, fenmu;ans = 0;for (int i = n - 1; i >= 0; i--) {if (i == n - 1) {t = 1;fenzi = a[i + 1] - a[i];fenmu = 1;ans += t;}else {int dis = a[i + 1] - a[i];fenmu *= dis;swap(fenmu, fenzi);if (fenzi % fenmu) {t = fenzi / fenmu + 1;}else {t = fenzi / fenmu;}ans += t;fenzi = a[i + 1] - a[i];fenmu = t;}}printf("Case #%d: %d\n", ++kase, ans);}return 0;}
0 0
- hdu5935 Car 贪心 卡精度
- hdu5935 Car(贪心,精度)
- HDU5935-Car
- HDU5935 Car (贪心) 2016年中国大学生程序设计竞赛(杭州)
- HDU 5935 Car(贪心、精度爆炸)
- HDU-5935 Car 逆推 贪心 精度
- hdoj 5935 Car (模拟, 精度)
- hdu 5935 Car【贪心】
- HDU 5935 Car 贪心
- HDU 5935 - Car(贪心)
- hdu 5935 Car(贪心)
- HDU 5935 Car(模拟+精度)
- HDU 5935 Car(思维,模拟,精度)
- HDU 5935 Car 贪心 倒推 分数
- HDU-4803-贪心+卡精度(挺难的)
- 延迟 car 选项卡
- Car
- Car
- C++中的数组
- 经济数据
- Windows Server 2003 R2 With Sp2 序列号
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解
- React官方文档--Rendering Elements
- hdu5935 Car 贪心 卡精度
- Linux学习---快捷键和历史命令
- 微软原版 windows server 2003 sp2 R2 系列下载分享
- split替换replace提高多字符串替换速度
- 友盟第三方 5.2.1报 "_OBJC_CLASS_$_QQApiAudioObject", referenced from:
- ubuntu软件管理
- CentOS yum 安装、卸载OpneJDK
- Golang-interface(四 反射)
- PostgreSQL将表导出为CSV(服务器端、客户机端)完整版