QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#54628 | #1364. Condorcet | not_so_organic# | AC ✓ | 1265ms | 3868kb | C++23 | 5.0kb | 2022-10-09 21:29:52 | 2022-10-09 21:29:53 |
Judging History
answer
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
#include <string>
using namespace std;
typedef long double DOUBLE;
typedef vector<DOUBLE> VD;
typedef vector<VD> VVD;
typedef vector<int> VI;
typedef vector<string> VS;
const DOUBLE EPS = 1e-9;
struct LPSolver {
int m, n;
VI B, N;
VVD D;
LPSolver(const VVD &A, const VD &b, const VD &c) :
m(b.size()), n(c.size()), N(n + 1), B(m), D(m + 2, VD(n + 2)) {
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) D[i][j] = A[i][j];
for (int i = 0; i < m; i++) { B[i] = n + i; D[i][n] = -1; D[i][n + 1] = b[i]; }
for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; }
N[n] = -1; D[m + 1][n] = 1;
}
void Pivot(int r, int s) {
for (int i = 0; i < m + 2; i++) if (i != r)
for (int j = 0; j < n + 2; j++) if (j != s)
D[i][j] -= D[r][j] * D[i][s] / D[r][s];
for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] /= D[r][s];
for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] /= -D[r][s];
D[r][s] = 1.0 / D[r][s];
swap(B[r], N[s]);
}
bool Simplex(int phase) {
int x = phase == 1 ? m + 1 : m;
while (true) {
int s = -1;
for (int j = 0; j <= n; j++) {
if (phase == 2 && N[j] == -1) continue;
if (s == -1 || D[x][j] < D[x][s] || (D[x][j] == D[x][s] && N[j] < N[s])) s = j;
}
if (D[x][s] > -EPS) return true;
int r = -1;
for (int i = 0; i < m; i++) {
if (D[i][s] < EPS) continue;
if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] ||
((D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r])) r = i;
}
if (r == -1) return false;
Pivot(r, s);
}
}
DOUBLE Solve(VD &x) {
int r = 0;
for (int i = 1; i < m; i++) if (D[i][n + 1] < D[r][n + 1]) r = i;
if (D[r][n + 1] < -EPS) {
Pivot(r, n);
if (!Simplex(1) || D[m + 1][n + 1] < -EPS) return -numeric_limits<DOUBLE>::infinity();
for (int i = 0; i < m; i++) if (B[i] == -1) {
int s = -1;
for (int j = 0; j <= n; j++)
if (s == -1 || D[i][j] < D[i][s] || (D[i][j] == D[i][s] && N[j] < N[s])) s = j;
Pivot(i, s);
}
}
if (!Simplex(2)) return numeric_limits<DOUBLE>::infinity();
x = VD(n);
for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n + 1];
return D[m][n + 1];
}
};
VI fact {1, 1, 2, 6, 24, 120, 720};
VS perms;
void perms_gen(int n) {
string perm = "";
for (int i = 0; i < n; i++) perm += 'A' + i;
do {
perms.push_back(perm);
} while (next_permutation(perm.begin(), perm.end()));
}
VVD adj;
void adj_gen(int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
adj.push_back(VD());
for (int k = 0; k < perms.size(); k++) {
if (perms[k].find('A' + i) < perms[k].find('A' + j)) {
adj.back().push_back(1);
} else {
adj.back().push_back(-1);
}
}
}
}
}
VVD graph;
VD parents;
void rec_graph_gen(int n, int cur) {
if (cur == n) {
VD subgraph;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (parents[i] == j) {
subgraph.push_back(1);
} else if (parents[j] == i) {
subgraph.push_back(-1);
} else {
subgraph.push_back(0);
}
}
}
graph.push_back(subgraph);
} else {
for (int i = 0; i < n; i++) {
if (i == cur) continue;
if (i < cur && parents[i] == cur) continue;
parents.push_back(i);
rec_graph_gen(n, cur + 1);
parents.pop_back();
}
}
}
void graph_gen(int n) {
rec_graph_gen(n, 0);
}
int main() {
int N, K;
cin >> N >> K;
perms_gen(N);
adj_gen(N);
graph_gen(N);
int dim = fact[N];
VD in(dim, 0), x(dim), C(dim, -1);
string ballot;
int count;
for (size_t i = 0; i < K; i++) {
cin >> ballot >> count;
auto it = find(perms.begin(), perms.end(), ballot);
in[it - perms.begin()] += count;
}
DOUBLE best = 999999999;
for (size_t probe = 0; probe <= dim; probe++) {
if (probe < dim) in[probe]++;
for (size_t i = 0; i < graph.size(); i++) {
VVD A(N);
VD B(N, -1);
int cur = 0;
for (size_t j = 0; j < graph[i].size(); j++) {
if (graph[i][j] != 0) {
A[cur] = adj[j];
for (size_t k = 0; k < dim; k++) {
A[cur][k] *= graph[i][j];
B[cur] -= A[cur][k] * in[k];
}
cur++;
}
}
LPSolver lp(A, B, C);
DOUBLE ans = -lp.Solve(x);
if (probe < dim) ans++;
bool integral = true;
for (size_t j = 0; j < dim; j++) {
if (abs(round(x[j]) - x[j]) > 1e-2) integral = false;
}
if (integral) best = min(best, ans);
}
if (probe < dim) in[probe]--;
}
cout << lround(best) << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3688kb
input:
3 3 ABC 1 BCA 1 CAB 1
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 4ms
memory: 3576kb
input:
3 6 CAB 35 ACB 142 ABC 34 BCA 66 CBA 53 BAC 44
output:
49
result:
ok single line: '49'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
3 6 BCA 41 ABC 103 ACB 49 BAC 79 CAB 52 CBA 57
output:
66
result:
ok single line: '66'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
3 6 CBA 49 BAC 50 ABC 64 ACB 98 BCA 35 CAB 70
output:
69
result:
ok single line: '69'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
3 5 BCA 102 BAC 89 ACB 77 CAB 43 ABC 69
output:
91
result:
ok single line: '91'
Test #6:
score: 0
Accepted
time: 1265ms
memory: 3724kb
input:
5 120 DEABC 125 DACBE 207 DABCE 167 BCDEA 186 BCEDA 118 AEBDC 175 CDBEA 252 CDEBA 208 ABCED 131 ADEBC 114 ABECD 132 CDAEB 365 BEADC 162 BEDAC 169 ECDBA 242 ECDAB 137 CEADB 215 EBDAC 179 EBADC 222 DABEC 248 EDBAC 177 DBEAC 207 EDACB 157 ECABD 67 DCBEA 143 BDECA 174 DACEB 195 AECDB 242 ABCDE 156 CABDE...
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3624kb
input:
4 22 CDAB 27 DABC 114 CADB 126 BCDA 51 CABD 58 BACD 16 DACB 87 DCAB 25 BADC 88 CBDA 31 ACBD 82 BCAD 45 CBAD 58 ADCB 26 DBCA 20 DBAC 85 ABCD 27 ADBC 28 BDAC 17 DCBA 44 ABDC 60 ACDB 11
output:
125
result:
ok single line: '125'
Test #8:
score: 0
Accepted
time: 5ms
memory: 3580kb
input:
4 22 ADCB 36 CADB 42 ABDC 82 DACB 52 BCAD 112 CBAD 69 ADBC 89 DBCA 15 CABD 64 CDBA 31 CBDA 53 BDCA 51 BDAC 56 ACDB 54 CDAB 15 DCAB 29 ACBD 36 ABCD 29 DABC 69 BCDA 62 DCBA 48 DBAC 33
output:
0
result:
ok single line: '0'
Test #9:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
4 22 CDBA 32 BCAD 94 ADBC 39 ACBD 71 ABCD 55 CBAD 65 DBCA 39 DBAC 81 DABC 38 ACDB 13 BDAC 58 BADC 94 CBDA 55 DCAB 90 BDCA 31 ADCB 63 BCDA 23 DCBA 45 ABDC 35 CDAB 52 CADB 14 CABD 32
output:
56
result:
ok single line: '56'
Test #10:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
4 22 CBAD 55 BADC 87 DACB 64 CDAB 57 DBAC 88 CADB 71 ABDC 54 DCAB 72 CABD 32 ADBC 25 ACBD 33 BDAC 58 DCBA 67 CBDA 34 ABCD 12 BCAD 110 ADCB 42 BACD 39 BDCA 41 DABC 39 DBCA 56 BCDA 42
output:
59
result:
ok single line: '59'
Test #11:
score: 0
Accepted
time: 1136ms
memory: 3788kb
input:
5 105 BCDAE 47 ACEDB 42 BDEAC 48 EBDAC 69 DBCEA 177 BECDA 86 BCEAD 42 EBADC 95 ACBDE 85 CABED 70 BCEDA 66 ECDAB 82 CEADB 91 BECAD 78 BDECA 67 ABECD 38 BEDAC 51 BAEDC 76 EABCD 56 ABEDC 50 ADBEC 110 ECBAD 49 ECBDA 15 DBCAE 132 ECDBA 13 BEACD 15 CADBE 37 EBDCA 75 ADCBE 70 BEDCA 79 DCAEB 140 BAECD 61 DB...
output:
291
result:
ok single line: '291'
Test #12:
score: 0
Accepted
time: 1252ms
memory: 3720kb
input:
5 115 DBECA 92 ADCBE 84 DABCE 15 BACED 67 BEDCA 49 ECADB 41 CDBAE 117 CBAED 45 ECABD 76 CAEDB 68 CEBAD 37 DBACE 27 ABDEC 17 CABED 53 ABCDE 133 BCDEA 12 EDABC 30 CBADE 106 EDBCA 61 EDCAB 21 EACDB 33 CDEAB 39 EBACD 67 EBDAC 106 DABEC 94 ACEBD 35 BDECA 70 EDCBA 86 BEACD 63 DCBAE 55 CBDAE 140 CABDE 98 A...
output:
242
result:
ok single line: '242'
Test #13:
score: 0
Accepted
time: 1108ms
memory: 3720kb
input:
5 105 ADCBE 27 CEABD 35 ACEBD 52 DBCEA 106 ACBED 117 BCEAD 110 CEDBA 115 BECAD 79 BEDAC 60 CDBEA 54 CEBDA 54 CAEBD 50 BADEC 107 BCDAE 47 DACEB 24 DCEAB 25 EBDCA 70 BEDCA 65 DABEC 107 AEDBC 27 CDEBA 85 CBEDA 44 CEBAD 37 DABCE 33 BDAEC 72 BCEDA 37 ADBCE 19 CADBE 36 EDBAC 91 BECDA 82 BACDE 91 EACDB 75 ...
output:
609
result:
ok single line: '609'
Test #14:
score: 0
Accepted
time: 1070ms
memory: 3848kb
input:
5 114 EABCD 82 CBEDA 55 EABDC 109 ABEDC 71 CEBAD 32 ADCEB 26 CEDBA 70 DABEC 56 ABDEC 97 ACEBD 36 CBDEA 23 EADBC 67 ACBDE 53 ECADB 65 BCEAD 73 BACDE 95 DCABE 80 EBDCA 20 DEABC 69 ACEDB 37 ABCDE 94 DACBE 61 DECBA 53 CDABE 31 DCEAB 88 BACED 43 BDCEA 34 EBADC 120 AECBD 97 CAEBD 87 DBEAC 80 ECABD 33 DCBA...
output:
78
result:
ok single line: '78'
Test #15:
score: 0
Accepted
time: 1115ms
memory: 3868kb
input:
5 120 BDCEA 145 BAECD 133 CADBE 143 CDEAB 106 EADBC 230 DECAB 141 EBADC 91 DCEBA 195 CDBEA 161 EBCDA 229 BADEC 197 CADEB 198 EBCAD 265 ABCDE 63 BACDE 128 ADEBC 221 CEDBA 168 ADCEB 138 ADCBE 223 EDABC 122 AEBDC 94 ACDEB 192 ADBEC 214 ABCED 182 DCAEB 206 CEBDA 207 AEDBC 202 AECBD 202 EBDCA 141 ECADB 1...
output:
0
result:
ok single line: '0'
Test #16:
score: 0
Accepted
time: 2ms
memory: 3648kb
input:
3 1 ABC 1
output:
2
result:
ok single line: '2'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
3 2 ABC 1 BAC 1
output:
3
result:
ok single line: '3'
Test #18:
score: 0
Accepted
time: 2ms
memory: 3672kb
input:
3 2 ABC 1 CAB 1
output:
1
result:
ok single line: '1'
Test #19:
score: 0
Accepted
time: 1085ms
memory: 3828kb
input:
5 120 BECDA 147736 DACEB 176257 BCEDA 153591 EDACB 162978 BADCE 147012 BDEAC 108087 BCADE 92054 BDECA 98920 EABDC 88384 DAEBC 140278 EABCD 138928 DECBA 151859 CEADB 137696 BDAEC 102371 DEBAC 95988 AEBCD 157319 DCBAE 164357 ABCED 176372 AECBD 135255 BCDEA 106132 BACDE 155499 AECDB 64691 CABED 121278 ...
output:
220621
result:
ok single line: '220621'
Test #20:
score: 0
Accepted
time: 995ms
memory: 3724kb
input:
5 120 BCDEA 127637 BECDA 152490 ADEBC 101841 CBEAD 116226 EABCD 92976 EDCBA 100342 DEACB 140362 DBEAC 120167 CDBAE 99508 DBAEC 176255 ADBCE 94096 BCEAD 134227 BCADE 117466 AEDBC 235510 EDCAB 150545 ACDBE 140266 BDECA 145728 BCAED 114111 ADECB 112189 CEABD 132076 BEDCA 109359 ECABD 165739 DCEBA 12192...
output:
134814
result:
ok single line: '134814'