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/archive/2010/11/14/1877010.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值