QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#189174#5143. Quick SortzhyljWA 44ms3696kbC++142.0kb2023-09-26 23:05:172023-09-26 23:05:18

Judging History

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

  • [2023-09-26 23:05:18]
  • 评测
  • 测评结果:WA
  • 用时:44ms
  • 内存:3696kb
  • [2023-09-26 23:05:17]
  • 提交

answer

#include <bits/stdc++.h>

#define gc() getchar()
template <typename T> inline void rd(T& x) {
	int si = 1; char c = gc(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = gc();
	while(isdigit(c)) x = x * 10 + c - 48, c = gc();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double ff;
typedef std::pair <int, int> pii;
const int N = 5e5 + 5, INF = 0x3f3f3f3f;

int n, ans, p[N], q[N];

void Swap(int i, int j) {
	std::swap(p[i], p[j]);
	std::swap(q[p[i]], q[p[j]]);
	++ans;
}
int Partition(int l, int r) {
	int mid = p[(l + r) / 2];
	std::vector <int> a, b;
	if(mid < (l + r) / 2) {
		for(int i = l; i <= mid; ++i)
			if(p[i] > mid) a.push_back(i);
		for(int i = l; i < mid; ++i)
			if(q[i] >= mid) b.push_back(q[i]);
	} else {
		for(int i = mid; i <= r; ++i)
			if(p[i] < mid) b.push_back(i);
		for(int i = mid + 1; i <= r; ++i)
			if(q[i] <= mid) a.push_back(q[i]);
	}
	std::sort(a.begin(), a.end());
	std::sort(b.rbegin(), b.rend());
	a.push_back(INF); b.push_back(-INF);
	int i = l - 1, j = r + 1, _i = 0, _j = 0;
	while(true) {
		++i; --j;
		while(a[_i] < i) ++_i;
		while(b[_j] > j) ++_j;
		i = std::min(a[_i], q[mid] >= i ? q[mid] : INF);
		j = std::max(b[_j], q[mid] <= j ? q[mid] : -INF);
		if(i >= j)
			return q[mid] <= mid ? mid : mid - 1;
		Swap(i, j);
	}
}
void QuickSort(int l, int r) {
	if(l < r) {
		int pos = Partition(l, r);
		QuickSort(l, pos);
		QuickSort(pos + 1, r);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int test_case_cnt = 1; rd(test_case_cnt);
	while(test_case_cnt--) {
		ans = 0;
		rd(n);
		for(int i = 1; i <= n; ++i) {
			rd(p[i]); q[p[i]] = i;
		}
		QuickSort(1, n);
		printf("%d\n", ans);
	} return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3624kb

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: 36ms
memory: 3696kb

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: 44ms
memory: 3624kb

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
9
6
8
8
7
7
11
7
6
9
8
3
6
8
6
6
7
8
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
11
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
7
...

result:

wrong answer 11th numbers differ - expected: '9', found: '7'