题解:P15521 [CCC 2016 J4] Arrival Time

· · 题解

题目描述

如果没有交通拥堵,通勤时间是 2 小时。高峰时段拥堵时,车速会减半。已知出发时间,何时能到达工作地点?

思路

可以理解为到工作地点的距离为 120 个单位,高峰时段拥堵时可行驶 1 个单位,其余时段可行驶 2 个单位。因为速度和时间成反比,所以直接枚举 120 个单位,高峰时段拥堵时需要 2 个单位时间行驶 1 个单位,其余时段需要 1 个单位时间行驶 1 个单位。用一开始的时间加上需要的时间即可。

AC Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int a,b;
    char x;
    cin>>a>>x>>b;//可以直接输入一个字符,不需要string转int
    int t = a*60+b;
    for(int i=1;i<=120;++i){
        if((t >= 420 && t < 600) || (t >= 900 && t < 1140)) t+=2;//高峰时段拥堵时需要2个单位时间行驶1个单位
        else t++;//其余时段需要1个单位时间行驶1个单位
    }
    //cout<<t<<' ';
    if(t/60%24 < 10)cout<<0<<t/60%24<<':';
    else cout<<t/60%24<<':';
    if(t%60 < 10)cout<<0<<t%60;
    else cout<<t%60;
    return 0;
}