2016年中国大学生程序设计竞赛(杭州)C Car(贪心算法+分数处理)
2016-10-31 10:47
323 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935
Car
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 287 Accepted Submission(s): 107
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
【中文题意】
一辆汽车从0点开始出发,给你几个他的时间点到达的地点,但这几个时间点不一定是连续的,问你到达给出的最后一个时间点所花费的时间为多少,对于速度有个要求:后一秒走的路程比大于等于前一秒。可能我描述的不是太清楚,能看懂就好了。
【思路分析】从后往前模拟,速度为非递增的,然后速度用分数来表示就好了,用小数做的挂成傻逼了都快,后来改的分数。
【AC代码】
Car
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 287 Accepted Submission(s): 107
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
【中文题意】
一辆汽车从0点开始出发,给你几个他的时间点到达的地点,但这几个时间点不一定是连续的,问你到达给出的最后一个时间点所花费的时间为多少,对于速度有个要求:后一秒走的路程比大于等于前一秒。可能我描述的不是太清楚,能看懂就好了。
【思路分析】从后往前模拟,速度为非递增的,然后速度用分数来表示就好了,用小数做的挂成傻逼了都快,后来改的分数。
【AC代码】
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL long long LL a[1000005]; int main() { int t,iCase=0; LL n,ans; scanf("%d",&t); while(t--) { ans = 0; scanf("%I64d",&n); for(int i=1; i<=n; i++) scanf("%I64d",&a[i]); a[0] = 0; LL vt = 1; LL vs = a - a[n-1]; LL ti; for(int i=n; i>=1; i--) { ti = (a[i]-a[i-1])*vt/vs;//这块需要使用分数来处理 if((a[i]-a[i-1])*vt%vs) ti++; ans+=ti; vs = a[i] - a[i-1]; vt = ti; } printf("Case #%d: %I64d\n",++iCase,ans); } return 0; }
相关文章推荐
- HDU 5933 ArcSoft's Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
- HDU5935 Car (贪心) 2016年中国大学生程序设计竞赛(杭州)
- 2016年中国大学生程序设计竞赛(杭州)
- HDU 5935 Car (贪心)——2016年中国大学生程序设计竞赛(杭州)
- 2016年中国大学生程序设计竞赛(杭州) 部分题解
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州)-重现赛【01,02,03,06】
- HDU 5938 Four Operations(贪心+细节)——2016年中国大学生程序设计竞赛(杭州)
- 2016年中国大学生程序设计竞赛(杭州) A ArcSoft's Office Rearrangement(贪心)
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU5933 ArcSoft's Office Rearrangement (模拟) 2016年中国大学生程序设计竞赛(杭州)
- (2016年中国大学生程序设计竞赛(杭州)-重现赛) ArcSoft's Office Rearrangement 模拟
- HDU 5933 ArcSoft's Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5935 Car 【模拟】 (2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州)1006 Four Operations
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
- 2016年中国大学生程序设计竞赛(杭州) F Four Operations(暴力枚举)