Description
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We'll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t.
You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.
You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.
Input
The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n=k=0. Otherwise, 1<=n<=100000 and there follow n integers with absolute values <=10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0<=t<=1000000000.
Output
For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.
Sample Input
5 1 -10 -5 0 5 10 3 10 2 -9 8 -7 6 -5 4 -3 2 -1 0 5 11 15 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 15 100 0 0
Sample Output
5 4 4 5 2 8 9 1 1 15 1 15 15 1 15
题目意思:
给你一个长为n的序列,进行m次询问,每次询问给出一个目标值,找到序列中一段连续的子区间使得区间总和的绝对值与目标值最近。输出区间范围和区间总和。
分析:
显然对于求区间和,我们需要使用前缀数组来预处理达到O(1)求和,这里注意一下,为了不把第一个元素特殊处理,我们在0位置添加一个值为0的元素,真正的n个数存在1~n.因为长度n长达100000,如果暴力枚举区间左右端点O(n^2)肯定会超时,所以我们需要使用二指针来帮忙,但是二指针需要有序,所以需要将前缀数组升序排序。这里肯定我们需要证明一下:因为我们是求的区间和的绝对值,所以排序后,并不影响区间和绝对值的值。这样整个序列就有序了,初始左指针为0,右指针为1。如果区间和绝对值小于tar就右指针右移,大于tar就左指针右移,等于直接break。每次移动就更新一下最接近tar的区间。注意。因为要排序,所以数组每个元素不仅要记录自身的值也要记录在原序列的位置。
代码:
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef pair<int,int> int2;
int2 a[100010];
int n,m;
void slove(int tar)
{
int lp,rp,ans,ansl,ansr,data;
ans = 2000000000; //要足够大
lp = 0,rp = 1;
while(lp <= n && rp <= n)
{
int offset = a[rp].first - a[lp].first;
if(abs(offset - tar) < ans)
{
ans = abs(offset - tar);
data = offset;
ansl = a[lp].second;
ansr = a[rp].second;
}
if(offset > tar) lp ++;
else if(offset < tar) rp ++;
else break;
if(lp == rp)rp ++;
}
if(ansl > ansr)swap(ansl,ansr);
printf("%d %d %d\n",data,ansl+1,ansr);
}
bool cmp(int2 a,int2 b)
{
return a.first < b.first;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n == 0 && m == 0) break;
a[0] = int2(0,0);
int data;
for(int i = 1 ; i <= n ; i ++)
{
scanf("%d",&data);
a[i] = int2(data + a[i-1].first,i);
}
sort(a,a+n+1,cmp);
for(int i = 0 ; i < m; i ++)
{
scanf("%d",&data);
slove(data);
}
}
return 0;
}