• ccf/csp 2018 12 小明放学


    试题编号: 201812-2
    试题名称: 小明放学
    时间限制: 1.0s
    内存限制: 512.0MB
    问题描述:
    题目背景
      汉东省政法大学附属中学所在的光明区最近实施了名为“智慧光明”的智慧城市项目。具体到交通领域,通过“智慧光明”终端,可以看到光明区所有红绿灯此时此刻的状态。小明的学校也安装了“智慧光明”终端,小明想利用这个终端给出的信息,估算自己放学回到家的时间。
    问题描述
      一次放学的时候,小明已经规划好了自己回家的路线,并且能够预测经过各个路段的时间。同时,小明通过学校里安装的“智慧光明”终端,看到了出发时刻路上经过的所有红绿灯的指示状态。请帮忙计算小明此次回家所需要的时间。
    输入格式
      输入的第一行包含空格分隔的三个正整数 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
     【思路】:
        使用一个变量来保存时间,到每个红绿黄灯的点在来讨论情况,先讲time % (r + g + y),然后加上余数
        是否会使其灯的状态发生变化,注意:灯的变化顺序为 红 绿 黄 ,求完余后,可以得到,最多就是变化到当前的
        灯色,所以讨论一个< r + g + y的范围就ok了,注意,单个t可以为1e6  n最大为1e5,所以可以超过int范围,所以采用
        longlong
    附上代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1e5 + 5;
    typedef long long ll;
    typedef struct MYINT
    {
        ll flag, time;
    }myint;
    ll r, y, g, n;
    myint arr[MAXN];
    int main()
    {
        while(~scanf("%lld%lld%lld", &r, &y, &g))
        {
            memset(arr, 0, sizeof(arr));
            scanf("%lld", &n);
            for(int i = 0; i < n; i ++)
            {
                scanf("%lld %lld", &arr[i].flag, &arr[i].time);
            }
            ll now = 0;
            for(int i = 0; i < n; i ++)
            {
                if(arr[i].flag == 0)
                {
                    now += arr[i].time;
                }
                else if(arr[i].flag == 1)
                {
                    ll temp;
                    temp = now % (r + g + y);
                    if(temp < arr[i].time)
                    {
                        now += (arr[i].time - temp);
                    }
                    else
                    {
                        if(temp < arr[i].time + g)
                        {
                            now = now;
                        }
                        else if(temp < arr[i].time + g + y)
                        {
                            now += (arr[i].time + g + y - temp + r);
                        }
                        else
                        {
                            now += (arr[i].time + g + y + r - temp);
                        }
                    }
                }
                else if(arr[i].flag == 2)
                {
                    ll temp = now % (r + g + y);
                    if(temp < arr[i].time)
                    {
                        now += (arr[i].time - temp + r);
                    }
                    else
                    {
                    if(temp < arr[i].time + r)
                    {
                        now += (arr[i].time + r - temp);
                        
                    }
                    else if(temp < arr[i].time + r + g)
                    {
                        now = now;
                    }
                    else
                    {
                        now += (arr[i].time + g + r + y - temp + r);
                    }
                    }
                }
                else if(arr[i].flag == 3)
                {
                    ll temp = now % (r + g + y);
                    if(temp < arr[i].time)
                    {
                        now = now;
                    }
                    else
                    {
                        if(temp < arr[i].time + y)
                        {
                            now += (r + (y + arr[i].time - temp));
                        }
                        else if(temp < arr[i].time + y + r)
                        {
                            now += (arr[i].time + y + r - temp);
                        }
                        else
                        {
                            now = now;
                        }
                    }
                }
            }
            printf("%lld
    ", now);
        }
        return 0;
    }
  • 相关阅读:
    python删除hbase一整列数据
    selenium基本操作
    python去除html标签及标签里面的内容
    Ambari-server 启动错误
    scala 命名规范
    HDFS坏块修复
    Nodejs+MySql+Echart(html模板渲染)
    Ambari openssl错误
    设置mysql 远程连接
    JMS 简介笔记
  • 原文地址:https://www.cnblogs.com/qq136155330/p/10313216.html
Copyright © 2020-2023  润新知