5-30 Table Tennis (30分)

1026 Table Tennis (30分) 测试点3,5,7,8无法通过 1026 Table Tennis (30分) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest n 阅读详情

原题

一看就是模拟题, 但就是有些细节有点繁琐..
题意大概是维护一些桌子, 有人来的时候如果有桌子就分配,没有桌子就让他们等待下一张空闲的桌子。

只不过还有一些特殊的VIP桌子和VIP用户,如果当前有空闲的VIP桌子,那就优先分配给已经到达的且当前正在等待队列中的那些VIP用户。若没有空闲的VIP桌子则VIP用户视为普通用户处理。

我的主要思路就是按时间点(currentTime)推进的顺序来逐一处理事件。到达一个新的时间点的时候,首先检查是否可以把VIP桌子分配给VIP用户。之后检查普通的桌子是否能够分配给哪个用户。第三步就是更新时间点currentTime为下一个需要进行处理的时间点。

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cctype>
#include <stack>
#include <list>
#include <vector>
#include <algorithm>

using namespace std;

const int StartTime = 8 * 60 * 60, EndTime = 21 * 60 * 60;

/*
计算过程中的时间均以秒为单位
*/

struct Pair {
    int arriveTime;
    int beginTime;
    int playTime;               // 将要玩的时间(以秒为单位, 【等于0则已经玩过】)
    int waitTime;               // 等待的时间
    bool isVip;
    Pair (int _a = 0, int _b = 0, bool _is = 0) : arriveTime (_a), playTime (_b), isVip(_is) { 
        beginTime = waitTime = 0; 
    }
};

int N, M, tblNum;
list<Pair> players;
int AvailableTime[101];             // 标记桌子下一次能用的时间
int Served[101];                                // 标记桌子用过次数
vector<int> vipTable;

bool CompByArrive (const Pair & p1, const Pair & p2) {
    return p1.arriveTime < p2.arriveTime;
}

bool CompByBegin (const Pair & p1, const Pair & p2) {
    return p1.beginTime < p2.beginTime;
}

int TimeToInt (char times[]) {
    int tmp = 0;
    char * result = strtok (times, ":");
    tmp += atoi (result) * 60;
    result = strtok (NULL, ":");
    tmp = (tmp + atoi (result) ) * 60;
    result = strtok (NULL, ":");
    tmp += atoi (result);
    return tmp;
}

string IntToTime (int time) {
    char result[20];
    int hour = time / ( 60 * 60 );
    time -= hour * 60 * 60;
    int minute = time / 60;
    time -= minute * 60;
    sprintf (result, "%02d:%02d:%02d", hour, minute, time);
    return string(result);
}

int nextPlayerTime ( ) {
    int earliest = EndTime;
    for ( auto it = players.begin ( ); it != players.end ( ); it++) {
        if( it->playTime > 0 )  earliest = min (it->arriveTime, earliest);
    }
    return earliest;
}

void SetPlayer (Pair & player, int tbl, int currentTime) {
    AvailableTime[tbl] = currentTime + min (player.playTime, 2 * 60 * 60);      // 最多用2小时
    player.beginTime = currentTime;
    player.waitTime = currentTime - player.arriveTime;
    player.playTime = 0;
    Served[tbl]++;
}

