forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
102 lines (88 loc) · 2.77 KB
/
C.cpp
File metadata and controls
102 lines (88 loc) · 2.77 KB
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(X) ((int) ((X).size()))
using namespace std;
map<string,int> id;
string name[111];
int getId(string s) {
if (!id.count(s)) {
int cur = SZ(id);
id[s] = cur + 1;
name[cur + 1] = s;
}
return id[s];
}
map<int, int> art[111];
vector< pair<int,int> > need[55];
char tmp[1000111];
bool canComp(int ally, int comp) {
REP(j,SZ(need[comp])) {
pair<int,int> p = need[comp][j];
if (art[ally][p.first] < p.second) return false;
}
return true;
}
int main() {
ios :: sync_with_stdio(false);
int nAlly, nBasic, nComp, nPurchase;
while (scanf("%d%d%d%d\n", &nAlly, &nBasic, &nComp, &nPurchase) == 4) {
FOR(i,1,nAlly) art[i].clear();
id.clear();
FOR(i,1,nComp) need[i].clear();
FOR(i,1,nBasic) {
gets(tmp); string s(tmp);
int u = getId(s);
}
FOR(i,1,nComp) {
gets(tmp);
REP(i,strlen(tmp))
if (tmp[i] == ':' || tmp[i] == ',')
tmp[i] = ' ';
stringstream ss(tmp);
string cur; ss >> cur;
int u = getId(cur);
string t;
int cnt;
while (ss >> t >> cnt) {
need[i].push_back(make_pair(getId(t), cnt));
}
}
FOR(i,1,nPurchase) {
int u;
scanf("%d %s\n", &u, &tmp[0]);
string s(tmp);
int v = getId(s);
art[u][v] += 1;
FOR(j,1,nComp)
if (canComp(u, j)) {
for(auto p : need[j]) {
art[u][p.first] -= p.second;
}
art[u][j + nBasic] += 1;
break;
}
}
FOR(i,1,nAlly) {
int cnt = 0;
for(auto p : art[i])
if (p.second > 0)
++cnt;
cout << cnt << '\n';
vector< pair<string,int> > res;
for(auto p : art[i])
if (p.second > 0) {
res.push_back(make_pair(name[p.first], p.second));
}
sort(res.begin(), res.end());
for(auto p : res) cout << p.first << ' ' << p.second << '\n';
}
}
}