-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclic Array.cpp
More file actions
executable file
·57 lines (55 loc) · 976 Bytes
/
Copy pathCyclic Array.cpp
File metadata and controls
executable file
·57 lines (55 loc) · 976 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL d[1000009][20];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
LL n, k;
cin >> n >> k;
vector<LL> v(n);
for (auto &x : v) cin >> x;
v.insert(v.end(), v.begin(), v.end());
int res = 3 * n;
for (int j = 0; j < 20; j++) {
for (int i = 0; i <= 2 * n; i++) {
d[i][j] = 2 * n;
}
}
LL s = v[0];
for (int i = 0, j = 1; i < 2 * n && j < 2 * n;) {
if (s == k) {
d[i][0] = j;
s -= v[i];
s += v[j];
i++;
j++;
} else if (s > k) {
d[i][0] = j - 1;
s -= v[i];
i++;
} else {
s += v[j];
j++;
}
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i < 2 * n; i++) {
d[i][j] = d[d[i][j - 1]][j - 1];
}
}
for (int i = 0; i < n; i++) {
int x = i;
int ans = 0;
for (int j = 19; j >= 0; j--) {
if (d[x][j] - i < n) {
x = d[x][j];
ans += 1 << j;
}
}
res = min(res, ans + 1);
}
cout << res << "\n";
return 0;
}