B - Bound Found(POJ2566)二分

B - Bound Found

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.

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個數,q次詢問,每次詢問給出一個t,讓你從中找出一個非空區間,使得區間和的絕對值與t最接近。

分析:

題目中重點是區間絕對值與t接近!

對尺取法有自己好的理解可以跳過

尺取法的原理:就像尺取蟲一樣,求解,《挑戰程序設計競賽》提出尺取法是建立在這樣的一個模型上:

  1. 找連續區間的問題,如果對於左端點s,第一個滿足條件的右端點是t,那麼對於左端點s+1,第一個滿足條件的右端點是t’>=t

那麼求所有的滿足條件的區間就可以像尺取蟲爬行的方式求解。

所以我們可以求出前綴和數組,對前綴和數組排序,對於一個左端點l,找出第一個右端點r,滿足區間對應的值>=t,對於左端點 l 區間和絕對值最接近t的就在此時的r和r-1處中取,只需要在r在遞增的過程中不停的更新最小值即可。

沒疑問的可以跳過

可能會產生這個疑問

找到第一個l對應的r之後,對於l+1他的右端點是r’>=r,那麼左端點逼近之後(l++)爲什麼不更新下此時的l與r-1區間是否可能是答案?

答:因爲l與r-1已經更新過答案,又因爲l+1與r-1的區間的絕對值肯定比t小,而且比l與r-1的區間的絕對值更小,所以答案不可能在l+1與r-1對應的區間。

所以l++即可,不用在此時判斷與r-1的區間的絕對值有無可能在答案。

代碼:

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+10;
const int branch=26;
const int inf=0x7fffffff;
const ll MOD=1e9+7;
struct Node{
    int val,id;
}a[maxn];
bool operator <(Node a,Node b)
{
    return a.val<b.val;
}
int main()
{
    int n,q,t;
    int nval;
    while(scanf("%d%d",&n,&q)&&(n|q))
    {
        a[0].val=0;
        a[0].id=0;
        for(int i=1;i<=n;++i)
        {
            int val;
            scanf("%d",&val);
            a[i].val=a[i-1].val+val;
            a[i].id=i;
        }
        sort(a,a+n+1);
        int al,ar,sum,l,r,minn;
        while(q--)
        {
            scanf("%d",&t);
            l=0,r=1;
            minn=inf;//區間絕對值與t之差的絕對值
            for(;;)
            {
                while(r<=n)
                {
                    nval=a[r].val-a[l].val;
                    if(abs(nval-t)<minn)
                    {
                        minn=abs(nval-t);
                        al=min(a[r].id,a[l].id)+1;
                        ar=max(a[r].id,a[l].id);
                        sum=nval;
                    }
                    if(nval<t)
                        r++;
                    else
                        break;
                }
                if(r>n)
                    break;
                else
                {
                    l++;
                    if(l==r)
                        r++;
                }
            }
            printf("%d %d %d\n",sum,al,ar);
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章