-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiff_Main.cpp
More file actions
52 lines (49 loc) · 1.21 KB
/
Copy pathdiff_Main.cpp
File metadata and controls
52 lines (49 loc) · 1.21 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
diff --git a/CodeJam/Main.cpp b/examples/2021/KS_G/C/Main.cpp
index 2150379..40f7632 100644
--- a/CodeJam/Main.cpp
+++ b/examples/2021/KS_G/C/Main.cpp
@@ -1570,11 +1570,47 @@ void init(){
cin >> T;
}
+ll N, K;
+v(ll) Bi, csBi;
+
void readInput(){
+ cin >> N >> K;
+ Bi.resize(N);
+ csBi.resize(N+1);
+ forn(i, N) cin >> Bi[i];
+ csBi[0] = 0;
+ forn(i, N) csBi[i+1] = csBi[i] + Bi[i];
+}
+
+ll intsum(ll a, ll b){
+ return csBi[b]-csBi[a];
}
// write to COMM_TYPE result
void calcFunction() {
+ result = INF/2;
+
+ // smallestIntUpToA[s] denotes the length of the smallest interval from
+ // 0 to a (see following loop parameter) with sum s
+ v(ll) smallestIntUpToA(K+1, INF/2);
+ smallestIntUpToA[0] = 0;
+
+ forn(a, N+1){
+ fornn(b, a, N+1){
+ ll sum = intsum(a, b);
+ ll rest = K-sum;
+ if(rest>=0){
+ result = min(result, b-a+smallestIntUpToA[rest]);
+ }
+ }
+ forn(b, a+1){
+ ll sum = intsum(b, a);
+ if(sum<=K){
+ smallestIntUpToA[sum] = min(smallestIntUpToA[sum], a-b);
+ }
+ }
+ }
+ if(result > N) result = -1;
}
} // namespace task