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, then move 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 important muffin 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 simplify matters, 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 line with 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
5
题目类型:队列模拟
算法分析:用cnt[10]数组表示读入任务的优先级(1~9)出现的个数,然后按照题目的叙述模拟队列,每次通过cnt数组找到优先级最高的优先级maxval,然后出队头一个元素val,比较元素val与maxval的大小,如果val < maxval,则直接将val压入队尾。反之,则时间自加1,判断val是否是题目要求输出的任务,如果是则退出程序,返回结果。否则将cnt[maxval]--并更新maxval,下面第一份代码是更好的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
/************************************************** filename :g.cpp author :maksyuki created time :2018/1/23 16:51:13 last modified :2018/1/23 17:08:29 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; PII que[maxn]; int cnt[16], qf, qb; void init() { qf = qb = 1; for(int i = 0; i < 16; i++) cnt[i] = 0; } int main() { #ifdef LOCAL CFF; //CFO; #endif int t; scanf("%d", &t); while(t--) { init(); int n, m; scanf("%d %d", &n, &m); m++; int tmp; for(int i = 1; i <= n; i++) { scanf(" %d", &tmp); cnt[tmp]++; if(i == m) que[qb++] = PII(tmp, 1); else que[qb++] = PII(tmp, 0); } qb--; int tim = 0; while(qf <= qb) { PII vv = que[qf++]; cnt[vv.first]--; int lev = -1; for(int i = 9; i >= 1; i--) if(cnt[i]) { lev = i; break; } if(vv.first < lev) { que[++qb] = vv; cnt[vv.first]++; } else { tim++; if(vv.second == 1) break; } } printf("%d\n", tim); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#include <iostream> #include <fstream> #include <cstring> #include <queue> using namespace std; int cnt[12]; int maxval; int sum; struct point { int val; bool output; point (int AA, bool BB) : val (AA), output (BB) {} }; int SearchMaxVal () { int i; for (i = 9; i >= 1; i--) if (cnt[i]) return i; return -1; //error } int main() { // ifstream cin ("aaa.txt"); int cases; cin >> cases; while (cases--) { memset (cnt, 0, sizeof (cnt)); queue <point> ans; int num, weizhi; cin >> num >> weizhi; int i, temp; for (i = 0; i < num; i++) { cin >> temp; cnt[temp]++; if (i == weizhi) ans.push (point (temp, true)); else ans.push (point (temp, false)); } maxval = SearchMaxVal (); sum = 0; while (1) { if (!ans.empty ()) { point temp = ans.front (); if (temp.val < maxval) { ans.pop (); ans.push (point (temp.val, temp.output)); } else { sum++; if (temp.output == true) break; else { ans.pop (); cnt[temp.val]--; maxval = SearchMaxVal (); } } } } cout << sum << endl; } return 0; } |
- « 上一篇:uva11988
- uva12412:下一篇 »