标签:res r++ scan main while ret clu ring names
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
int n,k;
ll t;
typedef pair<int,int>P;
P q[100005];
void solve(int t)
{
int l=0,r=1,ans=0x3f3f3f3f;
int ul,ur,sum,res;
while(r<=n)
{
sum=q[r].first-q[l].first;
if(abs(sum-t)<ans)
{
ans=abs(sum-t);
ul=q[l].second;
ur=q[r].second;
res=sum;
}
if(res<t)
r++;
else
l++;
if(l==r)
r++;
if(ul>ur)
swap(ul,ur);
}
cout<<res<<" "<<ul+1<<" "<<ur<<endl;
}
int main()
{
int num,sum;
while(scanf("%d%d",&n,&k)!=EOF&&(n+k))
{
sum=0;
q[0]=P(0,0);
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
sum+=num;
q[i]=P(sum,i);
}
sort(q,q+n+1);
while(k--)
{
scanf("%d",&t);
solve(t);
}
}
return 0;
}
标签:res r++ scan main while ret clu ring names
原文地址:http://www.cnblogs.com/renwjing/p/7300398.html