Bound Found
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1651   Accepted: 544   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.

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 思路:sum[i][j]=sum[0][j]-sum[0][i-1],所以可以把部分和问题转换成求两个和之间的差最接近T的问题
但是差可能有负也有正,那就把和排序一遍,这样就只能得到非负数差,可以用尺取,记录下编号小的在前就行了
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=;
int n,k,T;
typedef pair<long long ,int> P;
P sum[maxn];int nsts,nste;
long long nstt;
long long calc(int s,int e){
return sum[e].first-sum[s].first;
}
int main(){
while(scanf("%d%d",&n,&k)==&&n&&k){
long long s=;
sum[].first=;
sum[].second=;//这个不能在结果中出现,为了使得0存在而加入,是不含元素的和
for(int i=;i<=n;i++){
int tmp;
scanf("%d",&tmp);
s+=tmp;
sum[i].first=s;
sum[i].second=i;
}
nsts=nste=;nstt=sum[].first;
sort(sum,sum+n+);
for(int i=;i<k;i++){
int l=,r=;
scanf("%d",&T);
while(l<r&&r<=n){
long long tmp=calc(l,r);
if(abs(tmp-T)<abs(nstt-T)){
nstt=tmp;
nsts=min(sum[l].second,sum[r].second)+;
nste=max(sum[l].second,sum[r].second);
}
if(tmp>T&&l<r-){
l++;
}
else {
r++;
}
}
printf("%I64d %d %d\n",nstt,nsts,nste);
}
}
return ;
}

POJ 2566 Bound Found 尺取 难度:1的更多相关文章

  1. poj 2566 Bound Found 尺取法

    一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...

  2. poj 2566"Bound Found"(尺取法)

    传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...

  3. POJ 2566 Bound Found(尺取法,前缀和)

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5207   Accepted: 1667   Spe ...

  4. poj 2566 Bound Found 尺取法 变形

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2277   Accepted: 703   Spec ...

  5. poj 2566 Bound Found

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4384   Accepted: 1377   Spe ...

  6. POJ:2566-Bound Found(尺取变形好题)

    Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...

  7. Subsequence (POJ - 3061)(尺取思想)

    Problem A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...

  8. poj 2566 Bound Found(尺取法 好题)

    Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...

  9. B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值

    B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...

随机推荐

  1. 在awk中通过system调用sql语句来说明引号的使用

    一个实际应用例子: 在awk中,通过system调用连接数据库并作select操作,select语句中where条件来自于一个文件(file)的第一个域($1). $ cat file ... ... ...

  2. Python3基础 tuple 创建空元组或者只有一个元素的元组 并 用乘法成倍扩充

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. Python3基础 str while+iter+next 字符串的遍历

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. 分布式系统一致性算法Raft

    Raft 算法也是一种少数服从多数的算法,在任何时候一个服务器可以扮演以下角色之一:Leader:负责 Client 交互 和 log 复制,同一时刻系统中最多存在一个Follower:被动响应请求 ...

  5. HDU 2089 不要62 (数位DP)题解

    思路: 详解 数位DP入门题dp[pos][sta],pos代表当前位数是第几位,sta代表当前状态,因为题目中只要不出现64,所以当前状态只分为两种:前一位是6或不是. #include<io ...

  6. 【Maven】2.使用Nexus3搭建Maven私服+上传第三方jar包到本地maven仓库

    参考文章: http://www.cnblogs.com/luotaoyeah/p/3791966.html --------------------------------------------- ...

  7. Unity3d 常用的方法

    1.创建物体 2.加载物体 3.寻找物体 4.添加脚本 1.创建物体 GameObject go; // Use this for initialization void Start () { go ...

  8. hdu 5668 Circle 中国剩余定理

    Circle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  9. JSON序列化反序列化

    JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...

  10. C#学习 day1 c#基础

    C#是一门编程语言,为什么我今天开始学C#了,下学期有门C#的课,以及有一个经验丰富的老学长正在做C#项目,由于之前学过C++和C基础,所以,C#的基础部分我查看文档来尝试能否自学归纳,而不是一直依靠 ...