47 lines
915 B
C++
47 lines
915 B
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
using namespace std;
|
|||
|
|
const int N = 10010;
|
|||
|
|
typedef long long LL;
|
|||
|
|
map<LL, int> s;
|
|||
|
|
LL sum;
|
|||
|
|
int n, k;
|
|||
|
|
/**
|
|||
|
|
测试用例
|
|||
|
|
6 10
|
|||
|
|
1
|
|||
|
|
2
|
|||
|
|
3
|
|||
|
|
4
|
|||
|
|
5
|
|||
|
|
6
|
|||
|
|
|
|||
|
|
答案:
|
|||
|
|
1 4
|
|||
|
|
*/
|
|||
|
|
int main() {
|
|||
|
|
cin >> n >> k;
|
|||
|
|
|
|||
|
|
int L = INT_MAX, R; // 左右端点
|
|||
|
|
bool found = false; // 是不是找到了
|
|||
|
|
int x;
|
|||
|
|
|
|||
|
|
s[0] = 0;
|
|||
|
|
for (int i = 1; i <= n; i++) {
|
|||
|
|
cin >> x; // 新输入的数据
|
|||
|
|
sum += x; // 总和
|
|||
|
|
|
|||
|
|
auto it = s.find(sum - k); // 查找前缀和为sum-k的元素
|
|||
|
|
if (it != s.end()) { // 如果找到了
|
|||
|
|
L = it->second, R = i; // 记录答案
|
|||
|
|
found = true;
|
|||
|
|
} else
|
|||
|
|
s[sum] = i; // sum-k 不存在,才会记录sum,换句话说,就是sum是唯一的
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (found)
|
|||
|
|
cout << L + 1 << " " << R << endl;
|
|||
|
|
else
|
|||
|
|
cout << "No Solution" << endl;
|
|||
|
|
|
|||
|
|
return 0;
|
|||
|
|
}
|