zoukankan      html  css  js  c++  java
  • UVa 12100 Printer Queue(queue或者vector模拟队列)

    The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output.

    Because some jobs are more important than others, the Hacker General has invented and implemented a simple priority system for the print job queue. Now, each job is assigned a priority between 1 and 9 (with 9 being the highest priority,
    and 1 being the lowest), and the printer operates as follows.

    The first job J in queue is taken from the queue.
    If there is some job in the queue with a higher priority than job J, thenmove J to the end of the queue without printing it.
    Otherwise, print job J (and do not put it back in the queue).

    In this way, all those importantmuffin recipes that the Hacker General is printing get printed very quickly. Of course, those annoying term papers that others are printing may have to wait for quite some time to get printed, but that's life.

    Your problem with the new policy is that it has become quite tricky to determine when your print job will actually be completed. You decide to write a program to figure this out. The program will be given the current queue (as a list of priorities) as well as the position of your job in the queue, and must then calculate how long it will take until your job is printed, assuming that no additional jobs will be added to the queue. To simplifymatters, we assume that printing a job always takes exactly one minute, and that adding and removing jobs from the queue is instantaneous.

    Input
    One line with a positive integer: the number of test cases (at most 100). Then for each test case:
    One line with two integers n and m, where n is the number of jobs in the queue (1 ≤ n ≤ 100) and m is the position of your job (0 ≤ m ≤ n −1). The first position in the queue is number 0, the second is number 1, and so on.
    One linewith n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The first integer gives the priority of the first job, the second integer the priority of the second job, and so on.

    Output
    For each test case, print one line with a single integer; the number of minutes until your job is completely printed, assuming that no additional print jobs will arrive.

    Sample Input
    3
    1 0
    5
    4 2
    1 2 3 4
    6 0
    1 1 9 1 1 1

    Sample Output
    1
    2

    题意

    给你n个任务的优先度(9最大),和需要的第m个位子(0开始),求打印到第m个位子的时间

    打印工作方式:首先从队列头取出1个J,如果队列里有更大的,就把J放到队列最后

             否则打印J

    题解1

    一开始没想到用queue如何做,问题在于怎么判断后面有比他大的

    于是干脆用vector模拟一下队列

    代码1

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef pair<int,int> pi;
     4 int main()
     5 {
     6     //freopen("in.txt","r",stdin);
     7     //freopen("out.txt","w",stdout);
     8     int T,n,m,w;
     9     cin>>T;
    10     while(T--)
    11     {
    12         vector<pi> vec;
    13         cin>>n>>m;
    14         for(int i=0;i<n;i++)
    15         {
    16             cin>>w;
    17             vec.push_back(pi(w,i));
    18         }
    19         int time=0;
    20         while(1)
    21         {
    22             int pr=vec[0].first,pos=vec[0].second,F=1;
    23             for(int i=1;i<vec.size();i++)
    24             {
    25                 if(vec[i].first>pr)
    26                 {
    27                     F=0;break;
    28                 }
    29             }
    30             vec.erase(vec.begin());
    31             if(F)
    32             {
    33                 time++;
    34                 if(pos==m)
    35                     break;
    36             }
    37             else
    38                 vec.push_back(pi(pr,pos));
    39         }
    40         cout<<time<<endl;
    41     }
    42     return 0;
    43 }

    题解2

    后来想了个queue的,开个Cnt[10]数组用来存数字就可以解决了

    代码2

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef pair<int,int> pi;
     4 int main()
     5 {
     6     //freopen("in.txt","r",stdin);
     7     //freopen("out.txt","w",stdout);
     8     int T,n,m,w;
     9     cin>>T;
    10     while(T--)
    11     {
    12         int Cnt[10]={0};
    13         queue<pi> qu;
    14         cin>>n>>m;
    15         for(int i=0;i<n;i++)
    16         {
    17             cin>>w;
    18             Cnt[w]++;
    19             qu.push(pi(w,i));
    20         }
    21         int time=0;
    22         while(1)
    23         {
    24             int pr=qu.front().first,pos=qu.front().second,F=1;
    25             qu.pop();
    26             for(int i=9;i>pr;i--)
    27                 if(Cnt[i])
    28                     F=0;
    29             if(F)
    30             {
    31                 time++;
    32                 Cnt[pr]--;
    33                 if(pos==m)
    34                     break;
    35             }
    36             else
    37                 qu.push(pi(pr,pos));
    38         }
    39         cout<<time<<endl;
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    图像相似度
    二维数组 问题 E: 计算鞍点
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    【转载】2015 Objective-C 三大新特性 | 干货
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8453880.html
Copyright © 2011-2022 走看看