QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#294305 | #5529. Most Annoying Constructive Problem | zhoukangyang# | RE | 1ms | 3652kb | C++14 | 5.8kb | 2023-12-30 11:42:26 | 2023-12-30 11:42:26 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define ull unsigned long long
#define i128 __int128
#define pb emplace_back
using namespace std;
const int N = 1007;
int n, k;
int p[N];
bool h[N][N];
int vis[N];
vi V[N];
int cnt;
void Gen() {
cnt = 0;
L(i, 1, n) {
L(j, i + 1, n) {
h[i][j] = p[i] > p[j];
}
}
L(i, 1, n) {
L(j, i + 1, n) {
h[i][j] ^= h[i][j - 1];
}
}
R(i, n, 1) {
L(j, i + 1, n) {
h[i][j] ^= h[i + 1][j];
}
}
L(i, 1, n) {
L(j, i + 1, n) {
cnt += h[i][j];
}
}
}
void upm(int l, int r) {
L(j, l, r)
R(i, j - 1, 1)
cnt -= h[i][j],
h[i][j] = h[i + 1][j] ^ h[i][j - 1] ^ h[i + 1][j - 1] ^ (p[i] > p[j]),
cnt += h[i][j];
R(i, r, l)
L(j, i + 1, n)
cnt -= h[i][j],
h[i][j] = h[i + 1][j] ^ h[i][j - 1] ^ h[i + 1][j - 1] ^ (p[i] > p[j]),
cnt += h[i][j];
}
mt19937_64 orz;
const int mx = 1e9;
inline int rad(int l, int r) {
return orz() % (r - l + 1) + l;
}
inline bool update(int x, int nw) {
if(x < k && nw >= x) {
return 1;
}
if(x > k && nw <= x) {
return 1;
}
return orz() % mx < exp(-(double) abs(nw - k) / n * 10) * mx;
}
void solve(int tn, int tm) {
n = tn, k = tm;
if(k < n * (n - 1) / 4) {
L(i, 1, n) {
p[i] = i;
}
} else {
L(d, 1, n / 2) {
p[d * 2 - 1] = d * 2 + 1;
p[d * 2] = d * 2 - 1;
if(1 < d && d < n / 2) {
--p[d * 2];
}
if(d == n / 2)
--p[d * 2], --p[d * 2 - 1];
}
if(n % 2)p[n] = n;
}
Gen();
if((cnt - k) & 1) {
swap(p[1], p[2]);
Gen();
}
while(true){
int lst = cnt;
if(orz() % 2 == 0) {
int d = rad(1, n - 2);
swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);
upm(d, d + 2);
if(!update(lst, cnt)) {
swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
upm(d, d + 2);
// Gen();
}
}else {
int d = rad(1, n - 2);
swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
upm(d, d + 2);
if(!update(lst, cnt)) {
swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);
upm(d, d + 2);
}
}
if(cnt == k) {
Gen();
cout << "YES\n";
L(i, 1, n) cout << p[i] << ' ';
cout << '\n';
break;
}
if(orz()%(n*20)==0){
solve(tn,tm);
return;
}
}
}
void mini_solve(int tn, int tm) {
n = tn, k = tm;
if(k < n * (n - 1) / 4) {
L(i, 1, n) {
p[i] = i;
}
} else {
L(d, 1, n / 2) {
p[d * 2 - 1] = d * 2 + 1;
p[d * 2] = d * 2 - 1;
if(1 < d && d < n / 2) {
--p[d * 2];
}
if(d == n / 2)
--p[d * 2], --p[d * 2 - 1];
}
if(n % 2)p[n] = n;
}
Gen();
int Lun = (n + 3) / 3;
int g = sqrt(k) * 4;
g = min(g, n - 2);
L(d, 1, Lun){
int lst = cnt;
if(orz() % 2 == 0) {
int d = rad(1, g);
if(orz() & 1) d = n - 1 - d;
swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);
upm(d, d + 2);
if(!update(lst, cnt)) {
swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
upm(d, d + 2);
}
}else {
int d = rad(1, g);
if(orz() & 1) d = n - 1 - d;
swap(p[d + 1], p[d + 2]), swap(p[d], p[d + 1]);
upm(d, d + 2);
if(!update(lst, cnt)) {
swap(p[d], p[d + 1]), swap(p[d + 1], p[d + 2]);
upm(d, d + 2);
}
}
if(cnt == k) {
Gen();
cout << cnt << ' ' << k << endl;
cout << "YES\n";
L(i, 1, n) cout << p[i] << ' ';
cout << '\n';
return;
}
}
mini_solve(tn, tm);
}
bitset < N > dm[N], shan[N];
int ts;
void islv(int tn, int tm) {
n = tn;
k = tm;
auto dfs = [&] (auto self, int x, int cur) {
if(cur < 0)return 0;
int con = n * (n - 1) / 2 - (x - 1) * (x - 2) / 2 - max(n - x - 1, 0) / 2;
if(cur > con)return 0;
if(x == n + 1) {
cout << "YES\n";
L(i, 1, n) cout << p[i] << ' ';
cout << '\n';
Gen();
return 1;
}
dm[x] = dm[x - 1];
bitset < N > add;
R(i, x, max(1, x - 4)) {
if(i < x) {
int pos = 0;
R(j, x - 1, 1) {
if(p[j] == i) {
pos = j;
break;
}
}
add.set(pos);
++p[pos];
dm[x] ^= shan[pos];
}
p[x] = i;
int cmll = cur - dm[x].count();
if(self(self, x + 1, cmll)) return 1;
}
for(int d = add._Find_first(); d <= n; d = add._Find_next(d))
--p[d];
return 0;
};
if(dfs(dfs, 1, k)) {
} else {
cout << "NO\n";
}
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
L(i, 1, 1000) {
shan[i] = shan[i - 1];
shan[i].set(i);
}
int t;
cin >> t;
while(t--) {
int n, m;
cin >> n >> m;
int mxm = n * (n - 1) / 2 - n / 2;
if(mxm - m <= n || n <= 5) {
islv(n, m);
} else if(m < n) {
mini_solve(n, m);
} else {
solve(n, m);
}
}
return 0;
cin >> n;
L(i, 1, n) {
p[i] = i;
}
do {
Gen();
int cnt = 0;
L(i, 1, n) {
L(j, i + 1, n) {
cnt += h[i][j];
}
}
if(n*(n-1)/2-n/2-cnt<=3){
L(i, 1, n) {
L(j, 1, n) {
if(j <= i) cout << " ";
else cout << (p[i] > p[j]); // h[i][j];
}
cout << endl;
}
L(i,1,n)cout<<p[i]<<' ';
cout<<":"<<cnt<<endl;
cout<<endl;
}
vis[cnt] = 1;
} while(next_permutation(p + 1, p + n + 1));
// int gm=0;
// L(i, 0, n * (n - 1) / 2) {
// if(!vis[i])++gm;
//// cout<<vis[i];
// if(vis[i]) {
// L(d, 1, n) {
// p[d] = V[i][d - 1];
// }
// Gen();
// L(i, 1, n) {
// L(j, 1, n) {
// if(j <= i) cout << " ";
// else cout << (p[i] > p[j]); // h[i][j];
// }
// cout << endl;
// }
// for(auto&p : V[i]){
// cout << p << ' ';
// }
// cout << endl;
// cout << i << endl;
// cout << endl;
// }
// }
// cout<<gm<<endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3652kb
input:
4 1 0 3 3 4 1 6 15
output:
YES 1 YES 3 2 1 YES 1 3 4 2 NO
result:
ok Correct (4 test cases)
Test #2:
score: -100
Runtime Error
input:
5951 27 255 28 371 31 320 33 201 32 199 31 108 15 78 27 32 24 268 20 4 14 82 29 202 33 85 26 104 31 380 27 330 20 140 29 192 21 63 17 89 20 41 32 444 20 76 27 114 33 330 26 249 33 301 24 87 25 72 14 49 25 281 21 58 15 48 16 19 14 0 22 175 11 7 30 43 31 259 22 112 12 30 30 111 33 268 30 287 19 150 15...
output:
YES 3 2 5 6 13 7 1 4 8 9 12 10 15 11 17 14 23 19 16 25 21 18 22 20 26 24 27 NO YES 5 1 7 2 3 4 11 9 6 8 13 15 12 10 16 14 17 19 21 20 18 23 25 22 24 30 29 26 27 28 31 YES 2 1 3 6 4 5 7 10 14 9 13 8 12 18 11 15 16 17 19 23 21 22 24 20 25 26 27 30 29 31 28 32 33 YES 3 2 1 4 5 6 8 9 7 10 12 13 11 14...