-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclists.cpp
More file actions
76 lines (72 loc) · 1.34 KB
/
Cyclists.cpp
File metadata and controls
76 lines (72 loc) · 1.34 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
AUTHOR: Antarikshya Mitra
TIME:
PROBLEM NO:- Cyclists Div 2 B
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
void solve(vector<int> &arr,int k,int p,int m)
{
int n = arr.size();
vector<int> partof(k);
for(int i=0;i<k;i++)
partof[i] = arr[i];
queue<int> q;
int track = p-1;
int total_energy = 0;
int ans = 0;
int var = arr[p-1];
if(k<n){
for(int i=k;i<n;i++){
q.push(arr[i]);
}
}
while(true){
if(track < k){
if(total_energy + var > m) break;
if(!q.empty()){
partof[track] = q.front();
q.pop();
q.push(var);
}
ans++;
track = n - 1;
total_energy += var;
}
else{
int mini = min_element(partof.begin(),partof.end()) - partof.begin();
if(total_energy+partof[mini]>=m) break;
total_energy+=partof[mini];
track--;
if(!q.empty()){
int old = partof[mini];
partof[mini] = q.front();
q.pop();
q.push(old);
}
}
}
cout<<ans<<endl;
return;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
cin>>t;
while(t--)
{
int n,k,p,m;
cin>>n>>k>>p>>m;
vector<int> arr(n);
for (auto &a:arr)
cin>>a;
solve(arr,k,p,m);
}
return 0;
}