-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
65 lines (49 loc) · 1.14 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <limits>
#include <vector>
void testcase() {
int n, k;
std::cin >> n >> k;
std::vector<int> prefixSums = std::vector<int>(n + 2, 0);
for (int i = 1; i <= n; i++) {
int curr;
std::cin >> curr;
prefixSums[i] = prefixSums[i - 1] + curr;
}
prefixSums[n + 1] = prefixSums[n];
int left = 0;
int right = 1;
int bestLeft = 0;
int bestRight = 0;
int minDiff = std::numeric_limits<int>::max();
while (left <= n && right <= n) {
int currSum = prefixSums[right] - prefixSums[left];
if (currSum == k) {
// We found the lexicographically closest solution
bestLeft = left;
bestRight = right;
break;
}
int diff = std::abs(currSum - k);
// strictly smaller to keep lexicographic property
if (diff < minDiff) {
bestLeft = left;
bestRight = right;
minDiff = diff;
}
if (currSum > k) {
left++;
} else {
right++;
}
}
std::cout << bestLeft << " " << bestRight - 1 << "\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; i++) {
testcase();
}
}