Sicily 1443 Printer Queue

题目地址: http://sicily.3322.org/show_problem.php?pid=1443

一开始认为题目有数学规律, 考虑了一会儿发现此路不通, 于是直接模拟队列. 值得注意的是需要记录当前最大的权重是几. 为了记录目标工作, 在初始时用tar_val记录了目标工作的权重, 同时在队列中将目标工作的权重记为0.  另外, 还使用了数组weight_cnt来记录各个权重值当前的工作数目有多少. 队列采用数组模拟, front和rear分别为队首队尾指针. 由于工作数为最大为100, 考虑最坏情况, 数组最大长度 < 99!, 方便起见开了个10000的数组. 用c写的, 最后排进了前10, 嘿嘿~

不知道为啥codeblocks突然不能对单个文件调试了, 之前都可以. 建立工程的话还是可以调试的.

代码
#include <stdio.h>
#define MAXLEN 10000

int main()
{
int t, n, m, i; /* t-target, n-number of jobs, m-position of the target job*/
int tar_val; /* target value */
int job[MAXLEN]; /* job queue */
int weight_cnt[10]; /* number of jobs of specified weight */
int cur_max; /* current max weight */
int front, rear; /* front and rear of the job queue */
int time_cnt; /* the number of minutes until your job is completely printed*/

scanf(
"%d", &t);
while(t--)
{
/* initiate */
front
= rear = 0;
cur_max
= 1;
time_cnt
= 0;
for(i = 0; i < 10; i++)
weight_cnt[i]
= 0;

/* input */
scanf(
"%d%d", &n, &m);
for(i = 0; i < n; i++)
{
scanf(
"%d", &job[i]);

if(job[i] > cur_max)
cur_max
= job[i];

weight_cnt[job[i]]
++;
rear
++;
}

tar_val
= job[m];
job[m]
= 0;

while(front < rear)
{
if(job[front] == 0)
{
if(tar_val == cur_max)
{

time_cnt
++;
break;
}
else
{
job[rear
++] = job[front++];
}
}
else
{
if(job[front] == cur_max)
{
time_cnt
++;
weight_cnt[cur_max]
--;
while(weight_cnt[cur_max]==0)
cur_max
--;
}
else
{
job[rear
++] = job[front];
}
front
++;
}

/*printf("cur queue:");
for(i = front; i < rear; i++)
printf("%d ", job[i]);
printf("\n");
*/
}
printf(
"%d\n", time_cnt);
}
return 0;
}

原文地址:https://www.cnblogs.com/platero/p/1877010.html