int main ( ) {
    char strings[20];
    int playtime, isVIP;
    Pair player;
    // 读入数据
    cin >> N;
    for ( int i = 1; i <= N; i++ ) {
        cin >> strings >> playtime >> isVIP;
        players.push_back (Pair (TimeToInt (strings), playtime * 60, isVIP));
    }
    cin >> tblNum >> M;
    for ( int i = 0, tmp; i < M; i++) {
        cin >> tmp;
        vipTable.push_back (tmp);
    }

    // 模拟
    int currentTime = StartTime;
    list<Pair>::iterator it_player;
    for ( int i = 1; i <= tblNum; i++){
        AvailableTime[i] = StartTime;
    }

    players.sort(CompByArrive);
    do {
        // 找VIP桌子
        for ( int i = 0; i < vipTable.size ( ); i++ ) {
            if ( AvailableTime[vipTable[i]] <= currentTime ) {
                for ( it_player = players.begin ( ); it_player != players.end ( ); it_player++ ) {
                    if ( it_player->isVip && it_player->arriveTime <= currentTime && it_player->playTime > 0  ) {
                        SetPlayer (*it_player, vipTable[i], currentTime);
                        break;                          // 注意分配完一张桌子一定要及时break否则继续找Pair很可能会再分配这张桌子第二次
                    }
                }
            }
        }
        // 找普通桌子
        for ( int i = 1; i <= tblNum; i++ ) {
            if ( AvailableTime[i] <= currentTime ) {
                for ( it_player = players.begin ( ); it_player != players.end ( ); it_player++ ) {
                    if ( it_player->playTime > 0 && it_player->arriveTime <= currentTime ) {
                        SetPlayer (*it_player, i, currentTime);
                        break;
                    }
                }
            }
        }
        // 更新下一次的时间
        currentTime = EndTime;
        for ( int i = 1; i <= tblNum; i++) {
            currentTime = min (currentTime, AvailableTime[i]);
        }
        currentTime = max (currentTime, nextPlayerTime ( ));
    } while ( currentTime < EndTime );

    players.sort (CompByBegin);
    for ( it_player = players.begin ( ); it_player != players.end ( ); it_player++) {
        if ( it_player->playTime == 0 ) {
            cout << IntToTime (it_player->arriveTime) << " " << IntToTime (it_player->beginTime) << " " <<
                ( it_player->waitTime + 30 )/ 60 << endl;
        }
    }
    for ( int i = 1; i <= tblNum; i++ ) {
        cout << Served[i] << ( i < tblNum ? " " : "\n" );
    }
    system ("pause");
}
PAT真题练习(甲级)1026 Table Tennis (30 分)(含详细注释) PAT真题练习(甲级)1026 Table Tennis (30 分) 原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560 A table tennis club has N tables available to the public. The tables are number... 阅读详情

相关推荐

1026 Table Tennis (30 分)模拟排列问题

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the ava...

大山喵写博客的地方 2573

Table Tennis(强调几个注意事项)

Table Tennis题目答案参考注意 题目 A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest n

ljhsq的博客 1926

电源完整性之Cadence Sigrity Power DC_电热协同仿真

之前和大家分享过电源完整性之仿真设计原理链接: link.今天接着上一篇文章总结一下电源直流压降的的仿真操作流程及一些simulation中的设置参数,用到的是Cadence Sigrity 中的Power DC组件,来操作一下电热协同仿真,下面开始享受仿真的快乐吧: 第0步:仿真简介 电热协同仿真把电和热相互的影响考虑到了一起。因为电导率并不是固定不变的,而是和温度有关,随着温度的升高导电率会下降。同样电流流径电阻的时候会产生热量,此热量为焦耳热。因此为了获得准确的仿真信息,应考虑电与热的互相影响,进行电

刘小同学的博客 1万+

1026 Table Tennis (30 分)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the ...

雪之下 588

JavaScript比较与逻辑运算符底层原理详解

JavaScript中的比较运算符(如==、===、>)和逻辑运算符(如&&、||)并非简单返回布尔值,而是基于类型转换规则(ToNumber、ToPrimitive、ToBoolean)执行抽象操作的确定性过程。理解其背后的标准协议——抽象相等比较、严格相等判断、关系运算数值化,以及逻辑运算的短路与原值返回机制,是避免隐式转换陷阱、写出可预测代码的关键。这些机制直接支撑着条件判断、默认值处理、安全访问等高频工程实践,广泛应用于表单校验、API响应处理、状态管理及UI渲染逻辑中。

weixin_32075843的博客 338

区块链入门:用账本、乐高和三台旧电脑讲懂分布式账本

区块链是一种去中心化的分布式账本技术,其核心原理是通过密码学哈希、共识机制和链式结构实现数据不可篡改与多方共同验证。它不依赖单一中心,而是将交易记录同步复制到多个节点,依靠数学规则而非人工信任达成一致,从而在数字世界构建可验证的信用基础设施。这种‘集体见证’模式显著提升了系统抗单点故障与人为干预的能力,广泛应用于供应链溯源、数字身份、教育管理及公益透明等需强信任保障的场景。本文以账本类比、乐高拆解和Ganache实操为线索,面向零基础读者通俗解析哈希值、挖矿与智能合约等关键概念。

weixin_30517215的博客 280

PAT 甲级 1026 Table Tennis(30 分) 完整的逻辑分析

原题地址:PAT Adv 1026 题目正文: 1026 Table Tennis(30 分) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open...

wsxyh1071652438的博客 2572

SAP MM采购合同创建全解析:ME31K操作与后台逻辑

