Bound Found
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 3397 | Accepted: 1045 | Special Judge |
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个t。对于每个正整数t,求出数列中的一个子串,使子串和的绝对值最接近t。输出n,子串首尾元素在数列中的位号。
大体思路:
尺取法。
首先维护一个数据结构储存每个元素的前缀(含它本身)和以及它的位号。然后根据前缀和从小到大进行排序。然后运用尺取法就可以了。
#include<cstdio>
#include<utility>
#include<algorithm>
#define _max 100100
std::pair<int,int>s[_max];
int n,m;
/******* 尺取法 *******/
void fun (int x)
{
int i=0,j=1;//初始时做指针指向队列中第0个元素,右指针指向第1个
int l=s[i].second,r=s[j].second,ans=s[j].first-s[i].first,dis=abs(ans-x);//ans表示当前最接近x的子串和的绝对值,dis表示ans与x的差
while(j<=n&&dis!=0){//若 j>n 表示遍历结束,若dis==0表示当前ans已经等于x,已经可以作为答案输出
int t=s[j].first-s[i].first;//前缀和相减,计算当前的子串和
int d=abs(t-x);//计算当前子串和与x的差
if(d<dis) dis=d,ans=t,l=s[i].second,r=s[j].second;//如果当前子串和更接近,更新dis,ans,l,r
if(t<x) j++;//如果当前子串和偏大,则区域左端右移
if(t>x) i++;//如果当前子串和偏小,则区域右端右移
if(i==j) j++;//如果左右两端重合,右端右移
}
/****队列是按照前缀和的大小排列的,所以l与r的大小关系未定****/
if(l>r) i=l,l=r,r=i;
/****左指针需要加一****/
printf("%d %d %d\n",ans,l+1,r);
}
int main ()
{
while(scanf("%d%d",&n,&m)&&n!=0){
/****队列中第一个元素是(0,0),没有元素,前缀和(含这个数本身)为0****/
s[0]=std::make_pair(0,0);
int t,sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&t);
sum+=t;//求前缀和(含本身)
s[i]=std::make_pair(sum,i);//加入队列
}
std::sort(s,s+1+n);//按前缀和从小到大排序
while(m--){
int t;
scanf("%d",&t);//目标t
fun(t);
}
}
return 0;
}