题目地址:点击打开链接
一开始以为速度也是整数...然后一直wa.....其实速度一开始就有说是个实数。
参考题解:点击打开链接
题意:有个老司机开车,速度是一组非递减的序列,然后交警只记录了他的位置,问你,这个老司机要经过这些所有的位置最少需要的时间
思路:因为速度要么是不变,要么是递增的,所以经过最后一个位置的时候速度是最大的,又因为没有给出对速度的限制,所以我们就让司机经过最后一段距离的时间为一秒,所以最后一段的速度就是其长度,然后往前递推出前面的速度。又因为时间是个整数,但是速度不一定是个整数,所以这道题还要注意精度问题,就是每次用当前的路程除以速度,然后得到时间的整数,如果这个长度除以这个时间整数不能得到这个速度,那就时间加一,然后算出新的时间,更新时间和答案就行了。
P.S.一开始还以为速度为整数,然后还傻傻地想要用循环来更新时间,然后,不是超时就是WA,然后看了题解才知道,原来还可以是小数,不过也是,毕竟题目一直说的是实数和数字,并没有说是整数。
代码:
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
int a[maxn], t, n, ca = 1;
int main(void)
{
cin >> t;
while(t--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
ll ans = 0;
double spe = a[n]-a[n-1];
for(int i = n; i > 0; i--)
{
double len = (a[i]-a[i-1])*1.0;
int t = len/spe;
ans += t;
if(len/t != spe)
{
ans++;
spe = len/(t+1);
}
}
printf("Case #%d: %d\n", ca++, ans);
}
return 0;
}
Car
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 788 Accepted Submission(s): 265
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.
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
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
Source
Recommend
liuyiding