-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkstatistic.cpp
More file actions
55 lines (49 loc) · 1.06 KB
/
kstatistic.cpp
File metadata and controls
55 lines (49 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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int partition(vector<int> &vec, int l, int r){
int m = vec[(l + r) / 2];
int i = l;
int j = r;
while(i <= j){
while(vec[i] < m){
i++;
}
while(vec[j] > m){
j--;
}
if(i >= j) break;
swap(vec[i], vec[j]);
i++;
j--;
}
return j;
}
int find_k(vector<int> &vec, int l, int r, int k){
if(r == l)
return vec[l];
int m = partition(vec, l, r);
if(m - l >= k)
return find_k(vec, l, m, k);
else
return find_k(vec, m + 1, r, k - (m - l + 1));
}
void file_cin(){
freopen("kth.in", "r", stdin);
freopen("kth.out", "w", stdout);
}
int main() {
file_cin();
int n, k;
cin >> n >> k;
vector<int> vec(n);
int A, B, C, a, b;
cin >> A >> B >> C >> a >> b;
vec[0] = a;
vec[1] = b;
for(int i = 2; i < n; i++)
vec[i] = A * vec[i - 2] + B * vec[i - 1] + C;
cout << find_k(vec, 0, n - 1, k - 1);
}