-
Notifications
You must be signed in to change notification settings - Fork 6
/
10145.cpp
107 lines (96 loc) · 2.25 KB
/
10145.cpp
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
//Steven Kester Yuwono - UVa 10145
#include <cstdio>
#include <map>
#include <set>
using namespace std;
set<int> blocked; // transaction ID
map<int,int> lockedX; // item ID , transaction ID
map<int,set<int> > lockedS; // itemID, list of transactionID
map<int,set<int> >::iterator itS; //iterator for lockedS
set<int>::iterator itB; //iterator Blocked
map<int,int>::iterator itX; //iterator for lockedX
char buffer[10000000];
int main() {
gets(buffer);
int tc;
sscanf(buffer,"%d",&tc);
for(int d=0;d<tc;d++){
blocked.clear();
lockedX.clear();
lockedS.clear();
if(d!=0){
printf("\n");
}
while (gets(buffer)&&(buffer[0]!='#')){
if(buffer[0] =='\0'){
continue;
}
int trans,item;
char cmd = buffer[0];
sscanf(buffer+2,"%d %d",&trans,&item);
// printf("%c %d %d\n",cmd,trans,item);
itB = blocked.find(trans);
if(itB != blocked.end()){
printf("IGNORED\n");
continue;
}
if(cmd == 'S'){
// check whether item is found in lock X
itX = lockedX.find(item);
// if not found, granted
if(itX == lockedX.end()) {
itS = lockedS.find(item);
if(itS == lockedS.end()){
set<int> tempSet;
tempSet.insert(trans);
lockedS.insert(pair<int,set<int> >(item,tempSet));
}
else {
(itS->second).insert(trans);
}
printf("GRANTED\n");
}
//if found, depends
else {
if(itX->second != trans){
blocked.insert(trans);
printf("DENIED\n");
}
else {
printf("GRANTED\n");
}
}
}
else if(cmd == 'X'){
int grantedFlag = true;
// check whether itemID is found in X lock
itX = lockedX.find(item);
if(itX != lockedX.end()){
if(itX ->second != trans){
blocked.insert(trans);
printf("DENIED\n");
grantedFlag = false;
continue;
}
}
// check whether itemID is found in S lock
itS = lockedS.find(item);
if(itS != lockedS.end()) {
set<int>::iterator it;
for(it = itS->second.begin();it!=itS->second.end();it++){
if(*it != trans){
blocked.insert(trans);
printf("DENIED\n");
grantedFlag = false;
break;
}
}
}
if(grantedFlag){
lockedX.insert(pair<int,int>(item, trans));
printf("GRANTED\n");
}
}
}
}
}