基础数据结构第10天:队列(实战篇)

士兵队列训练问题

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    vector<int> s(n);
    for (int i = 0; i < n; i++)
    {
        s[i] = i + 1;
    }
    int num = 1;
    while (s.size() > 3)
    {
        int cnt;
        if (num % 2 == 1)
        {
            cnt = 2;
        }
        else
        {
            cnt = 3;
        }
        vector<int> tem;
        int k = 1;
        for (int i = 0; i < s.size(); i++)
        {
            if (k == cnt)
            {
                k = 1;
                continue;
            }
            tem.push_back(s[i]);
            k++;
        }
        num++;
        s = tem;
    }
    for (int i = 0; i < s.size(); i++)
    {
        cout << s[i] << " ";
    }
    cout << endl;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Team Queue

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;

int main()
{
    int cnt = 1;
    int t;
    while (cin >> t && t)
    {
        cout << "Scenario #" << cnt << endl;
        
        // 创建团队映射(元素值 -> 团队ID)
        vector<int> team_map(1000000, -1);  // 初始化为-1
        
        // 读取团队数据
        for (int i = 0; i < t; i++)
        {
            int n;
            cin >> n;
            for (int j = 0; j < n; j++)
            {
                int element;
                cin >> element;
                team_map[element] = i;  // 标记元素所属团队
            }
        }
        
        // 初始化数据结构
        vector<queue<int>> team_queues(t);   // 每个团队的队列
        queue<int> team_order;               // 团队顺序队列
        vector<bool> in_queue(t, false);     // 标记团队是否在队列中
        
        string command;
        while (cin >> command && command != "STOP")
        {
            if (command == "ENQUEUE")
            {
                int x;
                cin >> x;
                int tid = team_map[x];  // 获取元素所属团队ID
                
                // 如果团队已在队列中
                if (in_queue[tid]) {
                    team_queues[tid].push(x);
                }
                // 如果团队不在队列中
                else {
                    team_queues[tid].push(x);
                    team_order.push(tid);
                    in_queue[tid] = true;
                }
            }
            else if (command == "DEQUEUE")
            {
                // 获取队首团队ID
                int tid = team_order.front();
                // 获取并移除该团队的队首元素
                int x = team_queues[tid].front();
                team_queues[tid].pop();
                
                cout << x << endl;  // 输出出队元素
                
                // 如果该团队队列为空
                if (team_queues[tid].empty()) {
                    team_order.pop();        // 从团队顺序中移除
                    in_queue[tid] = false;   // 更新标记
                }
            }
        }
        
        cnt++;
        cout << endl;  // 测试用例结束后的空行
    }
    return 0;
}

933. 最近的请求次数 - 力扣(LeetCode)

LCR 041. 数据流中的移动平均值 - 力扣(LeetCode)

1700. 无法吃午餐的学生数量 - 力扣(LeetCode)

知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具

相关推荐
Rabitebla4 分钟前
【C++】string 类:原理、踩坑与对象语义
linux·c语言·数据结构·c++·算法·github·学习方法
邪修king12 分钟前
UE5 零基础入门第四弹:UMG UI 系统入门,从静态界面到逻辑联动
c++·ui·ue5
小雅痞1 小时前
[Java][Leetcode middle] 167. 两数之和 II - 输入有序数组
java·算法·leetcode
CN-Dust1 小时前
【C++】输入cin例题专题
java·c++·算法
数模竞赛Paid answer2 小时前
2025年MathorCup数学建模A题汽车风阻预测解题文档与程序
算法·数学建模·mathorcup
Old Uncle Tom8 小时前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent
会编程的土豆8 小时前
洛谷题单入门1 顺序结构
数据结构·算法·golang
生信碱移8 小时前
PACells:这个方法可以鉴定疾病/预后相关的重要细胞亚群,作者提供的代码流程可以学习起来了,甚至兼容转录组与 ATAC 两种数据类型!
人工智能·学习·算法·机器学习·数据挖掘·数据分析·r语言
智者知已应修善业8 小时前
【51单片机中的打飞机设计】2023-8-25
c++·经验分享·笔记·算法·51单片机
智者知已应修善业11 小时前
【51单片机按键调节占空比3位数码管显示】2023-8-24
c++·经验分享·笔记·算法·51单片机