Files
python/TangDou/Mkx/20241125/LianXuQuJianHe.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

47 lines
915 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}