hdu 5935 Car(贪心)
来源:互联网 发布:大数据 百度百科 编辑:程序博客网 时间:2024/06/04 19:16
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 record N 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.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case begins with an integers N, which is the number of the recorded positions.
The second line contains N numbers a1, a2, ⋯, aN, indicating the recorded positions.
Limits
1≤T≤100
1≤N≤105
0< ai≤109
ai< ai+1
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
1
3
6 11 21
Sample Output
Case #1: 4
题目大意:
比较难懂!有一辆车现在从t=0开始走,速度不能递减(可以匀速,也可以变加速)。警察在t为整数的时候记录了N个车的位置(整数),问到达最后一个位置时这辆车总共开了多长时间。
解题思路:
刚开始以为是递归求解,但是WA了好几发。后来转化一下思路,可以从最后一个距离往前逆序遍历,只要满足速度不能递减,求在这个情况下的最少时间就行了,那么贪心一下,还是蛮好做的。(开始傻逼了)
注意精度问题!
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define eps 1e-8int s[100005];int main(){ int t,n,x,ans,tt=1; double v; scanf("%d",&t); while(t--) { scanf("%d",&n); ans=1; for(int i=1;i<=n;i++) scanf("%d",&s[i]); v=s[n]-s[n-1]; for(int i=n-1;i;i--) { x=s[i]-s[i-1]; if(x<=v+eps) { ans++; v=x; } else { ans+=(int(double(x-eps)/v)+1); v=double(x)/(int(double(x-eps)/v)+1); } } printf("Case #%d: %d\n",tt++,ans); } return 0;}
- HDU 5935 - Car(贪心)
- hdu 5935 Car(贪心)
- hdu 5935 Car【贪心】
- HDU 5935 Car 贪心
- HDU 5935 Car(贪心、精度爆炸)
- HDU 5935 Car 贪心 倒推 分数
- HDU-5935 Car 逆推 贪心 精度
- HDU 5935 Car (贪心)——2016年中国大学生程序设计竞赛(杭州)
- hdu5935 Car(贪心,精度)
- HDU 5935 Car(模拟+精度)
- HDU 5935 Car(思维,模拟,精度)
- hdu 5935 Car
- HDU 5935 Car (模拟)
- hdu 5935 Car
- HDU-5935-Car
- hdu 5935 Car
- HDU 5935 Car
- HDU-5935-Car(杭州CCPC C题)-数学推导+水贪心
- C# 输入法
- XML——c#读取带命名空间的xml
- c++实验,1000到10000之间个位和千位相等的质数
- 米斯特白帽培训讲义(v2)实战篇 余闻同学录
- js基础--this大揭秘
- hdu 5935 Car(贪心)
- 博弈之SG函数(详解为什么异或可以得出结果)
- 蓝桥杯 算法训练 回文数
- BZOJ 4012 [HNOI2015]开店
- java之基础知识
- c++中enum 如何使用
- python学习(八):字符编码
- 解决listview onitemclick 点击无效问题
- ECNU CCCC选拔赛 E题 Teacher Panda and plagiarism dp