您的位置:首页 > 其它

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代码】

#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
相关文章推荐