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

Source

/*
* @Author: Lyucheng
* @Date: 2017-08-02 10:22:54
* @Last Modified by: Lyucheng
* @Last Modified time: 2017-08-02 15:57:35
*/
/*
题意;给你一个序列,然后有k次查询,让你找一个子序列绝对值最接近t的序列 思路:尺取,如果想要使用尺取,要保证数列的单调性,但是序列中有负数,要给前缀排序
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> #define MAXN 100005
#define INF 2147483640
using namespace std; struct Node{
int id;
int val;
bool operator < (const Node & other) const {
return val<other.val;
}
}node[MAXN];
int n,k;
int t;
int a;
int l,r;
int res_l,res_r;
int res;
int _min; int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(scanf("%d%d",&n,&k)!=EOF){
node[].id=;
node[].val=;
for(int i=;i<=n;i++){
scanf("%d",&a);
node[i].val=node[i-].val+a;
node[i].id=i;
}
sort(node,node+n+);
while(k--){
scanf("%d",&t);
l=,r=;
_min=INF;
while(r<=n&&_min){
int pos=abs(node[r].val-node[l].val);
if(abs(pos-t)<=_min){
_min=abs(pos-t);
res=pos;
res_l=node[l].id;
res_r=node[r].id;
}
if (pos<t) r++;
if (pos>t) l++;
if (l==r) r++;
}
if(res_l>res_r) swap(res_l,res_r);
printf("%d %d %d\n",res,res_l+,res_r);
}
}
return ;
}

poj 2566 Bound Found的更多相关文章

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

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

  2. POJ 2566 Bound Found 尺取 难度:1

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1651   Accepted: 544   Spec ...

  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 尺取法

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

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

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

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

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

  8. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

  9. POJ 2566:Bound Found(Two pointers)

    [题目链接] http://poj.org/problem?id=2566 [题目大意] 给出一个序列,求一个子段和,使得其绝对值最接近给出值, 输出这个区间的左右端点和区间和. [题解] 因为原序列 ...

随机推荐

  1. 常用Linux命令、包括vi 、svn

    /etc/init.d/network restart//===========================================更新脚本cd /www/scripts更新站点./sta ...

  2. MySQL高级查询(二)

    EXISTS 和NOT EXISTS子查询 EXISTS子查询 语法:   SELECT ……… FROM 表名 WHERE EXISTS (子查询); 例: SELECT `studentNo` A ...

  3. Linux 启动详解之init

    1.init初探 init是Linux系统操作中不可缺少的程序之一.init进程,它是一个由内核启动的用户级进程,然后由它来启动后面的任务,包括多用户环境,网络等. 内核会在过去曾使用过init的几个 ...

  4. 《effective Go》读后记录

    一个在线的Go编译器 如果还没来得及安装Go环境,想体验一下Go语言,可以在Go在线编译器 上运行Go程序. 格式化 让所有人都遵循一样的编码风格是一种理想,现在Go语言通过gofmt程序,让机器来处 ...

  5. 【JVM】Java中的JavaCore/HeapDump文件及其分析方法

    产生时间 Java程序运行时,有时会产生JavaCore及HeapDump文件,它一般发生于Java程序遇到致命问题的情况下. 有时致命问题发生后,Java应用不会死掉,还能继续运行: 但有时致命问题 ...

  6. 快速双边滤波 附完整C代码

    很早之前写过<双边滤波算法的简易实现bilateralFilter>. 当时学习参考的代码来自cuda的样例. 相关代码可以参阅: https://github.com/johng12/c ...

  7. Flip Game poj 1753

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29731   Accepted: 12886 Descr ...

  8. 从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

  9. 翻译连载 | 第 10 章:异步的函数式(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  10. MYSQL数据库45道练习题

    --第一题查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from Student;--第二题查询教师所有的单位即不重复的Depa ...