QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#422256 | #5956. Paradox Sort | zhaohaikun | 0 | 0ms | 0kb | C++20 | 3.6kb | 2024-05-27 09:29:04 | 2024-05-27 09:29:04 |
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;
}
// 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]) {
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) {
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;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
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:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #2:
score: 0
Time Limit Exceeded
input:
100 39 0 -YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN N-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYYN-YNN...