Description Signals of most probably extra-terrestrial origin has been received and digitalized by the aeronautic and Space administr Ation (that must is going through a defiant phase:"but I want to use feet, not meters!"). Each signal seems to comeinchParts:a sequence of n integer values and a non-negative integer t. We'll not go to details, but researchers found out of that a signal encodes the integer values. These can found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest t O T.You are given the sequence of n integers and the non-negative target T. You is to find a non-empty range of the sequence (i.e. a continuous subsequence) and output it lower index L and its UPP ER index U. The absolute value the sum of the values of the sequence fromThe l-th to the u-th element (inclusive) must is at least asClose to T asThe absolute value of the sum of any other non-empty range. Input The input file contains several test cases. Each test case starts with the 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 was 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 t His absolute sum is achieved. Possible indices start with 1 and go up to N.
Sample Input 5 1-10-5 0 5 10310 2-9 8-7 6-5 4-3 2-1 05 1115 2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-115 1000 0
Sample Output
5 4 45 2 89 1 115 1 1515 1 15
Source ULM Local 2001-foot extraction, note inf initialization1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <Set>6#include <map>7 using namespacestd;8 #defineN 1060069 #defineINF 1<<30Tenpair<int,int>G[n]; One intMain () A { - intn,k; - while(SCANF ("%d%d", &n,&k) = =2) the { - if(n==0&& k==0) - Break; - intsum=0; +g[0]=make_pair (0,0); - for(intI=1; i<=n;i++){ + intx; Ascanf"%d",&x); atsum=sum+x; -g[i]=Make_pair (sum,i); - } -Sort (g,g+n+1); - while(k--){ - in intVal; -scanf"%d",&val); to + intminn=inf; - intAns,ansl=1, ansr=1; the ints=0, t=1; * for(;;) { $ if(t>N)Panax Notoginseng Break; - if(minn==0) the Break; + intnum=g[t].first-G[s].first; A if(ABS (Num-val) <Minn) { theMinn=abs (num-val); +ans=num; -Ansl=G[s].second; $Ansr=G[t].second; $ } - - if(num<val) thet++; - if(num>val)Wuyis++; the if(s==t) -t++; Wu } - if(ansl>ANSR) About swap (ANSL,ANSR); $printf"%d%d%d\n", ans,ansl+1, ANSR); - } - - } A return 0; +}View Code |