HDU Problem - 5935 Car(模拟)

题目链接

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 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 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 N numbers a1, a2 a 2 , , aN a N , indicating the recorded positions.Limits 1T100 1 ≤ T ≤ 100 1N105 1 ≤ N ≤ 10 5 0<ai109 0 < a i ≤ 10 9 ai<ai+1 a i < a i + 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
AC
  • 题意:一辆汽车每次都是在整点通过关口而且速度是不递减,问这辆汽车走完所有的路程一共花多长时间?
  • 贪心:因为速度保持递增或者不变,后面的速度越大越好,这样整体的速度就大、用时也少,当最后一段路程用时为1秒,能符合假设
  • 用两个整数记录时间和路程,用小数直接处理速度会有精度问题
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define N 100005
using namespace std;

ll a[N];
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif  
    int t;
    scanf("%d", &t);
    int Case = 1;
    while (t--) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%lld", &a[i]);
        }
        // 总时间 
        ll sum = 1;
        // 最后一段路程的时间 和 距离 
        int time = 1, length = a[n] - a[n - 1];
        // 从后往前模拟 
        for (int i = n - 2; i >= 0; --i) {
            ll len = a[i + 1] - a[i];
            // 时间等于路程除以速度 
            ll ti = len * time / length;
            // 如果路程大, 时间加1,满足速度正向递增 
            if (time * len % length)    ti++;
            // 更新当前时间,路程,总时间 
            time = ti;
            length = len;
            sum += ti;
        }
        printf("Case #%d: %lld\n",Case++, sum);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值