SOJ 1082. MANAGER

题目大意:多组测试样例。对于每组测试样例:第一行的整数为程序的最大代价,第二行的整数为删除数需要输出的序号的总数,第三行是删除数时需要输出的序号。

接着每行是一组操作,"a value"操作表示要往加入代价为value的进程; "p value"表示切换manager的policy为value(1或者2),policy为1,即"r"操作删除代价最小的进程,policy为2,即"r"操作删除代价最大的进程;"e"操作表示停止操作。

解题思路:简单的模拟即可。注意:1.removal list中的值表示第几次的删除需要将被删除的数输出; 2.每两个测试样例之间有空行。

代码如下:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 10005;
 9 int cost[maxn];    // cost[i]表示cost为i的process数
10 
11 void init(const int &n) {
12     for (int i = 1; i <= n; ++i) cost[i] = 0;
13 }
14 
15 int main() {
16     int n;
17     bool first_time = true;
18     while (scanf("%d", &n) != EOF) {
19         if (!first_time) printf("\n");
20         init(n);
21         int policy = 1;
22         int rm_list_size;
23         int rm_list_index = 0;
24         int rm_count = 0;
25         scanf("%d", &rm_list_size);
26         vector<int> rm_list(rm_list_size);
27         for (int i = 0; i < rm_list_size; ++i)
28             scanf("%d", &rm_list[i]);
29         // sort(rm_list.begin(), rm_list.end());
30 
31         string a("a");
32         string p("p");
33         string r("r");
34         string e("e");
35 
36         string op;
37         int operand;
38         while (cin >> op, op != e) {
39             if (op == a) {
40                 scanf("%d", &operand);
41                 ++cost[operand];
42             } else if (op == p) {
43                 scanf("%d", &operand);
44                 policy = operand;
45             } else if (op == r) {
46                 ++rm_count;
47                 if (policy == 1) {    // remove the minimum cost process
48                     for (int i = 1; i <= n; ++i)
49                         if (cost[i] != 0) {
50                             --cost[i];
51                             if (rm_list_index != rm_list.size() && rm_count == rm_list[rm_list_index]) {
52                                 printf("%d\n", i);
53                                 ++rm_list_index;
54                             }
55                             break;
56                         }
57                 } else {    // policy == 2, remove the maximum cost process
58                     for (int i = n; i >= 1; --i)
59                         if (cost[i] != 0) {
60                             --cost[i];
61                             if (rm_list_index != rm_list.size() && rm_count == rm_list[rm_list_index]) {
62                                 printf("%d\n", i);
63                                 ++rm_list_index;
64                             }
65                             break;
66                         }
67                 }
68             }
69         }
70         first_time = false;
71     }
72     return 0;
73 }

 

posted @ 2015-11-17 22:45  MchCyLh  阅读(201)  评论(2编辑  收藏  举报