zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    SettingsProvider 它SettingsCache
    14、Cocos2dx 3.0三,找一个小游戏开发Scene and Layer:游戏梦想
    open-flash-chart2各种效果
    Android使用surface直接显示yuv数据(三)
    LeetCode:Populating Next Right Pointers in Each Node
    zoj 3203 Light Bulb,三分之二的基本问题
    POJ 2485 Highways
    Leetcode
    android音乐柱状频谱实现
    Android自定义控件实战——水流波动效果的实现WaveView
  • 原文地址:https://www.cnblogs.com/platero/p/1877010.html
Copyright © 2011-2022 走看看