这题就是求最大连续子序列,并使其区间和一直大于某个数K
题目链接
双指针算法
#include<iostream>
using namespace std;
#define int long long
const int N = 200010;
int n,K;
int a[N];
signed main()
{
int T,l,r;
cin>>T;
int s;
int l1,r1;
while(T--)
{
cin>>n>>K;
l=r=1;
for(int i=1;i<=n;i++)cin>>a[i];
int res=0,sum=0;
while(l<=n&&r<=n)
{
while(r<=n&&sum+K+a[r]>=0)sum+=a[r++];
if(r-l>res)
{
res=r-l;
l1=l,r1=r-1;
}
sum-=a[l++];
}
if(!res)cout<<-1<<endl;
else cout<<l1<<" "<<r1<<endl;
}
}