POJ - 2566 Bound Found

题意:询问一个静态序列的连续区间和绝对值最接近t的下标。

分析:由于询问的是绝对值,可以用前缀和相减得到区间和,并且和位置前后没有关系。于是把记录下标信息以后把

前缀和排序枚举大的前缀pj,pj-pi ≈ t,满足条件的:有pj-t的plower_bound以及plower_bound-1

而pj-t也是单调的,再用一个下标i去维护就好。

/*********************************************************
*            ------------------                          *
*   author AbyssalFish                                   *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std;

const int maxn = 1e5+1;

int t, n, k;

typedef pair<int,int> node;
#define val first
#define idx second
node a[maxn];

int best, lb, ub;

void update(int i,int j)
{
    if(abs(a[j].val - a[i].val - t) < abs(best-t)){
        best = a[j].val - a[i].val;
        lb = a[i].idx; ub = a[j].idx;
    }
}

void solve()
{

    a[0].val = a[0].idx = 0; //空前缀是必要的
    for(int i = 1; i <= n; i++) {
        a[i].val += a[i-1].val;
        a[i].idx = i;
    }
    sort(a,a+n+1);
    while(k--){
        scanf("%d",&t);
        best = a[1].val-a[0].val, lb = a[1].idx, ub = a[0].idx; //(lb,ub]
        for(int i = 0, j = 1; j <= n; j++){
            while(i < j && a[j].val - a[i].val > t) { //[i,j]
                i++;
            }
            if(i) update(i-1,j);
            if(i < j) update(i,j);
        }
        if(ub < lb) swap(ub, lb);
        printf("%d %d %d\n", best, lb+1, ub);
    }

}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif

    while(scanf("%d%d",&n,&k),n){
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i].val);
        }
        solve();
    }
    return 0;
}
时间: 2024-10-05 03:52:50

POJ - 2566 Bound Found的相关文章

poj 2566 Bound Found (前缀和,尺取法(two pointer))

Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2010   Accepted: 659   Special Judge Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration

POJ - 2566 Bound Found(尺取法+前缀和)

题目链接:http://poj.org/problem?id=2566 题意:给定一个序列(n个整数)和一个整数k(m个),求出这个序列的一个子串,使之和的绝对值与k的差最小. 尺取法的题目有两个特性: 1. 所求的序列是一个连续的序列,这样才能将序列抽象成一个头和一个尾来描述. 2. 头尾枚举的序列满足某种单调的性质,这样才能进行尺取的操作. 这个序列是一个随意的序列,不可能直接对其进行操作,先要预处理下,进行前缀和操作,把对应的值和标记放在同一个pair. 然后根据前缀和的值进行排序,这样就

poj 2566 Bound Found 尺取法 变形

Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2277   Accepted: 703   Special Judge Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration

poj 2566 Bound Found(尺取法 好题)

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

POJ 2566:Bound Found(Two pointers)

[题目链接] http://poj.org/problem?id=2566 [题目大意] 给出一个序列,求一个子段和,使得其绝对值最接近给出值, 输出这个区间的左右端点和区间和. [题解] 因为原序列的前缀和不具有单调性,难以处理, 因此我们对前缀和进行排序,同时保留前缀和的右端点做标识作用, 题目要求区段和的绝对值最接近目标,因此排序不会造成前后顺序变化造成的影响 现在题目转化为在一个有序数列中,求一个数对,使得差值最接近给出数, 利用单调性,可以尺取解决问题. [代码] #include <

挑战程序设计竞赛3.2习题:Bound Found POJ - 2566

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

Bound Found POJ - 2566 (尺取好题)

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

Greedy:Bound Found(POJ 2566)

   神奇密码 题目大意:就是给你一个数组,要你找出连续的数的绝对值的和最接近t的那一串,并且要找出数组的上界和下界的下标,并显示他们的和 因为这一题的数有正有负,所以必须要先把和求出来,然后排序,然后利用a(s,t)=sum(t)-sum(s)找出目标 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 5 using namespace std; 6 7 //pair<i

POJ 2566 尺取法(进阶题)

Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4297   Accepted: 1351   Special Judge Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration