201812-2小明放学

题目背景

  汉东省政法大学附属中学所在的光明区最近实施了名为“智慧光明”的智慧城市项目。具体到交通领域,通过“智慧光明”终端,可以看到光明区所有红绿灯此时此刻的状态。小明的学校也安装了“智慧光明”终端,小明想利用这个终端给出的信息,估算自己放学回到家的时间。

问题描述

  一次放学的时候,小明已经规划好了自己回家的路线,并且能够预测经过各个路段的时间。同时,小明通过学校里安装的“智慧光明”终端,看到了出发时刻路上经过的所有红绿灯的指示状态。请帮忙计算小明此次回家所需要的时间。

输入格式

  输入的第一行包含空格分隔的三个正整数 r、y、g,表示红绿灯的设置。这三个数均不超过 106。
  输入的第二行包含一个正整数 n,表示小明总共经过的道路段数和路过的红绿灯数目。
  接下来的 n 行,每行包含空格分隔的两个整数 k、t。k=0 表示经过了一段道路,将会耗时 t 秒,此处 t 不超过 106;k=1、2、3 时,分别表示出发时刻,此处的红绿灯状态是红灯、黄灯、绿灯,且倒计时显示牌上显示的数字是 t,此处 t 分别不会超过 r、y、g。

输出格式

  输出一个数字,表示此次小明放学回家所用的时间。

样例输入

30 3 30
8
0 10
1 5
0 11
2 2
0 6
0 3
3 10
0 3

样例输出

46

样例说明

  小明先经过第一段路,用时 10 秒。第一盏红绿灯出发时是红灯,还剩 5 秒;小明到达路口时,这个红绿灯已经变为绿灯,不用等待直接通过。接下来经过第二段路,用时 11 秒。第二盏红绿灯出发时是黄灯,还剩两秒;小明到达路口时,这个红绿灯已经变为红灯,还剩 11 秒。接下来经过第三、第四段路,用时 9 秒。第三盏红绿灯出发时是绿灯,还剩 10 秒;小明到达路口时,这个红绿灯已经变为红灯,还剩两秒。接下来经过最后一段路,用时 3 秒。共计 10+11+11+9+2+3 = 46 秒。

评测用例规模与约定

  有些测试点具有特殊的性质:
  * 前 2 个测试点中不存在任何信号灯。
  测试点的输入数据规模:
  * 前 6 个测试点保证 n ≤ 103。
  * 所有测试点保证 n ≤ 105。

做完了只有20分,想了2天,想着去看看别人写的,但是看了看思路就很烦躁,偶然间看评论说黄灯是不能过的!!!!!!!!!!!!!!!!要流泪了。

 同学们,我的血泪经验告诉你们黄灯是不可以闯的。

下面是代码(100分):

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 ll r, y, g, n;
ll judge(ll color, ll remain, ll currentime)
{
    ll total = currentime;

    if(color==1)//出发时刻是红灯
    {
        if(currentime<remain)
        {
            total = remain;
        }
        else
        {
            int remain_time = (currentime-remain)%(r+y+g);
            if(remain_time>=g)
            {
                total = r-(remain_time-y-g)+currentime;
            }
        }

    }
    else if(color==2)//出发时刻是黄灯
    {
        if(currentime<remain)
        {
            total = remain+r;
        }
        else
        {
         int remain_time = (currentime-remain)%(r+y+g);
         //cout << "remain_time" << remain_time << endl;
         if(remain_time<r)
         {
             total = r-remain_time+currentime;
             //cout << "total" << total << endl;
         }

         else if(remain_time>=r+g)
         {
             total = y-remain_time+2*r+g+currentime;
         }
        }
    }
    else if(color==3)//出发时刻是绿灯
    {

        int remain_time = (currentime-remain)%(r+y+g);
        if(currentime>=remain&&remain_time>=y && remain_time<y+r)
        {
            total = currentime+r-remain_time+y;

        }
        else if(currentime>=remain&&remain_time<y)
        {
            total = y-remain_time+r+currentime;
        }
    }
    return total;
}

int main()
{
    int k ;
    ll t;
    ll toltime = 0;
    scanf("%lld%lld%lld", &r, &y, &g);
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d%lld", &k, &t);
        if(k==0)
            {toltime+=t;
           // cout << "0: " << toltime << endl;
           }
        else if(k==1)
        {toltime=judge(1, t, toltime);
         //cout << "1: " << toltime << endl;
         }
            else if(k==2)

          {toltime=judge(2, t, toltime);
           //cout << "2: " << toltime << endl;
           }
                else if(k==3)
           {

            toltime=judge(3, t, toltime);
             //cout << "3: " << toltime << endl;
             }
    }
    cout << toltime << endl;

  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值