QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#422259 | #5956. Paradox Sort | zhaohaikun | 32 ✓ | 47ms | 4236kb | C++20 | 3.7kb | 2024-05-27 09:34:26 | 2024-05-27 09:34:26 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[32m[" << __LINE__ << "]\033[0m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
const int N = 2e3 + 10;
int n, pos, deg[N], col[N];//, w[N];
bool vis[N];
pair <int, int> p[N];
bool a[N][N];//, g[N];
void zhk() {
read(n), read(pos)++;
// debug << n << " " << pos << endl;
F(i, 1, n) {
// g[i] = false;
a[i][0] = true;
deg[i] = 0;
F(j, 1, n) {
char ch;
do {
ch = getchar();
} while (ch != '-' && ch != 'N' && ch != 'Y');
deg[i] += (a[i][j] = (ch == 'Y'));
}
p[i] = make_pair(deg[i], i);
}
sort(p + 1, p + n + 1);
int s = 0, cnt = 1;
F(i, 1, n) {
s += p[i].first;
col[p[i].second] = cnt;
if (i != n && s == i * (i - 1) / 2) {
cnt++;
// g[i] = true;
}
}
if (col[pos] != cnt) {
cout << "IMPOSSIBLE\n";
return;
}
// debug << col[pos] << " " << cnt << endl;
// vector <int> ta;
// priority_queue <int, vector <int>, greater <int>> tb;
// F(i, 1, n)
// if (col[pos] == cnt) ta.push_back(i);
// else tb.push(i);
// for (int i: ta) deg[i] -= tb.size();
F(i, 1, n) vis[i] = false;
int cur = 0;
vector <int> ans;
while (!vis[pos]) {
// debug << "! " << ans.size() << endl;
// for (int i: ans) cout << i << " "; cout << endl;
F(i, 1, n) if (!vis[i]) {
int pp;
if (a[cur][i]) pp = cur;
else pp = i;
if (i == pos) {
if (pp != pos) continue;
bool flag = true;
F(j, 1, n)
if (!vis[j] && j != i) {
if (a[j][i]) {
flag = false;
break;
}
}
if (flag) {
vis[i] = true;
ans.push_back(i);
break;
}
} else {
int tot = 0;
F(j, 1, n)
if (!vis[j] && j != i) p[++tot] = make_pair(deg[j] - a[j][i], j);
// if (!tot) {
// ans.push_back(i);
// break;
// }
sort(p + 1, p + tot + 1);
int cnt = 1, s = 0;
F(j, 1, tot) {
s += p[j].first;
col[p[j].second] = cnt;
if (j != tot && s == j * (j - 1) / 2) cnt++;
// debug << " -> " << p[j].first << ' ' << cnt << endl;
}
bool flag = true;
F(j, 1, n) if (!vis[j] && j != i && col[j] > col[pos]) {
// debug << " -> " << j << endl;
if (a[j][pp]) {
flag = false;
break;
}
}
// debug << "! " << i << " " << flag << " " << pp << endl;
// F(j, 1, n)
// if (!vis[j] && j != i) debug << col[j] << endl;
if (flag) {
cur = pp;
F(j, 1, n)
if (!vis[j] && j != i) deg[j] -= a[j][i];
vis[i] = true;
ans.push_back(i);
break;
}
}
// if (col[pos] == cnt) {
// while (tb.size() && tb.top() < ta[i - 1]) {
// ans.push_back(tb.top());
// tb.pop();
// }
// ans.push_back(ta[i - 1]);
// vector <int> nta;
// F(j, 1, ta.size()) if (j != i) {
// if (col[ta[j - 1]] == cnt) nta.push_back(ta[j - 1]);
// else tb.insert(ta[j - 1]);
// }
// break;
// }
}
}
F(i, 1, n)
if (!vis[i]) ans.push_back(i);
for (int i: ans) cout << i - 1 << ' '; cout << '\n';
}
signed main() {
int t; read(t);
F(i, 1, t) {
printf("Case #%d: ", i);
zhk();
}
return 0;
}
详细
Subtask #1:
score: 4
Accepted
Test #1:
score: 4
Accepted
time: 1ms
memory: 3796kb
input:
100 3 0 -YN N-Y YN- 2 0 -Y N- 5 0 -YNNN N-YNN YN-YN YYN-Y YYYN- 5 1 -NYYY Y-NNN NY-NY NYY-N NYNY- 6 5 -YYNNY N-YYNY NN-NYN YNY-NY YYNY-Y NNYNN- 4 0 -YYY N-YN NN-N NYY- 2 0 -Y N- 5 1 -NYNY Y-YYY NN-YY YNN-N NNNY- 7 5 -YYYYYY N-NNYYN NY-YNNN NYN-NYN NNYY-NN NNYNY-N NYYYYY- 8 0 -YNNNNNN N-YNNNNN YN-YNN...
output:
Case #1: 1 2 0 Case #2: 0 1 Case #3: 3 4 2 1 0 Case #4: 0 2 3 4 1 Case #5: 0 1 3 4 2 5 Case #6: 0 1 2 3 Case #7: 0 1 Case #8: 0 1 2 3 4 Case #9: IMPOSSIBLE Case #10: 6 7 5 4 3 2 1 0 Case #11: 0 1 2 Case #12: 0 1 Case #13: 0 1 Case #14: IMPOSSIBLE Case #15: IMPOSSIBLE Case #16: 7 8 6 5 4 ...
result:
ok 100 lines
Subtask #2:
score: 28
Accepted
Test #2:
score: 28
Accepted
time: 47ms
memory: 4236kb
input:
100 39 0 -YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN N-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYYN-YNN...
output:
Case #1: 37 38 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Case #2: 0 13 23 28 30 34 38 40 41 42 43 46 49 51 52 33 5 1 17 32 15 29 19 10 16 47 48 9 4 27 6 7 18 31 8 11 26 50 3 37 25 35 45 20 24 39 22 12 44 36 2 21 14 Case #3: 0 1 2 3 4 5 6 8...
result:
ok 100 lines
Extra Test:
score: 0
Extra Test Passed