forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfridgemadness.cpp
More file actions
147 lines (129 loc) · 3.41 KB
/
Copy pathfridgemadness.cpp
File metadata and controls
147 lines (129 loc) · 3.41 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <map>
#include <iterator>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
class engineer {
public:
int id;
int score;
engineer(int _id, int _score) : id(_id), score(_score) {}
bool operator<(const engineer& rhs) const {
if (score != rhs.score) return score > rhs.score;
return id > rhs.id;
}
};
int n2, n, m, p;
vector<vector<int> > dk, rank, rk1, rk2, vv;
deque<int> freeM;
vector<int> engage;
vector<string> split(const string& s, const string& delim =" ") {
vector<string> res;
string t;
REP(i,s.size()) {
if (delim.find(s[i]) != string::npos) {
if (!t.empty()) {
res.push_back(t);
t = "";
}
} else {
t += s[i];
}
}
if (!t.empty()) {
res.push_back(t);
}
return res;
}
vector<int> splitInt( const string& s, const string& delim =" " ) {
vector<string> tok = split(s, delim);
vector<int> res;
REP(i,tok.size()) res.push_back(atoi(tok[i].c_str()));
return res;
}
int getScore(int i1, int i2) {
int ret = 0;
REP(i,dk[i1].size()) {
int d = dk[i1][i];
if (rank[i2][d] == rank[i1][d]) ret += rank[i2][d] * rank[i2][d];
else if (rank[i2][d] < rank[i1][d]) ret += rank[i1][d];
}
return ret;
}
void Gale_Shapley() {
vv.assign(n, vector<int>(n, 0));
freeM.clear();
REP(i,n) freeM.push_back(i);
engage.assign(n2, -1);
while (!freeM.empty()) {
int now = freeM.front();
int idx = -1;
REP(i,n) {
if (vv[now][i] == 1) continue;
if (idx == -1 || (rk1[now][i] < rk1[now][idx])) idx = i;
}
vv[now][idx] = 1;
int cur = engage[idx + n];
if (cur == -1) {
engage[now] = idx + n;
engage[idx + n] = now;
freeM.pop_front();
} else {
if (rk2[idx][now] < rk2[idx][cur]) {
engage[now] = idx + n;
engage[idx + n] = now;
engage[cur] = -1;
freeM.pop_front();
freeM.push_back(cur);
}
}
}
REP(i,n) {
cout << i << " " << engage[i] << endl;
}
}
void run(const char* argv[]) {
ifstream input(argv[1]);
input >> n2 >> m;
n = (n2 >> 1);
string drink;
REP(i,m+1) getline(input, drink);
dk.assign(n2, vector<int>());
rank.assign(n2, vector<int>(m, -1));
REP(i,n2) {
int id;
string td;
input >> id >> td;
dk[id] = splitInt(td, ",");
p = dk[id].size();
REP(j,p) {
rank[id][dk[id][j]] = p - j;
}
}
rk1.assign(n, vector<int>(n));
rk2.assign(n, vector<int>(n));
REP(i,n) {
vector<engineer> eng;
REP(j,n) eng.push_back(engineer(j, getScore(i, j + n)));
sort(eng.begin(), eng.end());
REP(j,n) rk1[i][eng[j].id] = j;
eng.clear();
REP(j,n) eng.push_back(engineer(j, getScore(i + n, j)));
sort(eng.begin(), eng.end());
REP(j,n) rk2[i][eng[j].id] = j;
}
Gale_Shapley();
}
int main(int argc, const char* argv[]) {
run(argv);
}