201812-1(小明上学),201812-2(小明放学)

//201812-1
#include <bits/stdc++.h>

using namespace std;

int main() {
    //灯的工作时间顺序
    //红绿黄
    int r, y, g;
    scanf("%d%d%d", &r, &y, &g);
    int n;
    scanf("%d", &n);
    int totalTime=0;
    while (n--) {
        int k,t;
        scanf("%d%d", &k, &t);
        if(k==0)
            totalTime+=t;
        else if(k==1)
            totalTime+=t;
        else if(k==2)
            totalTime+=t+r;
    }

    printf("%d", totalTime);
    return 0;
}

//201812-2
#include <bits/stdc++.h>

using namespace std;

int main() {
    long long light[3], N, k, t, totalTime = 0;
    //变化顺序红绿黄
    //红    黄    绿
    scanf("%lld%lld%lld%lld", &light[0], &light[2], &light[1], &N);
    long long sum = light[0] + light[1] + light[2];  //sum为红绿灯变换一周的总时长
    while (N--) {
        scanf("%lld%lld", &k, &t);
        if (k == 0)  //是道路
            totalTime += t;  //时长直接递增

            //k的表示
            //红灯1    黄灯2    绿灯3
        else {  //是红绿灯
            if (k == 1)  //将红绿灯标号变为light数组的下标
                k = 0;
            else if (k == 3)
                k = 1;
            t = (light[k] - t + totalTime) % sum;  //该红绿灯变换的最后一周的时长
            while (t > light[k]) {  //若t比当前红绿灯时长长
                t -= light[k];  //减去当前的红绿灯时长
                k = (k + 1) % 3;  //转向下一个红绿灯
            }

            if (k == 0)  //是红灯
                totalTime += light[k] - t;  //加上红灯剩余时长
            else if (k == 2)  //是黄灯
                totalTime += light[k] - t + light[0];  //加上黄灯剩余时长以及红灯时长
        }
    }

    printf("%lld", totalTime);
    return 0;
}

//201812-2
#include <bits/stdc++.h>

using namespace std;

int main() {
    //灯的变化顺序
    //红绿黄
    int light[3] = {0};
    scanf("%d%d%d", &light[0], &light[2], &light[1]);
    int n;
    scanf("%d", &n);
    int totalTime = light[0] + light[1] + light[2];
    long long use = 0;
    while (n--) {
        long long k, t;
        scanf("%lld%lld", &k, &t);
        if (k == 0)
            use += t;
        else {
            if (k == 1) {
                k = 0;
            } else if (k == 3) {
                k = 1;
            }

            long long time = (light[k] - t + use)%totalTime;

            while (time > light[k]) {
                time -= light[k];
                k = (k + 1) % 3;
            }

            if (k == 0)
                use += light[k] - time;
            else if (k == 2)
                use += light[k] - time + light[0];
        }
    }
    printf("%lld\n", use);
    return 0;
}

//201812-2
#include <bits/stdc++.h>

using namespace std;

int main() {
//    红绿灯工作顺序->红->绿->黄
    vector<int> v(3);
    scanf("%d%d%d", &v[0], &v[2], &v[1]);
    int n;
    scanf("%d", &n);
    long long res = 0;
    long long totalTime = v[0] + v[1] + v[2];//红绿灯循环一次使用的时间
    long long useTime = 0;//当前小明使用的总时间
    int idx;//红绿灯索引
    int state;//当前红绿灯的状态
    while (n--) {
        int k, t;
        scanf("%d%d", &k, &t);
        if (k == 0) {
            res += t;
            useTime += t;
            continue;
        } else if (k == 1)
//            红灯
            idx = 0;
        else if (k == 2)
//            黄灯
            idx = 2;
        else if (k == 3)
//            绿灯
            idx = 1;

//        将当前使用的总时间转换为一次红绿灯循环内的时间
        long long temp = useTime % totalTime;
//        转换灯
        while (temp >= t) temp -= t, t = v[++idx % 3];
        t -= temp;

//            获取当前是什么灯
        state = idx % 3;
        if (state == 0)//红灯
            res += t, useTime += t;
        else if (state == 2)//黄灯
            res += t + v[0], useTime += t + v[0];

    }
    printf("%lld\n", res);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
201812-2小明放学的问题是关于小明利用“智慧光明”终端给出的信息,估算自己放学回家的时间。在这道题目中,小明已经规划好了回家的路线,并能够预测经过各个路段的时间。此外,小明还能够看到出发时刻路上经过的所有红绿灯的指示状态。根据题目的描述,我们可以推导出小明此次回家所需要的时间。 首先,题目给出了红绿灯的设置,包括红灯、黄灯和绿灯分别需要的时间。接下来,题目给出了小明经过的道路段数和路过的红绿灯数目。然后,题目给出了每个道路段和红绿灯的状态,包括经过的道路需要的时间和红绿灯的状态以及倒计时显示的数字。 根据题目的要求,我们需要不断地推演现在的时间下新经过的红绿灯状态。可以将红灯和黄灯放在一起,因为这两个状态下都得等待,然后将绿灯状态单独分开。这样就将问题简化为两个区间,即红黄和绿。通过计算每个区间所需的时间,就可以得到小明放学回家所用的总时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [CCF 201812-2----小明放学----区间问题](https://blog.csdn.net/weixin_44778155/article/details/101158855)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值