九度OJ 1326:Waiting in Line(排队) (模拟)
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:220
解决:64
- 题目描述:
-
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:
- The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line
behind the yellow line. - Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
- Customer[i] will take T[i] minutes to have his/her transaction processed.
- The first N customers are assumed to be served at 8:00am.
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.
For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is
served at window1 while customer2 is served at window2. Customer3 will
wait in front of window1 and customer4 will wait in front of window2. Customer5 will
wait behind the yellow line.At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will
leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10. - The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line
- 输入:
-
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer
queries).The next line contains K positive integers, which are the processing time of the K customers.
The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.
- 输出:
-
For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served
before 17:00, you must output "Sorry" instead.
- 样例输入:
-
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
- 样例输出:
-
08:07
08:06
08:10
17:00
Sorry
思路:
模拟题,排队题当然数据结构用队列比较好。
用C语言太苦逼了,还要自己写一个队列,还是C++比较好,以后要用C++了。
需要注意的是:即使是17点之前开始办业务,17点之后仍然没有办完就是sorry。
代码:
#include <stdio.h>
#include <stdlib.h> #define N 21
#define M 11
#define K 1001 typedef struct node1 {
int proc;
int begin;
} Cust; typedef struct node2 {
Cust *cust[M];
int front;
int rear;
} Queue; int isEmpty(Queue *q)
{
return (q->front == q->rear);
} int isFull(Queue *q)
{
return (q->front == (q->rear+1)%M);
} int countQueue(Queue *q)
{
return (q->rear + M - q->front) % M;
} void push(Queue *q, Cust *val)
{
q->cust[q->rear] = val;
q->rear = (q->rear+1)%M;
} Cust *pop(Queue *q)
{
int front = q->front;
q->front = (q->front+1)%M;
return q->cust[front];
} Cust *top(Queue *q)
{
return q->cust[q->front];
} int main(void)
{
int n, m, k, q, i, j;
Queue *queue[N];
Cust *cust[K];
int query[K];
int maxTime = (17-8)*60; while (scanf("%d%d%d%d", &n, &m, &k, &q) != EOF)
{
for (i=0; i<n; i++)
{
queue[i] = (Queue *)malloc(sizeof(Queue));
queue[i]->front = queue[i]->rear = 0;
}
for (i=0; i<k; i++)
{
cust[i] = (Cust *)malloc(sizeof(Cust));
scanf("%d", &cust[i]->proc);
cust[i]->begin = maxTime+1;
}
for (i=0; i<q; i++)
scanf("%d", &query[i]); // init state
int inQueue = 0;
int outQueue = k;
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
inQueue = i*n + j;
if (inQueue >= k)
break;
if (i == 0)
cust[inQueue]->begin = 0;
push(queue[j], cust[inQueue]);
}
if (inQueue >= k)
break;
}
inQueue = (m*n <= k) ? (inQueue+1) : inQueue;
outQueue = k - inQueue;
//printf("inQueue=%d, outQueue=%d, k=%d\n", inQueue, outQueue, k); // simulate the whole process
int time;
for (time=0; time<=maxTime; time++)
{
for (i=0; i<n; i++)
{
if (isEmpty(queue[i]))
break;
Cust *c = top(queue[i]);
if (c->begin + c->proc == time)
{
pop(queue[i]);
if (outQueue > 0)
{
push(queue[i], cust[k-outQueue]);
outQueue --;
}
if (!isEmpty(queue[i]))
{
top(queue[i])->begin = time;
//printf("i=%d, time=%d\n", i, time);
}
}
}
if (isEmpty(queue[0]))
break;
} for (i=0; i<q; i++)
{
int id = query[i] - 1;
int endTime = cust[id]->begin + cust[id]->proc;
if (endTime > maxTime)
printf("Sorry\n");
else
printf("%02d:%02d\n", 8 + endTime/60, endTime%60);
}
} return 0;
}
/**************************************************************
Problem: 1326
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:916 kb
****************************************************************/
九度OJ 1326:Waiting in Line(排队) (模拟)的更多相关文章
- 九度OJ 1110:小白鼠排队 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1734 解决:1054 题目描述: N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的 ...
- 九度OJ 1068 球半径和数量 (模拟)
题目1068:球的半径和体积 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4797 解决:1696 题目描写叙述: 输入球的中心点和球上某一点的坐标,计算球的半径和体积 输入: 球的中心 ...
- 九度OJ 1067 n的阶乘 (模拟)
题目1067:n的阶乘 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5666 解决:2141 题目描写叙述: 输入一个整数n,输出n的阶乘 输入: 一个整数n(1<=n<=2 ...
- 九度OJ 1183 守形数 (模拟)
题目1183:守形数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2663 解决:1424 题目描写叙述: 守形数是这样一种整数.它的平方的低位部分等于它本身. 比方25的平方是625. ...
- 九度OJ 1355:扑克牌顺子 (模拟)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:1676 解决:484 题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^). ...
- 九度OJ 1334:占座位 (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:864 解决:202 题目描述: sun所在学校的教室座位每天都是可以预占的. 一个人可以去占多个座位,而且一定是要连续的座位,如果占不到他所 ...
- 【九度OJ】题目1124:Digital Roots 解题报告
[九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...
- 【九度OJ】题目1433:FatMouse 解题报告
[九度OJ]题目1433:FatMouse 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1433 题目描述: FatMouse pr ...
- 【九度OJ】题目1439:Least Common Multiple 解题报告
[九度OJ]题目1439:Least Common Multiple 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1439 ...
随机推荐
- sqoop使用记录
sqoop简介 Sqoop是用来实现结构型数据(如关系数据库)和Hadoop之间进行数据迁移的工具.它充分利用了MapReduce的并行特点以批处理的方式加快数据的传输,同时也借助MapReduce实 ...
- 转 : SQL Server数据库优化经验总结
优化数据库的注意事项: 1.关键字段建立索引. 2.使用存储过程,它使SQL变得更加灵活和高效. 3.备份数据库和清除垃圾数据. 4.SQL语句语法的优化.(可以用Sybase的SQL Expert, ...
- 2017.8.4 Creating Server TCP listening socket *:6379: bind: No such file or directory
启动redis时出现如下错误: 解决办法:按顺序输入如下命令就可以连接成功. 1. redis-cli.exe 2. shutdown 3. exit 4. redis-server.exe 参考来 ...
- idea 添加多模块项目
建立多模块工程先建立一个空的项目,File-Project-Maven不勾选create from archetype即可 然后再右键父工程 添加模块jar包 添加模块war包 然后再pom中配置引用 ...
- PS如何使用自定义画笔
1 没有杂色的白背景不用抠图,GIF格式的透明背景不用抠图,有背景但是不想抠图都可以直接定义为画笔.先选中需要定义的画笔(得到选区),然后单击编辑-定义画笔预设. 2 随后就可以找到我们的画笔工具 ...
- hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流
题意: 在一般费用流题目改动:路过某路,每x单位流量须要花费 ai*x^2(ai为给定的系数). 開始的的时候,一看仅仅只是是最后统计费用上在改动罢了,一看例子.发现根本没那么简单(ps:以后每次写程 ...
- 【Excle数据透视表】如何调整压缩形式显示下的缩进字符数
调整前: ...
- java 异常 java.lang.OutOfMemoryError: GC overhead limit exceeded 解决
一.异常如下: Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded ...
- Android开发环境搭建 for windows (linux类似) 详细可参考“文件”中“Android开发环境搭建.pdf ”
ADT-Bundle for Windows 是由Google Android官方提供的集成式IDE,已经包含了Eclipse,你无需再去下载Eclipse,并且里面已集成了插件,它解决了大部分新手通 ...
- C# 中 finally 的用法
当一个异常抛出时,它会改变程序的执行流程.因此不能保证一个语句结束后,它后面的语句一定会执行,在 C# 中这个问题可以用 finally 解决. 为了确保一个语句总是能执行(不管是否抛出异常),需要将 ...