首页 > 代码库 > poj 2566 尺取法

poj 2566 尺取法

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

poj 2566 尺取法