ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214275 | #2022. a | nodgd | 20 | 39ms | 3348kb | C++11 | 2.0kb | 2024-11-16 22:24:29 | 2024-11-16 23:14:26 |
answer
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 32000 + 5;
int N, a[MAX_N], b[MAX_N];
vector<pair<int,int>> op;
void solve_2(int l, int r, int x) {
if (l == r) return;
int m = l + r >> 1;
solve_2(l, m, x);
solve_2(m + 1, r, x);
int p = m, q = m + 1;
for (; p >= l && a[p] > x; p --);
for (; q <= r && a[q] <= x; q ++);
// printf(" l=%d,r=%d, p=%d,q=%d\n", l, r, p, q);
if (p < m && q > m + 1) {
// printf(" +[%d,%d]\n", p + 1, q - 1);
reverse(a + p + 1, a + q);
op.push_back(make_pair(p + 1, q - 1));
}
}
void solve_1(int l, int r) {
if (l == r) return;
int m = l + r >> 1;
solve_2(l, m, b[m]);//左边找>b[m]的,放结尾
solve_2(m + 1, r, b[m + 1] - 1);//右边找<b[m]的,放开头
int p = m, q = m + 1;
for (; p >= l && a[p] > b[m]; p --);
for (; q <= r && a[q] < b[m + 1]; q ++);
// printf("l=%d,r=%d, p=%d,q=%d\n", l, r, p, q);
// for (int i = 1; i <= N; i ++) printf("%d%c", a[i], i < N ? ' ' : '\n');
// for (int i = l; i <= p; i ++) assert(a[i] <= b[m]);
// for (int i = r; i >= q; i --) assert(a[i] >= b[m + 1]);
if (p + 1 < q - 1) {
reverse(a + p + 1, a + q);
// printf(" +[%d,%d]\n", p + 1, q - 1);
op.push_back(make_pair(p + 1, q - 1));
}
// for (int i = l; i <= m; i ++) assert(a[i] <= b[m]);
// for (int i = r; i > m; i --) assert(a[i] >= b[m]);
solve_1(l, m);
solve_1(m + 1, r);
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i ++) {
scanf("%d", &a[i]);
}
// N = 32000;
// for (int i = 1; i <= N; i ++) {
// a[i] = N + 1 - i;
// }
for (int i = 1; i <= N; i ++) {
b[i] = a[i];
}
sort(b + 1, b + 1 + N);
solve_1(1, N);
printf("%d\n", (int)op.size());
long long s = 0;
for (auto t: op) {
printf("%d %d\n", t.first, t.second);
s += t.second - t.first + 1;
}
fprintf(stderr, "s=%lld\n", s);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 0ms
memory: 1264kb
input:
63 19732 30594 10113 7702 9784 6421 4697 13517 5317 8508 26509 15653 2986 31587 11246 12158 24378 49...
output:
163 1 4 3 8 11 12 14 16 12 15 7 14 17 18 22 23 18 22 27 28 26 27 29 30 30 31 27 30 20 28 13 23 33 35...
result:
ok ok,using 575 times
Test #2:
score: 0
Accepted
time: 0ms
memory: 1256kb
input:
25 21264 13876 11861 12802 18452 3136 17660 21163 14140 20632 25998 22051 10612 12680 7873 23249 274...
output:
55 1 2 2 4 5 6 6 7 4 6 8 9 11 13 9 11 6 9 17 19 16 17 20 22 24 25 21 24 17 22 8 19 1 3 2 6 8 9 9 10 ...
result:
ok ok,using 154 times
Test #3:
score: 0
Accepted
time: 0ms
memory: 1264kb
input:
95 26373 31391 7777 12225 25301 24166 2461 4926 15751 18727 29175 18511 17455 16025 16845 6723 3477 ...
output:
259 1 3 2 4 3 9 13 14 14 17 22 23 23 24 20 23 16 21 6 18 25 26 32 33 33 34 26 33 44 45 46 47 47 48 4...
result:
ok ok,using 1022 times
Test #4:
score: 0
Accepted
time: 0ms
memory: 1268kb
input:
100 15104 1621 22502 12704 13109 9592 1390 22280 23468 26863 23084 10981 20492 7842 12883 16636 2565...
output:
279 1 2 3 4 2 3 3 7 11 12 8 11 6 8 17 18 16 17 21 22 23 24 24 25 22 24 17 23 7 20 26 31 33 34 34 35 ...
result:
ok ok,using 1122 times
Test #5:
score: 0
Accepted
time: 0ms
memory: 1264kb
input:
100 13822 19801 16537 25492 24250 30303 18588 13044 8538 23642 864 10878 16671 5509 13062 15457 2577...
output:
299 10 12 2 11 17 18 18 19 20 25 19 22 6 21 28 31 33 37 30 34 39 40 43 44 40 43 45 50 42 47 32 44 14...
result:
ok ok,using 1118 times
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 40
Accepted
time: 0ms
memory: 1276kb
input:
197 10471 12679 10817 27406 21095 21068 9625 5396 14548 20977 29338 17674 30961 25672 4782 22715 301...
output:
650 5 7 4 5 5 9 14 15 20 21 21 22 15 21 7 16 26 27 27 29 30 32 29 30 34 35 37 38 35 37 30 36 39 41 4...
result:
ok ok,using 2797 times
Test #7:
score: 0
Accepted
time: 0ms
memory: 1288kb
input:
354 28133 31628 5619 8961 1003 9779 27591 14336 31618 17755 6349 17570 25604 24107 23136 21537 16494...
output:
1331 1 3 2 6 7 8 10 11 8 10 5 8 22 23 13 22 7 16 25 26 26 29 30 34 29 31 36 37 38 39 39 40 37 39 41 ...
result:
ok ok,using 6172 times
Test #8:
score: -40
Wrong Answer
time: 0ms
memory: 1316kb
input:
512 14563 17808 18597 4341 12143 30490 13558 4331 30512 15302 14593 3642 21783 21775 23315 5766 1661...
output:
2053 3 4 2 3 6 8 3 7 9 10 10 12 15 16 13 15 12 13 6 12 17 18 19 20 18 19 19 22 29 31 28 29 21 28 10 ...
result:
wrong answer sequence hasn't been sorted
Subtask #3:
score: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 10ms
memory: 1936kb
input:
29160 3 3 5 5 4 4 5 1 3 2 5 1 4 3 2 3 1 2 1 5 4 4 4 0 2 3 4 2 1 4 4 5 3 3 5 4 2 4 4 1 3 5 1 1 2 2 2 ...
output:
45741 7 8 5 7 1 5 9 10 11 12 10 11 13 15 11 13 2 11 16 17 17 19 23 24 24 25 27 28 28 29 25 28 19 26 ...
result:
wrong answer sequence hasn't been sorted
Subtask #4:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 29ms
memory: 3348kb
input:
25162 6548 134 11176 15393 24121 2053 29582 27616 22505 27930 3608 3082 22087 20841 5912 29959 21750...
output:
164821 5 6 8 12 6 9 14 15 17 18 15 17 20 22 23 25 21 23 16 21 8 17 26 27 27 30 36 38 35 36 28 35 39 ...
result:
wrong answer sequence hasn't been sorted