Bets CodeForces - 69B 贪心 Elevator CodeForces - 117A 模拟

本文介绍了两道CodeForces的编程题目,分别是69B和117A。解题策略分别涉及贪心算法和模拟方法。对于69B,关键在于理解题意;而117A可能需要对选择语句的各种情况有深入思考。通过这两题的解析,有助于提升编程思维和解题技巧。

一道挺水的题,好好读题理解题意就可以做了
题目链接

代码如下:

#include <iostream>
using namespace std;
const int MAX =1e3+5;
struct Node
{
    int t;
    int c;
}node[MAX];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        node[i].t=1e7;
        node[i].c=0;
    }
    int l,r,t,c;
    for(int i=0;i<m;i++){
        cin>>l>>r>>t>>c;
        for(int j=l;j<=r;j++){
            if(t < node[j].t) //新的运动员跑这段时间短
                node[j].c = c,node[j].t=t; //那么得替换成新的
        }
    }
    int total=0;
    for(int i=1;i<=n;i++){
        total+=node[i].c;
    }
    cout<<total<<endl;;
    return 0;
}

第二题有点烦人,就选择语句的情况要好好考虑一下,仔细点就能过了。

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;

    int s,f,t;
    for(int i=0;i<n;i++){
        cin>>s>>f>>t;
        int flag = t/(m-1); // 如果 flag为偶数说明正在向上开,如果不是偶数 说明正在向下开
        int yushu = t%(m-1);
        int floor;
        if(flag&1){
            floor = m-yushu;
        }
        else floor = 1+yushu;
        int totaltime=0;

        if(s>floor) // 他在当前楼层的上面,并且电梯往上开
        {
            if(flag%2==0)
                totaltime = s-floor;
            else totaltime = floor-1+s-1,flag=0; //这个时候往上开啦  往下开然后变成往上
            floor = s;
        }
        else if(s<floor){
            if(flag%2==0) //如果往上开
            {
                totaltime = (m-floor)+(m-s);
                flag = 1; // 要改成往下开
            }
            else totaltime = floor-s;
            floor = s;
        }
        //接到人了,然后就是判断坐电梯上去还是下去了

        if(s==f){
            totaltime = t;
        }
        else{
            if(floor > f) // 如果电梯的位置在要去的下面
            {
                if(flag%2==0){
                    totaltime += (m-floor+m-f);
                }
                else totaltime += floor-f;
            }
            else if(floor<f){
                if(flag%2==0)
                    totaltime += f-floor;
                else totaltime += floor-1+f-1;
            }
            totaltime+=t;
        }
        cout<<totaltime<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值