这题写了有一点时间,最开始想着优化一下时间,用优先队列去做,但是发现有锅,因为忽略了队的长度。

然后思考过后,觉得用时间线来模拟最好做,先把窗口前的队列填满,这样保证了队列的长度是统一的,这样的话如果到某个时间,队首的人已经服务完了,这样这个队列的长度就减少一,这就变成了所有队列中长度最短的队列,所以直接向这个队列里面添加一个人就可以了。

按照从小到大轮询窗口的话,这样正好符合题中的要求,就是队列长度相同短,先选窗口序号小的窗口。如果按照这种策略,少一个就补一个的策略,队列长度会一直保持相等。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1010; int ans[maxn],a[maxn],N,M,K,Q,t[maxn];
queue<int> q[22];
int pret[22]; void print(int t)
{
if (t==-1) {
printf("Sorry\n");
return;
} t+=480;
int hh=t/60,mm=t%60;
// printf("%d %d ",hh,mm);
if (hh>17) {
printf("Sorry\n");
}
else {
printf("%02d:%02d\n",hh,mm);
}
} int main()
{
// freopen("in.txt","r",stdin);
scanf("%d%d%d%d",&N,&M,&K,&Q);
for (int i=1;i<=K;i++) {
scanf("%d",&t[i]);
}
for (int i=1;i<=Q;i++) {
scanf("%d",&a[i]);
}
int c=1;
for (int i=1;i<=M;i++) {
for (int j=1;j<=N;j++) {
q[j].push(c++);
if (c>K) {
break;
}
}
}
memset(ans,-1,sizeof(ans));
for (int i=0;i<=540;i++) {
if (c>K) break;
for (int j=1;j<=N;j++) {
int p=q[j].front();
int tt=t[p];
if (i-pret[j]==tt) {
ans[p]=i;
q[j].pop();
q[j].push(c++);
pret[j]=i;
if (c>K) {
goto outloop;
}
}
}
}
outloop:
for (int j=1;j<=N;j++) {
while (!q[j].empty()) {
if (pret[j]>=540) {
break;
}
int p=q[j].front();
ans[p]=pret[j]+t[p];
pret[j]+=t[p];
q[j].pop(); }
}
for (int i=1;i<=Q;i++) {
print(ans[a[i]]);
} return 0;
}

PAT 1014 Waiting in Line (30分) 一个简单的思路的更多相关文章

  1. PAT 甲级 1014 Waiting in Line (30 分)(queue的使用,模拟题,有个大坑)

    1014 Waiting in Line (30 分)   Suppose a bank has N windows open for service. There is a yellow line ...

  2. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  3. 【PAT甲级】1014 Waiting in Line (30 分)(队列维护)

    题面: 输入四个正整数N,M,K,Q(N<=20,M<=10,K,Q<=1000),N为银行窗口数量,M为黄线内最大人数,K为需要服务的人数,Q为查询次数.输入K个正整数,分别代表每 ...

  4. PTA 1014 Waiting in Line (30分) 解题思路及满分代码

    题目 Suppose a bank has N windows open for service. There is a yellow line in front of the windows whi ...

  5. 1014 Waiting in Line (30 分)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  6. PAT 1014 Waiting in Line (模拟)

    1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  7. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  8. PAT A 1014. Waiting in Line (30)【队列模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1014 思路: 直接模拟类的题. 线内的各个窗口各为一个队,线外的为一个,按时间模拟出队.入队. 注 ...

  9. 1014 Waiting in Line (30)(30 分)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

随机推荐

  1. 一次m2eclipse的安装大坑经历之http://m2eclipse.sonatype.org/sites/m2e

    m2eclipse 插件的安装在<Maven 实战>这本书上是这么说的: ”由于Eclipse默认没有集成对Maven的支持,幸运的是由Maven之父Jason Van Zyl创立的Son ...

  2. 题解【洛谷P1618】 三连击(升级版)

    设三个数分别为n1.n2.n3,因为三个数的比为A:B:C,取一份量i,使得A·i=x,B·i=y,C·i=z(·是*的意思). 所以我们的代码只需要枚举i,并以此判断n1.n2.n3是否为三位数且包 ...

  3. c#中的强类型、弱类型和泛型

    强类型和弱类型的变量都有两个属性:类型和值. 强类型的变量类型是不能改变的,弱类型的变量类型是随需改变的,这是强弱的真正含义. 我们在编写c#代码时,变量类型是明确的,不可更改的,如string就是s ...

  4. Motif

    Motif discovery is in loose terms the problem of finding interesting patterns in sequences. motif: i ...

  5. Qt- 图形界面应用程序的运行模式

    main() 定义主窗口 ————>fd = DefineMainWindow() 创建主窗口————>win = CreateMainWindow() 创建主窗口中的元素-----> ...

  6. 12c的PDB创建DIRECTORY要注意与PATH_PREFIX的关系(ORA-65254)

    在创建PDB过程中如果使用了带PATH_PREFIX的参数,意味着在创建DIRECTORY目录时需要指定相对路径,而不能指定其它绝对路径.来自博客园AskScuti 11g整库作为一个PDB迁移至阿里 ...

  7. 家庭记账小账本Android studio

    经过长达7天多的时间,从Android studio的安装碰到很多的问题,真的很不走运,别人安装Android studio都是一气呵成,我的Android真的没话说 把自己的这些天的开发成果展示一下 ...

  8. Linux - Shell - find - 进阶: 范围

    概述 继续昨天的 find 背景 还有一些 过滤条件 1. 约束: 目录层数 概述 约束目录的层级 选项 -maxdepth 作用 约束最大目录层级 相对路径 -mindepth 作用 约束最小目录层 ...

  9. Ehab and a Special Coloring Problem

    You're given an integer nn. For every integer ii from 22 to nn, assign a positive integer aiai such ...

  10. testng的prioriy

    todo: 同一个class中的priority: 1.不标priority的case和标注priority的case,谁先谁后? 2.标注相同priority的case,谁先谁后?是不是并发? 3. ...