QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#649626 | #7960. 排序大师 | hos_lyric | RE | 0ms | 3824kb | C++14 | 6.7kb | 2024-10-18 06:12:34 | 2024-10-18 06:12:35 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
int root(vector<int> &uf, int u) {
return (uf[u] < 0) ? u : (uf[u] = root(uf, uf[u]));
}
bool connect(vector<int> &uf, int u, int v) {
u = root(uf, u);
v = root(uf, v);
if (u == v) return false;
if (uf[u] > uf[v]) swap(u, v);
uf[u] += uf[v];
uf[v] = u;
return true;
}
void exper() {
for (int N = 1; N <= 9; ++N) {
queue<string> que;
map<string, int> dist;
string ini(N, '?');
for (int i = 0; i < N; ++i) ini[i] = '0' + i;
dist[ini] = 0;
que.push(ini);
int maxC = 0;
for (; que.size(); ) {
const string s = que.front();
que.pop();
const int c = dist[s];
// printf("%d %s\n", c, s.c_str());
chmax(maxC, c);
auto sub = [&](int l, int r) -> string {
return s.substr(l, r - l);
};
for (int i = 0; i <= N; ++i) for (int j = i + 1; j <= N; ++j) for (int k = j; k <= N; ++k) for (int l = k + 1; l <= N; ++l) {
const string t = sub(0, i) + sub(k, l) + sub(j, k) + sub(i, j) + sub(l, N);
if (!dist.count(t)) {
dist[t] = dist[s] + 1;
que.push(t);
}
}
}
fflush(stdout);
for (const auto &sc : dist) {
const string &s = sc.first;
const int c = sc.second;
auto at = [&](int i) -> int {
return (0 <= i && i < N) ? (s[i] - '0') : i;
};
vector<int> uf(N + 1, -1);
for (int i = -1; i < N; ++i) {
connect(uf, at(i) + 1, at(i + 1));
}
int k = 0;
for (int u = 0; u <= N; ++u) if (uf[u] < 0) ++k;
assert((N + 1 - k) % 2 == 0);
assert(c == (N + 1 - k) / 2);
bool ok=true;for(int u=0;u<=N;++u)if(uf[u]<0)ok=ok&&(-uf[u]<=2);if(c>=2&&ok){cerr<<c<<" "<<s<<" "<<k<<endl;assert(false);}
}
cerr << "DONE N = " << N << ": maxC = " << maxC << endl;
}
}
/*
p: perm of [1, N]
q: perm of [1, N+1]: p[i] + 1 -> p[i+1] (0 <= i <= N)
... a [b ... c] [d ... e] f ...
... a [d ... e] [b ... c] f ...
a+1 -> b, c+1 -> d, e+1 -> f
a+1 -> d, c+1 -> f, e+1 -> b
3
... a [b ... c] d ... e [f ... g] h ...
... a [f ... g] d ... e [b ... c] h ...
a+1 -> b, c+1 -> d, e+1 -> f, g+1 -> h
a+1 -> f, c+1 -> h, e+1 -> b, g+1 -> d
2,2
target: q only has 1-cycles
always possible (by adj. swaps) ~~> q: even
use (3) to break cycle
a|b ... c|d ... e|f
f,d,b in this order on the cycle
bad:
u[0] -> u[1] -> ... -> u[l-1] -> u[0]
(u[l-1]-1)|u[0] ... (u[0]-1)|u[1] ... ... ... (u[l-2]-1)|u[l-1]
take narrowest cycle ~~> (u ... u-1) must contain some edge, (2,2) usable
use (2,2) to break 2 2-cycles
u <-> v
(v-1)|u ... (u-1)|v ... (v-1)|u
take narrowest cycle ~~> (v ... v-1) must contain some edge, (2,2) usable
*/
int N;
vector<int> P;
int main() {
// exper();
for (; ~scanf("%d", &N); ) {
P.resize(N + 2);
for (int i = 1; i <= N; ++i) {
scanf("%d", &P[i]);
}
P[0] = 0;
P[N + 1] = N + 1;
for (int iter = 0; ; ++iter) {
vector<int> Q(N + 2, 0);
for (int i = 0; i <= N; ++i) Q[P[i] + 1] = P[i + 1];
vector<vector<int>> cycles;
{
vector<int> vis(N + 2, 0);
for (int u = 1; u <= N + 1; ++u) if (!vis[u]) {
vector<int> cyc;
for (int v = u; !vis[v]; v = Q[v]) {
vis[v] = 1;
cyc.push_back(v);
}
cycles.push_back(cyc);
}
}
const int K = cycles.size();
assert((N + 1 - K) % 2 == 0);
if (iter == 0) {
printf("%d\n", (N + 1 - K) / 2);
}
// cerr<<"cycles = "<<cycles<<endl;
if (K == N + 1) break;
vector<int> invP(N + 2, 0);
for (int i = 0; i <= N + 1; ++i) invP[P[i]] = i;
int dm = N + 1;
int km = -1;
for (int k = 0; k < K; ++k) {
auto &cyc = cycles[k];
if (cyc.size() >= 2) {
int mn = N + 2, mx = -1;
for (const int u : cyc) {
chmin(mn, invP[u]);
chmax(mx, invP[u]);
}
if (chmin(dm, mx - mn)) {
km = k;
}
rotate(cyc.begin(), find(cyc.begin(), cyc.end(), P[mn]), cyc.end());
}
}
const auto &cyc = cycles[km];
const int L = cyc.size();
auto oper = [&](int b, int d, int f, int h) -> void {
const int i = invP[b];
const int j = invP[d];
const int k = invP[f];
const int l = invP[h];
assert(i < j); assert(j <= k); assert(k < l);
printf("%d %d %d %d\n", i, j - 1, k, l - 1);
vector<int> PP;
auto add = [&](int lef, int rig) -> void {
PP.insert(PP.end(), P.begin() + lef, P.begin() + rig);
};
add(0, i); add(k, l); add(j, k); add(i, j); add(l, N + 2);
P.swap(PP);
};
for (int l = 0; l < L - 1; ++l) if (invP[cyc[l]] > invP[cyc[(l + 1) % L]]) {
oper(cyc[0], cyc[(l + 1) % L], cyc[(l + 1) % L], cyc[l]);
goto found;
}
{
const int u = cyc[0];
const int v = Q[u];
int w = -1, x = -1;
for (int i = invP[u]; i < invP[u - 1]; ++i) if (P[i] + 1 != P[i + 1]) {
w = P[i + 1];
x = Q[w];
break;
}
assert(~w);
if (invP[w] < invP[x]) {
oper(u, w, v, x);
} else {
oper(x, u, w, v);
}
}
found:{}
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3824kb
input:
1 1
output:
0
result:
ok orz U R the sorting master!
Test #2:
score: -100
Runtime Error
input:
1970 1452 1799 174 371 132 637 23 1510 1819 1794 1665 450 1183 564 1305 548 554 1310 701 1454 1843 1498 1040 1678 77 614 1928 1761 1718 1637 1853 1026 1804 1062 805 864 1859 586 663 346 335 681 152 1768 1639 1713 856 1401 1833 1350 1842 558 241 1829 802 581 1958 845 722 239 1793 1118 1251 1892 1949 ...