QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#227857 | #6229. 排列 | SorahISA | 70 | 83ms | 23756kb | C++20 | 11.0kb | 2023-10-28 00:59:49 | 2023-10-28 00:59:49 |
Judging History
answer
#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA
int recur(const int N, const vector<int> &arr, int L, int R) {
if (L == R) return 1;
int M = (L + R) >> 1;
int ans = 0;
ans += recur(N, arr, L, M);
ans += recur(N, arr, M+1, R);
static vector<int> mn(N), mx(N);
static vector<int> cnt1(4*N), cnt2(4*N); /// offset by 2*N
mn[M] = mx[M] = arr[M];
for (int i = M-1; i >= L; --i) mn[i] = min(mn[i+1], arr[i]), mx[i] = max(mx[i+1], arr[i]);
mn[M+1] = mx[M+1] = arr[M+1];
for (int i = M+2; i <= R; ++i) mn[i] = min(mn[i-1], arr[i]), mx[i] = max(mx[i-1], arr[i]);
for (int l = M, r1 = M, r2 = M; l >= L; --l) {
/// r1: last pos which mn[r1] and mx[r1] is between mn[l] and mx[l] ///
/// r1: [max - min - r + l = 0] ==> [r = l + max - min] ///
while (r1 < R and mn[r1+1] > mn[l] and mx[r1+1] < mx[l]) {
++r1;
++cnt1[2*N + r1];
--cnt2[2*N + r1 + mn[r1]];
}
/// r2: last pos which mx[r2] is below mx[l] ///
/// r2: [max - min - r + l = 0] ==> [r + min = l + max] ///
while (r2 < R and mx[r2+1] < mx[l]) {
++r2;
++cnt2[2*N + r2 + mn[r2]];
}
ans += cnt1[2*N + l + mx[l] - mn[l]];
ans += cnt2[2*N + l + mx[l]];
if (l == L) {
while (r2 != r1) --cnt2[2*N + r2 + mn[r2]], --r2;
while (r1 != M) --cnt1[2*N + r1], --r1;
}
}
for (int l1 = M+1, l2 = M+1, r = M+1; r <= R; ++r) {
/// l1: first pos which mn[l1] and mx[l1] is between mn[r] and mx[r] ///
/// l1: [max - min - r + l = 0] ==> [l = r + min - max] ///
while (l1 > L and mn[l1-1] > mn[r] and mx[l1-1] < mx[r]) {
--l1;
++cnt1[2*N + l1];
--cnt2[2*N + l1 - mn[l1]];
}
/// l2: first pos which mx[l2] is below mx[r] ///
/// l2: [max - min - r + l = 0] ==> [l - min = r - max] ///
while (l2 > L and mx[l2-1] < mx[r]) {
--l2;
++cnt2[2*N + l2 - mn[l2]];
}
ans += cnt1[2*N + r + mn[r] - mx[r]];
ans += cnt2[2*N + r - mx[r]];
if (r == R) {
while (l2 != l1) --cnt2[2*N + l2 - mn[l2]], ++l2;
while (l1 != M+1) --cnt1[2*N + l1], ++l1;
}
}
return ans;
};
void solve() {
int N, K; cin >> N >> K;
if (K == 0) {
cout << N * (N+1) / 2 << "\n";
for (int i = 1; i <= N; ++i) cout << i << " \n"[i == N];
return;
}
vector<int> A(N), B(N, -1), used(N+1, 0);
for (int i = 0; i < K; ++i) cin >> A[i], used[A[i]] = 1;
for (int i = K, j = K-1, mn = A[j], mx = A[j]; i < N; ++i) {
while (mn >= 1 and used[mn]) --mn;
while (mx <= N and used[mx]) ++mx;
while (j >= 0 and mn <= A[j] and A[j] <= mx) --j;
if (j < 0) {
B = A;
int tmp_mn = mn, tmp_mx = mx;
for (int k = i; k < N; ++k) A[k] = (mn >= 1 ? mn-- : mx++);
mn = tmp_mn, mx = tmp_mx;
for (int k = i; k < N; ++k) B[k] = (mx <= N ? mx++ : mn--);
break;
}
if (A[j] < mn) used[A[i] = mn] = 1;
else used[A[i] = mx] = 1;
// debug(mn, mx, i, j, A);
}
int ans_A = recur(N, A, 0, N-1), ans_B = (B[0] == -1 ? -1 : recur(N, B, 0, N-1));
// int tmp_A = 0, tmp_B = 0;
// for (int L = 0; L < N; ++L) {
// int mn_A = A[L], mx_A = A[L], mn_B = B[L], mx_B = B[L];
// for (int R = L; R < N; ++R) {
// chmin(mn_A, A[R]), chmax(mx_A, A[R]);
// chmin(mn_B, B[R]), chmax(mx_B, B[R]);
// if (mx_A - mn_A == R - L) ++tmp_A;
// if (mx_B - mn_B == R - L) ++tmp_B;
// }
// }
// debug(ans_A, tmp_A, ans_B, tmp_B);
if (ans_A > ans_B) {
cout << ans_A << "\n";
for (int i = 0; i < N; ++i) cout << A[i] << " \n"[i == N-1];
}
else {
cout << ans_B << "\n";
for (int i = 0; i < N; ++i) cout << B[i] << " \n"[i == N-1];
}
}
int32_t main() {
fastIO();
int t = 1; // cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case #" << _ << ": ";
solve();
}
return 0;
}
#else
#ifdef local
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
// #define double __float80
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;
// #define X first
// #define Y second
#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())
template <size_t D, typename T> struct Vec : vector<Vec<D-1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template <typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D-1, T>>(n, Vec<D-1, T>(args...)) {}
};
template <typename T> struct Vec<1, T> : vector<T> {
Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
};
template <class F>
inline constexpr decltype(auto) lambda_fix(F&& f) {
return [f = std::forward<F>(f)](auto&&... args) {
return f(f, std::forward<decltype(args)>(args)...);
};
}
#ifdef local
#define fastIO() void()
#define debug(...) \
_color.emplace_back("\u001b[31m"), \
fprintf(stderr, "%sAt [%s], line %d: (%s) = ", _color.back().c_str(), __FUNCTION__, __LINE__, #__VA_ARGS__), \
_do(__VA_ARGS__), _color.pop_back(), \
fprintf(stderr, "%s", _color.back().c_str())
#define print(...) \
fprintf(stdout, "%s", "\u001b[36m"), \
_P(__VA_ARGS__), \
fprintf(stdout, "%s", "\u001b[0m")
deque<string> _color{"\u001b[0m"};
template <typename T> concept is_string = is_same_v<T, string&> or is_same_v<T, const string&>;
template <typename T> concept is_iterable = requires (T _t) {begin(_t);};
template <typename T> inline void _print_err(T &&_t);
template <typename T> inline void _print_err(T &&_t) requires is_iterable<T> and (not is_string<T>);
template <size_t I, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(const tuple<U...> &);
template <size_t I, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(const tuple<U...> &_t);
template <size_t I, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(tuple<U...> &);
template <size_t I, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(tuple<U...> &_t);
template <typename T, typename U> ostream& operator << (ostream &os, const pair<T, U> &_tu);
inline void _do() {cerr << "\n";};
template <typename T> inline void _do(T &&_t) {_print_err(_t), cerr << "\n";}
template <typename T, typename ...U> inline void _do(T &&_t, U &&..._u) {_print_err(_t), cerr << ", ", _do(_u...);}
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#define print(...) _P(__VA_ARGS__)
#endif
inline void _P() {cout << "\n";};
template <typename T> inline void _P(T &&_t) {cout << _t << "\n";}
template <typename T, typename ...U> inline void _P(T &&_t, U &&..._u) {cout << _t << " ", _P(_u...);}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int getRand(int L, int R) {
if (L > R) swap(L, R);
return (int)(rng() % ((uint64_t)R - L + 1) + L);
}
template <typename T, typename U> bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}
template <typename T, typename U> bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}
/// below are Fast I/O and _print_err templates ///
/*
/// Fast I/O by FHVirus ///
/// https://fhvirus.github.io/blog/2020/fhvirus-io/ ///
#include <unistd.h>
const int S = 65536;
int OP = 0;
char OB[S];
inline char RC() {
static char buf[S], *p = buf, *q = buf;
return p == q and (q = (p = buf) + read(0, buf, S)) == buf ? -1 : *p++;
}
inline int RI() {
static char c;
int a;
while (((c = RC()) < '0' or c > '9') and c != '-' and c != -1);
if (c == '-') {
a = 0;
while ((c = RC()) >= '0' and c <= '9') a *= 10, a -= c ^ '0';
}
else {
a = c ^ '0';
while ((c = RC()) >= '0' and c <= '9') a *= 10, a += c ^ '0';
}
return a;
}
inline void WI(int n, char c = '\n') {
static char buf[20], p;
if (n == 0) OB[OP++] = '0';
p = 0;
if (n < 0) {
OB[OP++] = '-';
while (n) buf[p++] = '0' - (n % 10), n /= 10;
}
else {
while (n) buf[p++] = '0' + (n % 10), n /= 10;
}
for (--p; p >= 0; --p) OB[OP++] = buf[p];
OB[OP++] = c;
if (OP > S-20) write(1, OB, OP), OP = 0;
}
/// Fast I/O by FHVirus ///
/// https://fhvirus.github.io/blog/2020/fhvirus-io/ ///
*/
#ifdef local
template <typename T> inline void _print_err(T &&_t) {cerr << _t;}
template <typename T> inline void _print_err(T &&_t) requires is_iterable<T> and (not is_string<T>) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back() << "[";
for (bool _first = true; auto &_x : _t) {
if (!_first) cerr << ", ";
_print_err(_x), _first = false;
}
cerr << "]" << (_color.pop_back(), _color.back());
}
template <typename T, typename U> ostream& operator << (ostream &os, const pair<T, U> &_tu) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back() << "(";
_print_err(_tu.first), cerr << ", ", _print_err(_tu.second);
cerr << ")" << (_color.pop_back(), _color.back());
return os;
}
template <size_t I = 0, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(const tuple<U...> &) {
cerr << ")" << (_color.pop_back(), _color.back());
}
template <size_t I = 0, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(const tuple<U...> &_t) {
if (!I) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back();
}
cerr << (I ? ", " : "("), _print_err(get<I>(_t)), _print_err<I+1, U...>(_t);
}
template <size_t I = 0, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(tuple<U...> &) {
cerr << ")" << (_color.pop_back(), _color.back());
}
template <size_t I = 0, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(tuple<U...> &_t) {
if (!I) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back();
}
cerr << (I ? ", " : "("), _print_err(get<I>(_t)), _print_err<I+1, U...>(_t);
}
#endif
#endif
详细
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 0ms
memory: 3628kb
input:
10 9 2 5 10 8 1 4 9 7 6
output:
12 2 5 10 8 1 4 9 7 6 3
result:
ok correct!
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
10 6 4 3 2 1 5 7
output:
32 4 3 2 1 5 7 6 8 9 10
result:
ok correct!
Test #3:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
10 4 10 5 7 8
output:
30 10 5 7 8 6 9 4 3 2 1
result:
ok correct!
Test #4:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
9 3 2 3 4
output:
38 2 3 4 5 6 7 8 9 1
result:
ok correct!
Subtask #2:
score: 10
Accepted
Test #5:
score: 10
Accepted
time: 0ms
memory: 3604kb
input:
20 14 6 1 4 3 2 10 5 20 17 16 13 18 15 12
output:
34 6 1 4 3 2 10 5 20 17 16 13 18 15 12 14 19 11 9 8 7
result:
ok correct!
Test #6:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
20 5 19 16 8 7 10
output:
76 19 16 8 7 10 9 11 12 13 14 15 17 18 6 5 4 3 2 1 20
result:
ok correct!
Subtask #3:
score: 10
Accepted
Test #7:
score: 10
Accepted
time: 0ms
memory: 3532kb
input:
22 20 3 19 17 5 7 12 15 22 10 13 11 8 2 9 6 14 16 20 4 1
output:
23 3 19 17 5 7 12 15 22 10 13 11 8 2 9 6 14 16 20 4 1 18 21
result:
ok correct!
Test #8:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
22 14 3 18 12 11 7 1 8 4 14 9 22 21 16 5
output:
27 3 18 12 11 7 1 8 4 14 9 22 21 16 5 6 10 13 15 17 19 20 2
result:
ok correct!
Subtask #4:
score: 10
Accepted
Test #9:
score: 10
Accepted
time: 62ms
memory: 23412kb
input:
199899 1 79400
output:
10412404949 79400 79401 79402 79403 79404 79405 79406 79407 79408 79409 79410 79411 79412 79413 79414 79415 79416 79417 79418 79419 79420 79421 79422 79423 79424 79425 79426 79427 79428 79429 79430 79431 79432 79433 79434 79435 79436 79437 79438 79439 79440 79441 79442 79443 79444 79445 79446 79447 ...
result:
ok correct!
Subtask #5:
score: 10
Accepted
Test #10:
score: 10
Accepted
time: 54ms
memory: 22776kb
input:
191393 191393 32224 3810 113151 150094 136298 14214 128374 5493 40926 95388 13836 108772 80691 142140 98735 77447 47803 161246 117751 68781 68971 47977 96955 84464 105119 76918 59979 19121 179774 121202 185018 68697 26220 118151 188665 166116 184189 124353 23802 62039 22066 142966 171539 104019 1189...
output:
191398 32224 3810 113151 150094 136298 14214 128374 5493 40926 95388 13836 108772 80691 142140 98735 77447 47803 161246 117751 68781 68971 47977 96955 84464 105119 76918 59979 19121 179774 121202 185018 68697 26220 118151 188665 166116 184189 124353 23802 62039 22066 142966 171539 104019 118900 2254...
result:
ok correct!
Test #11:
score: 0
Accepted
time: 48ms
memory: 23756kb
input:
200000 200000 3 2 1 33 35 12 36 17 14 19 6 10 21 20 5 13 28 9 15 25 27 7 26 34 32 37 30 24 4 11 23 29 18 8 22 31 16 44 41 39 40 38 43 42 53 58 72 67 51 50 76 47 61 49 77 73 54 55 69 74 65 48 62 75 59 57 64 70 68 60 45 52 71 66 56 63 46 86 104 89 97 106 87 90 103 95 92 109 108 102 105 80 88 84 100 98...
output:
21330771 3 2 1 33 35 12 36 17 14 19 6 10 21 20 5 13 28 9 15 25 27 7 26 34 32 37 30 24 4 11 23 29 18 8 22 31 16 44 41 39 40 38 43 42 53 58 72 67 51 50 76 47 61 49 77 73 54 55 69 74 65 48 62 75 59 57 64 70 68 60 45 52 71 66 56 63 46 86 104 89 97 106 87 90 103 95 92 109 108 102 105 80 88 84 100 98 110 ...
result:
ok correct!
Subtask #6:
score: 10
Accepted
Test #12:
score: 10
Accepted
time: 58ms
memory: 22084kb
input:
185493 185493 164138 100544 43516 166466 20210 73092 6885 134912 135326 146721 166053 126249 173180 143077 7740 63990 108333 144740 79182 183947 95106 159595 97281 77456 17553 108730 7747 128355 70374 68823 126350 73005 89833 130930 104817 88271 37412 5136 92049 7100 100916 103685 27328 98227 130349...
output:
185496 164138 100544 43516 166466 20210 73092 6885 134912 135326 146721 166053 126249 173180 143077 7740 63990 108333 144740 79182 183947 95106 159595 97281 77456 17553 108730 7747 128355 70374 68823 126350 73005 89833 130930 104817 88271 37412 5136 92049 7100 100916 103685 27328 98227 130349 18526 ...
result:
ok correct!
Test #13:
score: 0
Accepted
time: 42ms
memory: 21928kb
input:
184857 184857 36 29 19 31 22 28 5 35 6 2 21 14 10 25 13 23 24 7 33 3 15 27 4 1 20 11 18 30 34 32 8 17 9 26 16 12 65 45 53 58 47 54 61 39 71 56 38 51 77 52 62 67 70 75 59 46 76 40 72 79 68 49 48 78 43 69 55 64 37 66 74 41 57 63 50 73 80 42 44 60 95 100 99 96 93 98 105 87 85 86 97 101 90 94 89 103 92 ...
output:
18143719 36 29 19 31 22 28 5 35 6 2 21 14 10 25 13 23 24 7 33 3 15 27 4 1 20 11 18 30 34 32 8 17 9 26 16 12 65 45 53 58 47 54 61 39 71 56 38 51 77 52 62 67 70 75 59 46 76 40 72 79 68 49 48 78 43 69 55 64 37 66 74 41 57 63 50 73 80 42 44 60 95 100 99 96 93 98 105 87 85 86 97 101 90 94 89 103 92 102 8...
result:
ok correct!
Subtask #7:
score: 0
Wrong Answer
Test #14:
score: 0
Wrong Answer
time: 27ms
memory: 12936kb
input:
94962 47652 17 10 11 21 5 12 20 1 13 19 66 114 38 79 83 49 108 104 112 94 44 24 63 105 67 85 70 100 73 39 72 91 96 71 99 28 88 95 106 55 42 65 110 60 97 35 81 76 54 47 141 130 172 189 137 121 171 183 180 196 166 147 133 194 175 165 193 144 119 146 205 122 145 118 149 126 206 199 138 195 207 129 148 ...
output:
147770 17 10 11 21 5 12 20 1 13 19 66 114 38 79 83 49 108 104 112 94 44 24 63 105 67 85 70 100 73 39 72 91 96 71 99 28 88 95 106 55 42 65 110 60 97 35 81 76 54 47 141 130 172 189 137 121 171 183 180 196 166 147 133 194 175 165 193 144 119 146 205 122 145 118 149 126 206 199 138 195 207 129 148 188 1...
result:
wrong answer your plan is not optimal
Subtask #8:
score: 0
Wrong Answer
Test #15:
score: 0
Wrong Answer
time: 63ms
memory: 23428kb
input:
200000 70130 3 6 13 15 5 76 59 104 89 42 60 73 51 115 46 105 26 31 45 116 35 108 111 88 41 50 91 32 48 69 20 114 87 29 117 53 74 96 19 94 34 55 18 72 100 170 147 121 132 169 122 171 160 128 129 164 123 124 168 138 120 153 139 142 173 141 133 148 126 125 174 175 190 198 183 197 178 192 185 176 193 21...
output:
451481 3 6 13 15 5 76 59 104 89 42 60 73 51 115 46 105 26 31 45 116 35 108 111 88 41 50 91 32 48 69 20 114 87 29 117 53 74 96 19 94 34 55 18 72 100 170 147 121 132 169 122 171 160 128 129 164 123 124 168 138 120 153 139 142 173 141 133 148 126 125 174 175 190 198 183 197 178 192 185 176 193 219 202 ...
result:
wrong answer your plan is not optimal
Subtask #9:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 64ms
memory: 23428kb
input:
200000 111432 12 35 21 17 9 43 25 36 47 38 10 42 28 13 32 24 3 46 26 48 49 5 7 34 27 50 37 1 40 53 54 52 68 107 82 101 99 74 79 88 104 96 67 100 94 80 56 66 105 91 102 61 55 106 57 86 87 70 76 75 65 71 89 58 112 110 150 111 132 153 137 141 152 114 122 125 113 146 121 118 115 133 171 205 199 226 176 ...
output:
284000 12 35 21 17 9 43 25 36 47 38 10 42 28 13 32 24 3 46 26 48 49 5 7 34 27 50 37 1 40 53 54 52 68 107 82 101 99 74 79 88 104 96 67 100 94 80 56 66 105 91 102 61 55 106 57 86 87 70 76 75 65 71 89 58 112 110 150 111 132 153 137 141 152 114 122 125 113 146 121 118 115 133 171 205 199 226 176 202 214...
result:
wrong answer your plan is not optimal
Subtask #10:
score: 10
Accepted
Test #18:
score: 10
Accepted
time: 53ms
memory: 23424kb
input:
200000 180419 41 16 42 49 52 27 6 22 7 24 12 20 23 30 13 1 40 38 11 35 5 43 19 46 18 29 39 4 17 31 44 10 47 25 15 9 36 50 53 37 34 33 32 48 55 14 26 8 45 2 104 93 82 102 57 91 98 81 79 84 65 69 97 83 64 76 101 105 96 89 72 78 85 86 74 92 90 58 103 67 75 100 70 99 87 61 60 107 88 80 62 59 71 95 106 7...
output:
228165 41 16 42 49 52 27 6 22 7 24 12 20 23 30 13 1 40 38 11 35 5 43 19 46 18 29 39 4 17 31 44 10 47 25 15 9 36 50 53 37 34 33 32 48 55 14 26 8 45 2 104 93 82 102 57 91 98 81 79 84 65 69 97 83 64 76 101 105 96 89 72 78 85 86 74 92 90 58 103 67 75 100 70 99 87 61 60 107 88 80 62 59 71 95 106 77 115 1...
result:
ok correct!
Test #19:
score: 0
Accepted
time: 83ms
memory: 23200kb
input:
196542 192884 4015 68810 47545 70278 196343 25868 16040 103198 150508 130222 192293 1148 88773 150912 49343 132154 140164 193876 122474 117922 173775 56014 184072 109551 52544 117341 77408 122145 55476 176029 2985 137183 77861 12484 108489 44578 134648 87137 74111 3074 81632 81673 191887 76403 51555...
output:
196638 4015 68810 47545 70278 196343 25868 16040 103198 150508 130222 192293 1148 88773 150912 49343 132154 140164 193876 122474 117922 173775 56014 184072 109551 52544 117341 77408 122145 55476 176029 2985 137183 77861 12484 108489 44578 134648 87137 74111 3074 81632 81673 191887 76403 51555 17946 ...
result:
ok correct!