-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
49 lines (39 loc) · 811 Bytes
/
main.cpp
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
#include <bits/stdc++.h>
void solve() {
int n, k; std::cin >> n >> k;
std::vector<int> vs;
for (int i = 0; i < n; i++) {
int v; std::cin >> v;
vs.push_back(v);
}
int opt = std::numeric_limits<int>::max();
int i_opt = -1, j_opt = -1;
int i = 0, j = 0;
int sum = vs[0];
while (j < n) {
int val = std::abs(k - sum);
if (val < opt) {
opt = val;
i_opt = i;
j_opt = j;
}
if (val == 0) break;
if (k > sum) {
j++;
if (j < n) sum += vs[j];
} else { // k < sum
sum -= vs[i];
i++;
if (i > j) {
j++;
if (j < n) sum += vs[j];
}
}
}
std::cout << i_opt << " " << j_opt << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t; std::cin >> t;
while (t--) solve();
}