地址:https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264
推荐博客:
ACM常用的解题技巧:尺取法
https://blog.csdn.net/consciousman/article/details/52348439
这个博客解释的挺清楚的
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 1e5 + 5;
int a[N];
typedef struct Node{
int l,r;
friend bool operator < (const Node &p,const Node &q){
if(p.l == q.l){
return p.r < q.r;
}else{
return p.l < q.l;
}
}
}Node;
vector<Node>ve;
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i = 1;i <= n;++i){
scanf("%d",&a[i]);
}
int l = 1,r = 1;
LL ans = 0;
LL MIN = inf;
while(true){
while(r <= n && ans < m) ans += (LL)a[r++];
if(ans < m) break;
if(ans <= MIN){
if(ans < MIN){
MIN = ans;
ve.clear();
ve.pb((Node){l,r - 1});
}else{
ve.pb((Node){l,r - 1});
}
}
ans -= a[l++];
}
sort(ve.begin(),ve.end());
int len = ve.size();
for(int i = 0;i < len;++i){
printf("%d-%d\n",ve[i].l,ve[i].r);
}
return 0;
}