QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#227812#6229. 排列SorahISA20 0ms3820kbC++208.0kb2023-10-28 00:12:042023-10-28 00:12:04

Judging History

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

  • [2023-10-28 00:12:04]
  • 评测
  • 测评结果:20
  • 用时:0ms
  • 内存:3820kb
  • [2023-10-28 00:12:04]
  • 提交

answer

#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA

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), 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 (mn >= 1 and (j == -1 or A[j] < mn)) {
            used[A[i] = mn] = 1;
        }
        else {
            used[A[i] = mx] = 1;
        }
        // debug(mn, mx, i, j, A);
    }
    
    int ans = 0;
    for (int L = 0; L < N; ++L) {
        int mn = A[L], mx = A[L];
        for (int R = L; R < N; ++R) {
            chmin(mn, A[R]), chmax(mx, A[R]);
            if (mx - mn == R - L) ++ans;
        }
    }
    cout << ans << "\n";
    for (int i = 0; i < N; ++i) cout << A[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: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 0ms
memory: 3644kb

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: 3548kb

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: -10
Wrong Answer
time: 0ms
memory: 3772kb

input:

9 3
2 3 4

output:

28
2 3 4 1 5 6 7 8 9

result:

wrong answer your plan is not optimal

Subtask #2:

score: 10
Accepted

Test #5:

score: 10
Accepted
time: 0ms
memory: 3820kb

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: 3600kb

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: 3612kb

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: 3652kb

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: 0
Time Limit Exceeded

Test #9:

score: 0
Time Limit Exceeded

input:

199899 1
79400

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #10:

score: 0
Time Limit Exceeded

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:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #12:

score: 0
Time Limit Exceeded

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:


result:


Subtask #7:

score: 0
Time Limit Exceeded

Test #14:

score: 0
Time Limit Exceeded

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:


result:


Subtask #8:

score: 0
Time Limit Exceeded

Test #15:

score: 0
Time Limit Exceeded

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:


result:


Subtask #9:

score: 0
Time Limit Exceeded

Test #16:

score: 0
Time Limit Exceeded

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:


result:


Subtask #10:

score: 0
Time Limit Exceeded

Test #18:

score: 0
Time Limit Exceeded

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:


result: