Codeforces Gym 100431D Bubble Sort 水题乱搞
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-andrew-stankevich-contest-37-asc-37-en.pdf
题意
神奇的电路可以通过比较器来实现冒泡。
题解
倒着往回考虑,搞搞就好。
代码
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define MAX_N 222 using namespace std; int n; int a[MAX_N][MAX_N]; int x[MAX_N]; int tim[MAX_N][MAX_N]; int main() { freopen("bubble.in", "r", stdin); freopen("bubble.out", "w", stdout); tim[0][0] = 0; scanf("%d", &n); for (int i = 0; i < n; i++)x[i] = i; for (int i = 0; i < n - 1; i++) for (int j = 0; j <= i; j++) scanf("%d", &a[i][j]); for (int i = 0; i < n - 2; i++) for (int j = 0; j <= i; j++) tim[i + 1][j] = tim[i][j] + 1, tim[i + 1][j + 1] = tim[i][j] - 1; for (int now = tim[n - 2][n - 2]; now <= tim[n - 2][0]; now++) { for (int i = 0; i < n - 1; i++) for (int j = 0; j <= i; j++) if (tim[i][j] == now) { int u = i, v = i + 1; if (x[u] > x[v]) { printf("NO\n"); return 0; } if (a[i][j])swap(x[u], x[v]); } } printf("YES\n"); for (int i = 0; i < n; i++) printf("%d ", x[i] + 1); return 0; }