HDU 5935 - Car(贪心)

Car

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2701    Accepted Submission(s): 728


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
1T100
1N105
0<ai109
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
 

Source
 

题意:
在n个点上有摄像头,进过这几个点的时间必须是整数,你从0秒开始,0点开始。速度只能增加或不变,求用时最少。

POINT:
从最后往前推,最后的速度肯定是V=x[n]-x[n-1] m/s,让最后一段用时1秒,然后用这个速度去比较前面一段,看看能不能整除,不能整除就在(x[n-1]-x[n-2])/V的时间上向上取整。在算出这个速度。重复做这个操作。O(n)

#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
#define ll long long int
#define inf 0x7fffffff
const int maxn = 1e5 + 10;
const double eps = 0.00000001;
double dp[maxn];
int c[maxn];
int main()
{
	int T;
	int n;
	int cs = 0;
	scanf("%d", &T);
	while (T--)
	{
		cs++;
		memset(dp, 0, sizeof(dp));
		memset(c, 0, sizeof(c));
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &c[i]);		
			dp[i] = c[i] - c[i - 1];
		}
		double Speed = dp[n];
		int cnt = 1;
		for (int i = n - 1; i >= 1; i--)
		{
			double Tmp = dp[i] / Speed;
			if (fabs(Tmp-(int)Tmp)<=eps)
			{
				cnt = cnt + (int)Tmp;
			}
			else
			{
				cnt = cnt + (int)Tmp + 1;
				Speed = dp[i] / ((int)Tmp + 1);
			}
		}
		printf("Case #%d: %d\n", cs, cnt);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值