牛客多校联赛第四场部分题解

L题 Buy Figurines


题目大意


给定n个顾客与其开始时间和消费时间,再给定m个队列,求最短消费总时长

思路

利用数组和循环遍历每个队列。

代码
 

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#pragma warning(disable:4996)
#define ll long long
using namespace std;
struct node {
    ll l, r;
}peo[200005];
bool cmp(node a, node b) {
    return a.l < b.l;
}
int main() {
    int t, n, m, a, s;
    cin >> t;
    while (t--) {
        scanf("%d %d", &n, &m);
        vector<queue<node>> qs(m);
        for (int i = 1; i <= n; i++) {
            scanf("%d %d", &a, &s);
            peo[i].l = a;
            peo[i].r = a + s;
        }
        sort(peo + 1, peo + 1 + n, cmp);  //左端点递增排序

        ll ans = 0;
        for (int i = 1; i <= n; i++) {  //遍历所有区间(顾客)
            //最短队列的元素个数  最短队列下标
            int minSize = n + 1, index = 0;
            bool flag = 0;

            for (int j = 0; j < m; j++) {  //遍历所有m个队列
                //更新队列,即弹出以消费完的顾客
                while (!qs[j].empty() && peo[i].l >= qs[j].front().r) {
                    qs[j].pop();
                }
                      //若当前队列没人排队,则可直接插入(因为必定为人最少且最靠前的队列)
                if (qs[j].size() == 0) {
                    qs[j].push(peo[i]);
                    ans = max(ans, peo[i].r);
                    flag = 1;
                    break;
                }
                //否则更新minSize和index,找出最少人排队且尽量靠前的队列
                if (qs[j].size() < minSize) {
                    minSize = qs[j].size();
                    index = j;
                }
            }
            /*若当前peo[i]还没有插入,说明要插入的队列一定还有元素,
            且要插入的元素紧跟队列最后*/
            if (!flag) {
                s = peo[i].r - peo[i].l;
                peo[i].l = qs[index].back().r;
                peo[i].r = peo[i].l + s;
                qs[index].push(peo[i]);
                ans = max(ans, peo[i].r);
            }
        }
        cout << ans << endl;
    }

    return 0;
}

J题 Bragging Dice

题目大意

y 和 p玩猜骰子的游戏,y开始猜有x个y点数的骰子,轮到p可以选择揭晓或继续猜更大的,一次类推,另外两条关键规则为,当一个盒子里的骰子点数互不相等时视为一个也没有,且两人都知道所有骰子点数。

思路

由于两人都知道点数,那么当盒子里都没骰子时才算p胜利。

代码

#include <iostream>
#include <string.h>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std;
int cup[3][7], sum[7];
int main() {
    int t;
    cin >> t;
    while (t--) {
        memset(cup, 0, sizeof(cup));
        memset(sum, 0, sizeof(sum));
        int n;
        scanf("%d", &n);
        for (int j = 1; j <= 2; j++) {
            for (int i = 0; i < n; i++) {
                int temp;
                scanf("%d", &temp);
                cup[j][temp]++;
                sum[temp]++;
            }
        }
        bool if3[3] = { 0 };  //是否符合规则3
        if (n <= 6) {
            for (int i = 1; i <= 2; i++) {
                bool flag = 1;
                for (int j = 1; j <= 6; j++) {
                    if (cup[i][j] > 1) {
                        flag = 0;
                        break;
                    }
                }
                if (flag) if3[i] = 1;
            }
        }
        if (if3[1] && if3[2]) {
            cout << "Just a game of chance." << endl;
            continue;
        }
        else {
            cout << "Win!" << endl;
            continue;
        }

    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值