-
Notifications
You must be signed in to change notification settings - Fork 0
/
1044.cpp
76 lines (67 loc) · 1.18 KB
/
1044.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
66
67
68
69
70
71
72
73
74
75
76
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int sum[100001];
struct Pair
{
int i;
int j;
Pair(int I, int J) :i(I), j(J) {};
};
int main()
{
int n, m;
scanf("%d %d", &n, &m);
// 录入的同时求和
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &sum[i]);
sum[i] = sum[i - 1] + sum[i];
}
vector<Pair> res;
int minSum = 1000000000;
// 对序列起点进行枚举
for (int i = 1; i <= n; i++)
{
int begin = i;
int end = n;
int mid;
int sumSeq;
// 二分法寻找
while (begin < end) // 因为现在找的是 >= m 的数
{
mid = (begin + end) / 2;
sumSeq = sum[mid] - sum[i - 1];
if (sumSeq >= m)
end = mid;
else
begin = mid + 1;
}
// 循环退出的条件是 begin = end
sumSeq = sum[end] - sum[i - 1];
if (sumSeq >= m)
{
if (sumSeq < minSum)
{
res.clear();
res.push_back(Pair(i, end));
minSum = sumSeq;
}
else if (sumSeq == minSum)
{
res.push_back(Pair(i, end));
}
}
else
{
break; // 当前起点找不到 >= m 的序列,再往后也不可能了
}
}
for (int i = 0; i < res.size(); i++)
{
printf("%d-%d\n", res[i].i, res[i].j);
}
return 0;
}