和为k的连续区间

 

#include <cmath>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=10010;
int f[N];
ll s[N];
vector<int>res{10010,10010};
int main() {
    int n, k ;
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>f[i];
    for(int i=1;i<=n;i++){
        s[i]=s[i-1]+f[i];
    }
    bool flag=false;
    unordered_map<ll,int>has;
    has[0]=0;
    for(int i=1;i<=n;i++){
        if(has.find(s[i]-k)!=has.end()&&res[0]>has[s[i]-k]+1){
            flag=true;
            res={has[s[i]-k]+1,i};
        }
        if(has.find(s[i])==has.end())has[s[i]]=i;
    }
    if(!flag)cout<<"No Solution"<<endl;
    else cout<<res[0]<<" "<<res[1]<<endl;

    return 0;
}

 

posted @ 2019-07-20 14:06  YF-1994  阅读(309)  评论(0编辑  收藏  举报