码迷,mamicode.com
首页 > 其他好文 > 详细

和为k的连续区间

时间:2019-07-20 14:29:12      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:++   space   clu   flag   技术   end   pre   http   vector   

技术图片

 

技术图片

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

 

和为k的连续区间

标签:++   space   clu   flag   技术   end   pre   http   vector   

原文地址:https://www.cnblogs.com/programyang/p/11217570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!