hdu 3645 Code Management System
http://acm.hdu.edu.cn/showproblem.php?pid=3645
一道单纯的模拟,不过要非常仔细的读题,否则必然会有各种各样的错误!区域赛的模拟题就是有难度,差点又卡我一个晚上了。。。。囧!
给点自己debug用的数据:
Sample
Sample in: 2 Alice 1 3 [2010/07/18 12:00:00] SYNC [2010/07/18 12:01:00] MODIFY 1 [2010/07/18 12:05:00] SUBMIT Bob 2 3 [2010/07/18 12:00:01] SYNC [2010/07/18 12:01:30] MODIFY 1 [2010/07/18 12:06:00] SUBMIT 2 Alice 1 3 [2010/07/18 12:05:01] SYNC [2010/07/18 12:05:30] MODIFY 1 [2010/07/18 12:06:00] SUBMIT Bob 2 3 [2010/07/18 12:00:01] SYNC [2010/07/18 12:01:30] MODIFY 1 [2010/07/18 12:05:00] SUBMIT 1 Alice 1 8 [2010/07/18 12:00:00] SYNC [2010/07/18 12:01:00] MODIFY 1 [2010/07/18 12:04:00] SUBMIT [2010/07/18 12:05:01] MODIFY 1 [2010/07/18 12:05:02] MODIFY 2 [2010/07/18 12:06:00] SUBMIT [2010/07/18 12:06:01] MODIFY 1 [2010/07/18 12:07:00] SUBMIT 2 Alice 10 2 [2010/07/18 12:10:00] SYNC [2010/07/18 12:11:00] MODIFY 1 Bob 2 3 [2010/07/18 12:05:01] SYNC [2010/07/18 12:06:30] MODIFY 1 [2010/07/18 12:07:00] SUBMIT 2 Alice 10 3 [2010/07/18 12:01:00] SYNC [2010/07/18 12:04:00] SUBMIT [2010/07/18 12:12:00] SUBMIT Bob 2 3 [2010/07/18 12:05:01] SYNC [2010/07/18 12:06:00] SUBMIT [2010/07/18 12:06:30] MODIFY 1 0 Sample out: 1 [2010/07/18 12:06:00] BY Bob END 1 [2010/07/18 12:06:00] BY Alice END 1 [2010/07/18 12:07:00] BY Alice 2 [2010/07/18 12:06:00] BY Alice END 1 [2010/07/18 12:07:00] BY Bob END END
总结一下容易错的位置:
1、题目要求的是获得代码要在上一个提交接受以后,也就是说同一个人连续提交两次,后面的一次会被忽略。
2、要注意如果修改了没有提交,那个修改编号的内容不应该输出。
3、提交了一次以后,记得要将修改队列清空。
代码如下:
View Code
1 /* 2 Auther: Lyon 3 Problem: hdu 3645 4 */ 5 6 #include <cstdio> 7 #include <cstring> 8 #include <cassert> 9 #include <algorithm> 10 #include <map> 11 #include <string> 12 #include <vector> 13 14 using namespace std; 15 typedef vector<int> vi; 16 17 map<string, int> pos; 18 map<int, int> line; 19 const char *resetTime = "[0000/00/00 00:00:00]"; 20 int ttEvent, ttLine; 21 22 struct Line{ 23 char timeMark[30]; 24 int line; 25 int id; 26 }lineInform[50001]; 27 28 struct Author{ 29 char name[35]; 30 int pri; 31 char syncTime[30]; 32 vi modify; 33 }author[10001]; 34 35 struct Event{ 36 char timeMark[30]; 37 int op; 38 int id; 39 }event[50001]; 40 41 bool cmpEvent(const Event a, const Event b){ 42 return strcmp(a.timeMark, b.timeMark) < 0; 43 } 44 45 bool cmpLine(const Line a, const Line b){ 46 return a.line < b.line; 47 } 48 49 void pre(int n){ 50 int k; 51 char tm1[30], tm2[15], op[10]; 52 53 ttEvent = ttLine = 0; 54 pos.clear(); 55 line.clear(); 56 57 for (int i = 0; i < n; i++){ 58 scanf("%s%d%d", author[i].name, &author[i].pri, &k); 59 pos[author[i].name] = i; 60 author[i].modify.clear(); 61 while (k--){ 62 scanf("%s%s%s", tm1, tm2, op); 63 strcat(tm1, " "); 64 strcat(tm1, tm2); 65 strcpy(event[ttEvent].timeMark, tm1); 66 if (!strcmp(op, "SYNC")){ 67 event[ttEvent].op = -1; 68 } 69 else if (!strcmp(op, "SUBMIT")){ 70 event[ttEvent].op = -2; 71 } 72 else{ 73 assert(!strcmp(op, "MODIFY")); 74 scanf("%d", &event[ttEvent].op); 75 if (!line.count(event[ttEvent].op)){ 76 line[event[ttEvent].op] = ttLine; 77 strcpy(lineInform[ttLine].timeMark, resetTime); 78 lineInform[ttLine++].line = event[ttEvent].op; 79 } 80 } 81 event[ttEvent++].id = i; 82 } 83 } 84 sort(event, event + ttEvent, cmpEvent); 85 /* 86 puts("Debug:"); 87 for (int i = 0; i < ttEvent; i++){ 88 printf("%s %d %d\n", event[i].timeMark, event[i].id, event[i].op); 89 } 90 puts("End Debug"); 91 puts(""); 92 */ 93 } 94 95 void run(){ 96 for (int i = 0; i < ttEvent; i++){ 97 switch (event[i].op){ 98 case -2: 99 { 100 int id = event[i].id; 101 int pri = author[id].pri; 102 103 for (vi::iterator ii = author[id].modify.begin(); ii != author[id].modify.end(); ii++){ 104 int linePos = line[*ii]; 105 int lineID = lineInform[linePos].id; 106 107 //printf("cmp %s %s\n", lineInform[linePos].timeMark, author[id].syncTime); 108 if (strcmp(lineInform[linePos].timeMark, author[id].syncTime) < 0){ 109 strcpy(lineInform[linePos].timeMark, event[i].timeMark); 110 lineInform[linePos].id = id; 111 } 112 else if (author[lineID].pri < pri){ 113 strcpy(lineInform[linePos].timeMark, event[i].timeMark); 114 lineInform[linePos].id = id; 115 } 116 } 117 strcpy(author[id].syncTime, event[i].timeMark); 118 author[id].modify.clear(); 119 } 120 break; 121 case -1: 122 { 123 strcpy(author[event[i].id].syncTime, event[i].timeMark); 124 author[event[i].id].modify.clear(); 125 }break; 126 default: 127 { 128 author[event[i].id].modify.push_back(event[i].op); 129 } 130 131 } 132 } 133 sort(lineInform, lineInform + ttLine, cmpLine); 134 } 135 136 void print(){ 137 for (int i = 0; i < ttLine; i++){ 138 if (strcmp(lineInform[i].timeMark, resetTime)) printf("%d %s BY %s\n", lineInform[i].line, lineInform[i].timeMark, author[lineInform[i].id].name); 139 } 140 puts("END"); 141 } 142 143 int main(){ 144 int n; 145 146 //freopen("in", "r", stdin); 147 while (~scanf("%d", &n) && n){ 148 pre(n); 149 run(); 150 print(); 151 } 152 153 return 0; 154 }
——written by Lyon