forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclic Array.cpp
More file actions
43 lines (36 loc) · 767 Bytes
/
Cyclic Array.cpp
File metadata and controls
43 lines (36 loc) · 767 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+1;
int N, last[maxN];
ll K, x[2*maxN];
int main(){
scanf("%d %lld", &N, &K);
for(int i = 0; i < N; i++){
scanf("%lld", &x[i]);
x[i+N] = x[i];
}
int r = 0;
ll sum = 0;
for(int l = 0; l < N; l++){
while(r < l+N && sum + x[r] <= K){
sum += x[r];
r++;
}
last[l] = r % N;
sum -= x[l];
}
int opt = 0;
for(int i = 0; i < N; i++)
opt = last[opt];
int cnt = 1;
sum = x[opt];
for(int i = opt+1; i < N+opt; i++){
if(sum + x[i] <= K) sum += x[i];
else {
sum = x[i];
cnt++;
}
}
printf("%d\n", cnt);
}