Car HDU – 5935

作者: qwq 分类: ACM 发布时间: 2017-04-05 22:41

被这道题坑了很久。。。。

期间犯了各种错误,一度开始怀疑人生。。

这道题的大意是:

一辆汽车再路上跑,而且速度不会减少。

给你几个数字,每个数字代表汽车在某个整数时间所在的位置,让你算出来这辆汽车在满足速度不减少这个条件时到达最后一个位置最短需要跑多长时间。

所以可以把最后一段路程所需要的时间定为1,在这一段路程的速度就是 距离/时间

比如样例:

0 6 11 21

11-21这一段时间是1;

6-11这一段因为速度不能超过后一段,所以时间也是1;

0-6这一段如果时间是1的话那么速度就会超过第二段,所以时间是2;

一共时间是4

 

首先我们想到的算法是((前一段距离)/(后一段距离))*后一段距离的时间+1

然后发现有BUG,会在某些特定情形下产生错误的解。

后来又改成((double)(前一段距离)/(double)(后一段距离))*后一段距离的时间+1

这时候就发现如果 ((double)(前一段距离)/(double)(后一段距离)) 是整数的话就不用加一,即汽车在两段路上以相同的速度行驶。

但是怎么判断是不是整数呢?这个问题想了挺长的一段时间。

最后发现如果是整数的话 后一段路程*前一段时间==前一段路程*后一段时间

如果不是的话因为int是取整的原因两个会不相等。

这样time就加一就可以了。

 

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 NN 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 00 .

Now they want to know the minimum time that Ruins used to pass the last position.InputFirst line contains an integer TT , which indicates the number of test cases.

Every test case begins with an integers NN , which is the number of the recorded positions.

The second line contains NN numbers a1a1 , a2a2 , , aNaN , indicating the recorded positions.

Limits
1T1001≤T≤100
1N1051≤N≤105
0<ai1090<ai≤109
ai<ai+1ai<ai+1OutputFor 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
#include<iostream>
#include<cstdio>
using namespace std;
int a[100010];
int num=1;
int main(){
	int t;
	cin>>t;
	a[0]=0;
	while(t--){
		int n;
		
		cin>>n;
		n++;
		for(int i=1;i<n;i++){
			scanf("%d",&a[i]);
		}
		int time=1;
		long long  alltime=1;
		int pre;
		for(int i=n-2;i>0;i--){
			pre=time;
		time=((double)(a[i]-a[i-1])*time)/(double)(a[i+1]-a[i]);
		if((a[i+1]-a[i])*time!=(a[i]-a[i-1])*pre){
			time++;
		}
		alltime+=time;
		}
		printf("Case #%d: %lld\n",num++,alltime);
		
		
		
	}
	
	
	
	
	return 0;
} 

 

 

一条评论
  • qwq

    2017年4月5日 下午11:02

    哈哈哈qwq

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注