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. topcoder srm 620 div1

    problem1 link 分别计算可以得到(a,b)的有哪些二元组,以及可以得到(c,d)的有哪些二元组.然后在公共的二元组中找到和最大的即可. problem2 link 设最后的排序为$r=[2 ...

  2. C# MVC框架初学者

    推荐网站:http://blog.csdn.net/zhuyu19911016520/article/category/6318590

  3. luogu P2073 送花 线段树

    思路&心路 一眼认定沙比提 写的比较慢,写了1小时吧 开心的交上去 卧槽,只有20? 不服不服,拿着题解的代码去对拍 Emma,<100没问题 100000数据错了,还只是错了一个数据 ...

  4. C#学习笔记(八):多维数组

    一维数组 冒泡排序 二维数组 Length:取数组元素的总个数 GetLength:取不同维度的个数 using System; using System.Collections.Generic; u ...

  5. 【转】Makefile 中:= ?= += =的区别

    最近接触使用C++项目,需要使用Makefile,因此需要好好学习下. [转自]:http://www.cnblogs.com/wanqieddy/archive/2011/09/21/2184257 ...

  6. UVa 1629 切蛋糕(记忆化搜索)

    https://vjudge.net/problem/UVA-1629 题意: 有一个n行m列的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能直切不能拐弯.要求最后每一块蛋糕上 ...

  7. UVa 12325 宝箱

    https://vjudge.net/problem/UVA-12325 题意:有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1‘宝物2的体积为S2,价值为V2.计算出最多能装 ...

  8. Linux——系统开关机指令简单学习笔记

    关机: 命令名称:shutdown 命令所在路径:/usr/sbin/shutdown 执行权限:root 语法:shutdown 功能描述:关机 范例:# shutdown -h now 重启: 命 ...

  9. go 并发

    package main import ( "fmt" "time" ) func say(s string) { ; i < ; i++ { time. ...

  10. shell 注释

    以 # 开头的行就是注释,会被解释器忽略. 通过每一行加一个 # 号设置多行注释,像这样: #-------------------------------------------- # 这是一个注释 ...