QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#679191 | #5143. Quick Sort | lmx111 | WA | 55ms | 3648kb | C++20 | 2.9kb | 2024-10-26 17:05:17 | 2024-10-26 17:05:18 |
Judging History
answer
// Coded by hjxddl
#include <bits/stdc++.h>
#define ll long long
#define db double
#define mid (l + r >> 1)
const int N = 2e5 + 5;
int n;
ll ans;
std::vector<int> a, min, max;
void build(int x = 1, int l = 1, int r = n) {
if (l == r) {
min[x] = max[x] = a[l];
return;
}
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
min[x] = std::min(min[x << 1], min[x << 1 | 1]);
max[x] = std::max(max[x << 1], max[x << 1 | 1]);
}
int query(int opt, int k, int l1, int r1, int x = 1, int l = 1, int r = n) {
if (l == r)
return l;
if (opt == 0) {
int ans = n + 5;
if (max[x << 1] >= k) {
if (l1 <= mid)
ans = std::min(ans, query(opt, k, l1, r1, x << 1, l, mid));
if (ans == n + 5 && max[x << 1 | 1] >= k)
return query(opt, k, l1, r1, x << 1 | 1, mid + 1, r);
return ans;
} else {
if (max[x << 1 | 1] >= k)
return query(opt, k, l1, r1, x << 1 | 1, mid + 1, r);
return ans;
}
}
if (opt == 1) {
int ans = 0;
if (min[x << 1 | 1] <= k) {
if (r1 > mid)
ans = std::max(ans, query(opt, k, l1, r1, x << 1 | 1, mid + 1, r));
if (ans == 0 && min[x << 1] <= k)
return query(opt, k, l1, r1, x << 1, l, mid);
return ans;
} else {
if (min[x << 1] <= k)
return query(opt, k, l1, r1, x << 1, l, mid);
return ans;
}
}
}
void change(int k, int pos, int x = 1, int l = 1, int r = n) {
if (l == r) {
min[x] = max[x] = k;
return;
}
if (pos <= mid)
change(k, pos, x << 1, l, mid);
else
change(k, pos, x << 1 | 1, mid + 1, r);
min[x] = std::min(min[x << 1], min[x << 1 | 1]);
max[x] = std::max(max[x << 1], max[x << 1 | 1]);
}
int partition(int l, int r) {
int p = a[mid];
while (1) {
int i = query(0, p, l, n);
int j = query(1, p, 1, r);
if (i >= j) return j;
int x = a[i], y = a[j];
std::swap(a[i], a[j]);
change(x, j);
change(y, i);
ans++;
}
}
void quicksort(int l, int r) {
if (l >= 0 && r >= 0 && l < r) {
int p = partition(l, r);
quicksort(l, p);
quicksort(p + 1, r);
}
}
void solve() {
std::cin >> n;
ans = 0;
a.clear();
a.resize(n + 5);
min.clear(), max.clear();
min.resize((n + 5) << 2), max.resize((n + 5) << 2);
for (int i = 1; i <= n; i++)
std::cin >> a[i];
build();
quicksort(1, n);
std::cout << ans << '\n';
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0), std::cout.tie(0);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
std::cout << std::flush;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3640kb
input:
3 3 3 2 1 5 2 4 5 3 1 10 7 2 4 6 1 9 10 8 5 3
output:
1 4 7
result:
ok 3 number(s): "1 4 7"
Test #2:
score: 0
Accepted
time: 38ms
memory: 3648kb
input:
100000 4 3 4 2 1 5 5 4 1 3 2 4 3 1 4 2 4 4 2 1 3 4 1 3 2 4 4 4 2 3 1 4 3 2 1 4 5 1 2 3 4 5 5 5 2 3 1 4 5 1 3 5 4 2 5 4 3 2 1 5 5 3 4 2 1 5 5 5 4 3 2 1 4 3 4 2 1 5 4 2 5 1 3 5 4 1 5 2 3 4 3 4 1 2 4 2 1 3 4 5 4 3 2 5 1 4 4 2 1 3 5 3 1 5 2 4 4 4 1 2 3 5 1 5 2 4 3 5 4 1 3 5 2 5 4 2 3 5 1 5 1 2 3 4 5 5 4...
output:
3 4 3 2 1 1 1 0 2 2 2 3 2 3 2 3 2 1 3 2 4 3 2 3 2 0 4 2 4 1 3 2 3 2 2 1 3 1 1 2 4 3 2 3 1 1 1 3 3 3 4 4 2 3 3 2 3 3 3 2 1 1 2 3 1 3 1 1 3 4 2 2 4 1 1 3 2 2 2 2 1 3 4 4 3 4 2 2 1 3 2 3 2 3 3 2 1 4 2 3 4 1 2 2 3 2 2 3 2 2 2 2 4 1 2 3 2 2 2 2 3 2 3 4 3 2 3 4 2 4 1 3 2 3 4 3 3 4 1 2 4 3 2 4 2 3 3 1 2 2 ...
result:
ok 100000 numbers
Test #3:
score: -100
Wrong Answer
time: 55ms
memory: 3552kb
input:
50000 10 3 1 2 10 6 8 5 4 7 9 10 8 3 9 2 10 4 5 1 7 6 9 6 8 4 9 5 7 1 3 2 9 6 7 9 3 8 5 2 1 4 10 7 10 1 2 6 5 3 9 4 8 10 1 10 4 3 2 9 7 8 5 6 9 1 5 3 4 9 6 7 2 8 10 4 7 2 8 3 6 9 5 10 1 9 6 4 9 1 8 5 2 3 7 10 5 1 7 8 10 3 9 6 2 4 9 4 8 6 3 9 7 5 2 1 9 9 1 7 6 2 3 8 5 4 10 5 7 2 1 4 3 6 8 9 10 10 9 7...
output:
8 8 8 8 9 5 3 8 8 9 7 8 7 9 8 11 6 8 8 7 7 11 7 6 11 8 3 6 8 6 6 7 10 4 8 6 6 8 5 6 7 6 7 5 5 8 9 5 9 10 6 7 8 9 4 9 6 7 8 8 9 7 5 7 6 6 10 9 8 6 4 8 6 10 4 9 9 6 9 6 7 7 7 9 6 6 9 8 7 9 9 5 6 8 8 8 8 8 6 4 8 8 5 8 8 8 9 8 8 7 8 7 6 10 9 5 7 7 6 5 5 6 8 9 5 6 7 8 6 7 9 6 7 9 7 7 6 8 9 9 7 7 9 7 8 6 ...
result:
wrong answer 11th numbers differ - expected: '9', found: '7'