欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.51nod 1094

程序员文章站 2022-05-11 17:58:31
...

题目????1094
此代码来自百度????

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
// a[L]+a[L+1]+...a[R-1]+a[R]=k
// sum[R]-sum[L-1] = k
// 枚举L从 0 ~ n-1 是否存在 sum[R]=sum[L]+k  
using namespace std;
const int maxn=1e4+10;
typedef long long LL;
LL n,k;
bool flag;
struct Node
{
 LL p,sum;
}sum[maxn],a[maxn];
int _lowerbound (Node b[],int first, int last, const LL& val)
{
  int count,step,it;
  count = last-first;
  while (count>0)
  {
    it = first;
 step=count/2; 
 it+=step;
    if (b[it].sum<val) 
 {                 
   first=++it;
      count-=step+1;
    }
    else count=step;
  }
  return first;
}
bool cmp(const Node& x,const Node& y)
{
 return x.sum==y.sum?x.p<y.p:x.sum<y.sum;
}
void input()
{
 sum[0].p=sum[0].sum=0;
 a[0]=sum[0];
 for(int i=1;i<=n;i++)
 {
  scanf("%lld",&sum[i].sum);
  sum[i].sum=sum[i].sum+sum[i-1].sum;
  sum[i].p=i;
  a[i]=sum[i];
 }
 sort(sum+1,sum+1+n,cmp);
}
void solve()
{  
 //for(int i=0;i<=n;i++) cout<<a[i].sum<<" ";cout<<endl;
 //for(int i=0;i<=n;i++) cout<<sum[i].sum<<" ";cout<<endl;
 for(int i=0;i<=n-1;i++)
 {
  LL key=a[i].sum+k;
  int p=_lowerbound(sum,1,n+1,key);
  while(sum[p].p <i+1 && sum[p].sum==key) p++;
  if(sum[p].p>=i+1 && sum[p].p<=n && sum[p].sum==key ) 
  { 
   printf("%d %d\n",i+1,sum[p].p); 
   flag=true;
   break;
  }
 }
 if(!flag) printf("No Solution\n");
}
int main()
{
 while(scanf("%lld%lld",&n,&k)==2)
 {
  flag=false;
  input();
  solve();
 }
}