-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.cpp
More file actions
57 lines (52 loc) · 1.06 KB
/
Partition.cpp
File metadata and controls
57 lines (52 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
#define MAX 200000
// 与えられたvectorを最後のインデックスの
// 要素の大小で分割する
int Partition(vector<int>& vec_A, int p, int r)
{
int x = vec_A[r];
int i = p - 1;
int tmp = 0;
for (int j = p; j < r; j++)
{
if (vec_A[j] <= x) {
i++;
tmp = vec_A[i];
vec_A[i] = vec_A[j];
vec_A[j] = tmp;
}
}
tmp = vec_A[i+1];
vec_A[i+1] = vec_A[r];
vec_A[r] = tmp;
return i+1;
}
int main() {
vector<int> vec;
vec.reserve(MAX);
int n = 0;
int m = 0;
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&m);
vec.push_back(m);
}
int idx = 0;
idx = Partition(vec, 0, n-1);
for (int i = 0; i < n; i++)
{
if (i == n - 1)
{
printf("%d\n", vec[i]);
} else if (i == idx) {
printf("[%d] ", vec[i]);
}
else
{
printf("%d ", vec[i]);
}
}
}