QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#227844#6229. 排列SorahISA10 97ms23492kbC++2010.6kb2023-10-28 00:52:172023-10-28 00:52:17

Judging History

你现在查看的是最新测评结果

  • [2023-10-28 00:52:17]
  • 评测
  • 测评结果:10
  • 用时:97ms
  • 内存:23492kb
  • [2023-10-28 00:52:17]
  • 提交

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), 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 = recur(N, A, 0, N-1);
    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

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3840kb

input:

10 9
2 5 10 8 1 4 9 7 6

output:

12
0 0 0 0 0 0 0 0 0 0

result:

wrong answer Integer parameter [name=p_i] equals to 0, violates the range [1, 10]

Subtask #2:

score: 0
Wrong Answer

Test #5:

score: 0
Wrong Answer
time: 0ms
memory: 3824kb

input:

20 14
6 1 4 3 2 10 5 20 17 16 13 18 15 12

output:

34
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

result:

wrong answer Integer parameter [name=p_i] equals to 0, violates the range [1, 20]

Subtask #3:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 0ms
memory: 3564kb

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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

result:

wrong answer Integer parameter [name=p_i] equals to 0, violates the range [1, 22]

Subtask #4:

score: 10
Accepted

Test #9:

score: 10
Accepted
time: 54ms
memory: 23440kb

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: 0
Wrong Answer

Test #10:

score: 0
Wrong Answer
time: 83ms
memory: 22640kb

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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

result:

wrong answer Integer parameter [name=p_i] equals to 0, violates the range [1, 191393]

Subtask #6:

score: 0
Wrong Answer

Test #12:

score: 0
Wrong Answer
time: 79ms
memory: 22212kb

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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

result:

wrong answer Integer parameter [name=p_i] equals to 0, violates the range [1, 185493]

Subtask #7:

score: 0
Wrong Answer

Test #14:

score: 0
Wrong Answer
time: 30ms
memory: 12860kb

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: 68ms
memory: 23464kb

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:

451477
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: 72ms
memory: 23492kb

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: 0
Wrong Answer

Test #18:

score: 0
Wrong Answer
time: 97ms
memory: 23408kb

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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

result:

wrong answer Integer parameter [name=p_i] equals to 0, violates the range [1, 200000]