http://acm.hdu.edu.cn/showproblem.php?pid=5935
到比赛结束也没有理解题意...以为每一段的速度不是一个定值,不知道题目想干嘛。
哦,原来每一段速度是一个定值,那么就是每一段路程的速度从左到右是非递减,且每一段所用的时间是整数。
那么最后一段路程的时间一定是1,保证最快(小于1不可能,大于2:因为是最后一段,速度没有上限,所以一秒可行,比>=2的优)
于是我们倒推倒数第二段路程所花费的时间,t = 该段路程 / 上一段的速度,如果刚好整除,那就用这个速度,因为是非递减,如果不整除,那么把时间补全到整数,向上取整。
确定了时间之后,再v = 该段路程 / 该段时间。以此倒推,注意速度可以不是整数,所以直接用了分数。
数据量比较大,io流同步没关之前超时了...
//
// main.cpp
// 5935 Car 贪心 分数
//
// Created by czf on 2016/10/31.
// Copyright © 2016年 czf. All rights reserved.
//
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 100;
LL temp[MAXN], a[MAXN];
struct F {
LL fz, fm;
F & operator = (const F &rhs) {
fz = rhs.fz;
fm = rhs.fm;
return *this;
}
F operator / (const F &rhs) const {
F ret;
ret.fz = fz * rhs.fm;
ret.fm = fm * rhs.fz;
LL temp = __gcd(ret.fz, ret.fm);
ret.fz /= temp;
ret.fm /= temp;
return ret;
}
LL getv() {
return fz / fm;
}
bool check() {
return fz % fm;
}
};
int main() {
std::ios::sync_with_stdio(false);
int t, kase = 0; cin >> t;
while (t--) {
int n; cin >> n;
for (int i = 1; i <= n; i ++) cin >> temp[i];
for (int i = 1; i <= n; i ++) a[i] = temp[i] - temp[i-1];
F v{a[n], 1}; LL res = 1;
for (int i = n-1; i >= 1; i --) {
F dist = {a[i], 1};
F tim = dist / v;
if (tim.check()) tim.fz += tim.fm;
res += tim.getv();
F temp = {tim.getv(), 1};
v = dist / temp;
}
cout << "Case #" << ++kase << ": " << res << '\n';
}
return 0;
}