PAT 1026 Table Tennis[比较难]
1026 Table Tennis (30)(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 number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.
Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.
One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.
Input Specification:
Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players' info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.
Output Specification:
For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.
Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2
我的代码:
#include <stdio.h>
#include<iostream>
#include <algorithm>
using namespace std; struct Play{
int arrive,serve,ing,ed,vip;
int waiting;
Play(){
serve=-;//表示没被服务。
}
}play[];
struct Table{
int vip,people;
int ing;//是否正在使用
Table(){
vip=;ing=-;people=;
}
}table[];
bool cmp(Play& a,Play& b){
return a.arrive<b.arrive;//从小到大。
}
bool cmp2(Play& a,Play& b){
return a.serve<b.serve;
}
int main() {
int n,m,vm,vmno;
scanf("%d",&n);
int h,miu,s,bg=*;
for(int i=;i<n;i++){
scanf("%d:%d:%d",&h,&miu,&s);
play[i].arrive=h*+miu*+s-bg;
scanf("%d %d",&play[i].ing,&play[i].vip);
play[i].ing*=;
if(play[i].ing>)
play[i].ing=;
}
sort(play,play+n,cmp);
scanf("%d%d",&m,&vm);
for(int i=;i<=vm;i++){
scanf("%d",&vmno);
table[vmno].vip=;//是vip桌子。
}
// for(int i=0;i<n;i++){
// printf("%02d:%02d:%02d ",play[i].arrive/3600+8,play[i].arrive%3600/60,play[i].arrive%60);
// printf("%d %d\n",play[i].ing,play[i].vip);
// //printf("%02d:%02d:%02d ",play[i].serve/3600+8,play[i].serve%3600/60,play[i].serve%60);
// } //一定要注意要服务一个人的时候,需要保证那个人已经到了。。。
//注意,题目要求,如果当前队列中没有VIP选手,那么正常选手可以使用VIP桌子。
int ct=;
for(int tm=;tm<;tm++){
//判断当前是否有结束的
//不能用来计数,因为有可能serve到。
if(ct==n-)break;//人已经分配完了。
for(int i=;i<=m;i++){
if(table[i].ing!=-&&play[table[i].ing].ed==tm){//判断当前服务是否结束
table[i].ing=-;
}
}
int bg=,j,k;//记录开始循环的地方。
for(int i=;i<=m;i++){
j=bg;
if(table[i].ing==-&&table[i].vip==){//分配空闲vip桌子,分配vip用户。
for(j=bg;j<n;j++){
if(play[j].arrive<=tm&&play[j].vip==&&play[j].serve==-){//当前选手是vip,且没被服务
table[i].ing=j;
table[i].people++;
play[j].serve=tm;
play[j].ed=tm+play[j].ing;ct++;
// printf("%d %d %d %d vip\n",i,j,tm,tm-play[j].arrive);
break;//跳出内层循环
}
}
bg=j;
}
}
bg=;
for(int i=;i<=m;i++){
k=bg;
if(table[i].ing==-){//分配桌子与群众
for(k=bg;k<n;k++){
if(play[k].arrive<=tm&&play[k].serve==-){
table[i].ing=k;
table[i].people++;
play[k].serve=tm;
play[k].ed=tm+play[k].ing;ct++;
// printf("%d %d %d %d\n",i,j,tm,tm-play[j].arrive);
break;
}
}
bg=k;//加了这个之后就可以了,不用每次都从头开始遍历。有一个标记。
}
} }
ct=;
sort(play,play+n,cmp2);
for(int i=;i<n;i++){
if(play[i].serve!=-){
printf("%02d:%02d:%02d ",play[i].arrive/+,play[i].arrive%/,play[i].arrive%);
printf("%02d:%02d:%02d ",play[i].serve/+,play[i].serve%/,play[i].serve%);
if((play[i].serve-play[i].arrive)%>=)
ct=;
else
ct=;
printf("%d\n",(play[i].serve-play[i].arrive)%/+ct);
}
}
printf("%d",table[].people);
for(int i=;i<=m;i++){
printf(" %d",table[i].people);
}
return ;
}
//!!!在牛客网上AC 了,但是在pat上就只有16分,只通过了两个点,其他的都是答案错误,真是哭唧唧,完全不知道该怎么改了。!!先放这。明天复习一下。绝望了。
其中第一次提交通过80%,有如下没通过:
测试用例:
4
08:00:00 60 0
08:10:00 50 0
08:50:00 30 0
09:00:00 30 1
2 1
2
对应输出应该为:
08:00:00 08:00:00 0
08:10:00 08:10:00 0
09:00:00 09:00:00 0
08:50:00 09:00:00 10
2 2
你的输出为:
08:00:00 08:00:00 0
08:10:00 08:10:00 0
08:50:00 09:00:00 10
改了cmp2函数之后,提交之后报:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。
看来我这么循环着做,不太对啊。复杂度太高了,一直循环。
大佬的代码:https://www.liuchuo.net/archives/2955
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct person {
int arrive, start, time;
bool vip;
}tempperson;
struct tablenode {
int end = * , num;
bool vip;
};
bool cmp1(person a, person b) {
return a.arrive < b.arrive;
}
bool cmp2(person a, person b) {
return a.start < b.start;
}
vector<person> player;
vector<tablenode> table;
void alloctable(int personid, int tableid) {
if(player[personid].arrive <= table[tableid].end)
player[personid].start = table[tableid].end;
//如果客户已经到了,当前vip桌子还没结束,那么开始服务时间就被设置为这个桌子的结束时间
//这个不按照时间线过来,还是有点颠覆我以前的做法了。
else
player[personid].start = player[personid].arrive;
table[tableid].end = player[personid].start + player[personid].time;
//当前桌子的结束时间被设置为客户开始服务时间+客户需服务时间
table[tableid].num++;
}
int findnextvip(int vipid) {
vipid++;
while(vipid < player.size() && player[vipid].vip == false) {
vipid++;
}
return vipid;
}
int main() {
int n, k, m, viptable;
scanf("%d", &n);
for(int i = ; i < n; i++) {
int h, m, s, temptime, flag;
scanf("%d:%d:%d %d %d", &h, &m, &s, &temptime, &flag);
tempperson.arrive = h * + m * + s;//转换成秒
tempperson.start = * ;//所有的开始服务时间都被设置为最终点。
if(tempperson.arrive >= * )
continue;
tempperson.time = temptime <= ? temptime * : ;//限制时间。
tempperson.vip = ((flag == ) ? true : false);
player.push_back(tempperson);
}
scanf("%d%d", &k, &m);
table.resize(k + );
for(int i = ; i < m; i++) {
scanf("%d", &viptable);
table[viptable].vip = true;
}
sort(player.begin(), player.end(), cmp1);//对向量可以这么排序。
int i = , vipid = -;
vipid = findnextvip(vipid);//找到队列中vip所在的下标
while(i < player.size()) {//这个是基于客户来循环。
int index = -, minendtime = ;
for(int j = ; j <= k; j++) {//k表示窗口的数量
if(table[j].end < minendtime) {
minendtime = table[j].end;//对每个桌子标记了结束时间。
index = j;//找到最近时间空闲的窗口
}
}
if(table[index].end >= * )
break;//如果所有桌子的最小服务结束时间>=21*3600,那么就结束
if(player[i].vip == true && i < vipid) {
i++;
continue;
}
if(table[index].vip == true) {
if(player[i].vip == true) {//如果当前客户i是vip客户。
alloctable(i, index);
if(vipid == i)
vipid = findnextvip(vipid);
i++;
} else {
if(vipid < player.size() && player[vipid].arrive <= table[index].end) {
//如果当前vipid不超过队列大小,并且在这张桌子使用结束前到来。
alloctable(vipid, index);
vipid = findnextvip(vipid);//寻找下一个vip
} else {
alloctable(i, index);//这个队列中没有vip了,那么就顺位。
i++;
}
}
} else {//不是vip桌子。
if(player[i].vip == false) {
alloctable(i, index);
i++;
} else {//如果当前是vip客户,但不是vip桌子
int vipindex = -, minvipendtime = ;
for(int j = ; j <= k; j++) {//遍历所有桌子,找出使用距离最近的桌子
if(table[j].vip == true && table[j].end < minvipendtime) {
minvipendtime = table[j].end;
vipindex = j;
}
}
if(vipindex != - && player[i].arrive >= table[vipindex].end) {
alloctable(i, vipindex);
if(vipid == i)
vipid = findnextvip(vipid);//找下一个。
i++;
} else {
alloctable(i, index);
if(vipid == i)
vipid = findnextvip(vipid);
i++;
}
}
}
}
sort(player.begin(), player.end(), cmp2);
for(i = ; i < player.size() && player[i].start < * ; i++) {
printf("%02d:%02d:%02d ", player[i].arrive / , player[i].arrive % / , player[i].arrive % );
printf("%02d:%02d:%02d ", player[i].start / , player[i].start % / , player[i].start % );
printf("%.0f\n", round((player[i].start - player[i].arrive) / 60.0));
}
for(int i = ; i <= k; i++) {
if(i != )
printf(" ");
printf("%d", table[i].num);
}
return ;
}
//这题真的太难了,看晕了,暂时放弃。
2018-11-22更————
今天又看了以下,还是没发现问题,不知道问题出在哪里,...太难了这道,暂时放弃。
PAT 1026 Table Tennis[比较难]的更多相关文章
- PAT 1026. Table Tennis
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For ...
- 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 a ...
- PAT甲级1026. Table Tennis
PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...
- PAT 甲级 1026 Table Tennis(模拟)
1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...
- PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)
1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are ...
- 1026. Table Tennis (30)
题目如下: A table tennis club has N tables available to the public. The tables are numbered from 1 to N. ...
- PAT A1026 Table Tennis (30 分)——队列
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...
- 1026 Table Tennis (30)(30 分)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...
- 1026 Table Tennis (30分)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...
随机推荐
- 使用IBM SVC构建vSphere存储间集群
使用IBM SVC构建vSphere存储间集群 本文目的 本文描述利用IBM SVC来构建Vsphere 存储间集群 解决方案 什么是vMSC? vShpere存储间集群是一个针对VmwarevSpe ...
- 日记整理---->2017-05-17
起始时间是2017-05-17,记录一下spring的学习过程.陌生人可以变成熟人,但熟人一旦变成陌生人,就再也回不去了. 一.测试一下init-method和depend-on huhx.xml文件 ...
- Android 屏幕适配:最全面的解决方案
转自:https://www.jianshu.com/p/ec5a1a30694b 前言 Android的屏幕适配一直以来都在折磨着我们Android开发者,本文将结合: Google的官方权威适配文 ...
- 解决Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
解决Unknown error: to the dx.jar the SDK folder!最近渐渐迁移到Android Studio来了,更新过Android SDK Manager里的东西后,打开 ...
- vue2.0笔记《一》列表渲染
内容中包含 base64string 图片造成字符过多,拒绝显示
- Mac下门罗币矿工样本分析
背景 今天遇到一个JSONRPC的告警,怀疑挖矿木马,IOC是132.148.245.101,无其他信息,随即google一波. 查询网络 遇到了,主动下载样本分析,下载地址:http://rjj.q ...
- Linux系统java环境jdk的安装
在linux环境中jdk的安装有两种方式,一为rpm安装机制,另一种为源码安装(已编译好)因此在ORACLE官网提供两种安装文件,一为rpm格式,另一种为gz格式,两种的安装方式都大同小异的. 1.r ...
- iOS自带分享
NSArray *activityItems; if (self.sharingImage != nil) { activityItems = @[self.sharingText, self.sha ...
- Python3中关于下划线变量和命名的总结
变量 #!-*-coding:utf-8-*- #__author__ = 'ecaoyng' # # 变量 #_xxx,单下划线开头的变量,标明是一个受保护(protected)的变量,原则上不允许 ...
- Java工程师之SpringBoot系列教程前言&目录
前言 与时俱进是每一个程序员都应该有的意识,当一个Java程序员在当代步遍布的时候,你就行该想到我能多学点什么.可观的是后端的框架是稳定的,它们能够维持更久的时间在应用中,而不用担心技术的更新换代.但 ...