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

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

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

#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. Big research problems (1)

    1. how to measure the uncertainty of prediction model or data analysis? 2.

  2. 2020年国外PhD申请QQ群907928541

    2020年申请国外读博的 可以加QQ群:907928541 供大家学习交流套磁!

  3. AcWing 896. 最长上升子序列 II

    #include<iostream> #include<algorithm> #include<vector> using namespace std; int m ...

  4. Lose it!

    You are given an array aa consisting of nn integers. Each aiai is one of the six following numbers:  ...

  5. HTML5学习(2)语义化

    什么是语义化? 1.每一个HTML元素都有具体的含义,例: a元素:超链接,p元素:段落 2.所有的元素与展示效果无关 元素内容展示到页面中的效果,应该由CSS决定. 因为浏览器带有默认的CSS样式, ...

  6. ubuntu 终端快捷方式汇总

    terminal 是一个命令行终端,将启动系统默认的shell,shell是一个解释并执行在命令行提示符输入的命令的程序. 启动 terminal1 在 “面板主页” 的应用程序搜索栏中,输入命令gn ...

  7. python调用c/c++ (入参出参为指针)

    python可以使用ctypes库调用c++编译的so库函数 0x01  c/c++编译为so库文件 编译C文件 gcc -o libpycallfoo.so -shared -fPIC rsa.c  ...

  8. 【做题笔记】P2871 [USACO07DEC]手链Charm Bracelet

    就是 01 背包.大意:给您 \(T\) 个空间大小的限制,有 \(M\) 个物品,第 \(i\) 件物品的重量为 \(c_i\) ,价值为 \(w_i\) .要求挑选一些物品,使得总空间不超过 \( ...

  9. hadoop3 配置second name node

    设置hdfs-site.xml <property> <name>dfs.http.address</name> <value>192.168.1.81 ...

  10. LNMP调优

    1.编译安装nginx前修改: 在安装包目录下  vim src/core/nginx.h //#号不代表注释 #define nginx_version      1009009 //软件版本号 # ...