在SAP ERP的物料管理模块中,采购合同作为框架协议的核心,定义了企业与供应商之间的长期供货关系。它与采购订单不同,合同不直接产生收货或发票,而是为后续采购订单提供数据源。创建合同(事务码ME31K)时,需要区分数量合同与金额合同,并维护抬头、行项目、有效期、价格条件和税码等关键信息。SAP通过源确定机制在创建采购订单时自动匹配合同,同时审批策略、编码范围和输出类型也会影响合同的实际可用性。掌握合同创建逻辑,能有效解决采购订单无法带出合同、审批未触发、价格缺失等高频问题。从ME31K实操出发,系统梳理采购

weixin_30000735的博客 206

Table Tennis

B. Table Tennis time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output n people are standing in a line to play table tennis. At first, the first ...

abc1235454的博客 858

【华为机试真题 Python实现】分苹果

《华为机试真题》专栏含牛客网华为专栏、华为面经试题、华为OD机试真题。如果您在准备华为的面试,期间有想了解的可以私信我,我会尽可能帮您解答,也可以给您一些建议!本文解法非最优解(即非性能最优)。A,B两个人把苹果分为两堆,A希望按照它的计算规则等分苹果,他的计算规则是按照二进制加法计算,并且不计算走位,12+5=9(1100+0101=9), B的计算规则是十进制加法,包括正常进位,B希望在满足A的情况下获取苹果重量最多,输入苹果的数量和每个苹果重量,输出满足A的情况下获取的苹果总重量, 如果无法满足A的要

3417

【Proteus仿真】51单片机+DAC0832+数码管制作0-25V电压可调

【Proteus仿真】51单片机+DAC0832+数码管制作0-25V电压可调

perseverance51博客 2120

1026 Table Tennis (30分)测试点5没过

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables

muAluo的博客 392

AI技术在电商中的实际应用

人工智能、电商、推荐系统、个性化、机器学习、深度学习 1. 背景介绍 电子商务行业近年来发展迅速,用户数量不断增长,商品种类日益丰富。然而,随着市场竞争的加剧,电商平台面临着如何提升用户体验、提高转化率、挖掘潜在商机等一系列挑战。人工智能(AI)技术作为一种新兴技术,凭借其强大的数

AGI×大数据,开启智能时代的认知跃迁;解码AGI,赋能数据驱动的智能革命。 1747

A1026 Table Tennis (30 分)

#include&lt;cstdio&gt; #include&lt;cmath&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; using namespace std; const int K = 110; const int INF = 1e9; struct player { int arrival, start, train; ...

A_Aria的博客 446

AMBA-CHI协议详解(十)

该规范提供了一个Request Retry(请求重试)机制,确保当请求到达一个Completer 时,它要么被接受,要么被给予一个RetryAck响应,以防止阻塞REQ通道。

Crazzy_M的博客 996

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

1026 Table Tennis (30分)

文章目录1 题目2 解析2.1 题意2.2 思路3 参考代码 1 题目 1026 Table Tennis (30分) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some t...

韩江雪de 小屋 715

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

1026 Table Tennis (30 分)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the ...

SourDumplings的博客 602

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

1026. Table Tennis (30)-PAT甲级真题(模拟)

1026. Table Tennis (30) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they wi

柳婼 の blog 3960

1026 Table Tennis (30 分)(C++)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the ava...

qq_41562704的博客 972

1026 Table Tennis (30分)

1026Table Tennis(30分) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they wi...

chen_zan_yu_的博客 317

PAT 甲级1026 Table Tennis (30 分)

PAT 甲级1026 Table Tennis (30 分) 题目 A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arri...

努力不脱发的程序媛 505

【PAT甲级真题】- Table Tennis (30)

对于 vip 的分配如果 vip 进入队列时,有空的桌子,那么要尝试寻找 vip 桌,vip 会被分配到编号最小的 vip 桌,而不是编号最小的空桌子可能会有多张桌子在同一时刻空闲,这时,等待队列当中的分配顺序是先尝试给 vip 分配vip桌子,再考虑普通人如果没有 vip 桌子可以分配,那么 vip 会被视作普通人,轮到才分配round是四舍五入取整,不是向上取整(牛客没有这个样例,PTA第8个测试点是)可能会出现 21 点来到的用户,此时不应把这种用户加入计算。

ppparaneciym的博客 623
上一篇: uva 804 Petri Net 模拟
下一篇: Uva 127 模拟+细节
_星尘_
博客等级 码龄16年 5粉丝 78原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值