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

 
尺取法,注意 inf 初始化
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
#define N 106006
#define inf 1<<30
pair<int,int> g[N];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)==)
{
if(n== && k==)
break;
int sum=;
g[]=make_pair(,);
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
sum=sum+x;
g[i]=make_pair(sum,i);
}
sort(g,g+n+);
while(k--){ int val;
scanf("%d",&val); int minn=inf;
int ans,ansl=,ansr=;
int s=,t=;
for(;;){
if(t>n)
break;
if(minn==)
break;
int num=g[t].first-g[s].first;
if(abs(num-val)<minn){
minn=abs(num-val);
ans=num;
ansl=g[s].second;
ansr=g[t].second;
} if(num<val)
t++;
if(num>val)
s++;
if(s==t)
t++;
}
if(ansl>ansr)
swap(ansl,ansr);
printf("%d %d %d\n",ans,ansl+,ansr);
} }
return ;
}

poj 2566 Bound Found(尺取法 好题)的更多相关文章

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

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

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

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

  3. poj 2566 Bound Found 尺取法 变形

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

  4. poj 2566 Bound Found 尺取法

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

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

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

  6. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  7. hdu 5056(尺取法思路题)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. poj 3061(二分 or 尺取法)

    传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...

  9. 【尺取法好题】POJ2566-Bound Found

    [题目大意] 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. [思路] ……前缀和比较神奇的想法.一般来说,我们必须要保证数列单调性,才能使用尺取法. 预处理出前i个 ...

随机推荐

  1. 【自学iOS攻略】了解有什么新技术的途径

    1.了解有什么新技术1> 苹果API文档 - General - Guides - iOSx API Diffs2> 观看WWDC会议视频 2.如何使用新技术1> 自己根据API文档 ...

  2. 10个利用Eclipse调试Java的常见技巧

    http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...

  3. 【Android】Activity的菜单机制和方法解析

    Activity有一套机制来实现对菜单的管理,方法如下: 1. 初始化菜单 public boolean onCreateOptionsMenu(Menu menu) 此方法用于初始化菜单,其中men ...

  4. 酷Q机器人,QQ机器人使用教程

    软件介绍: 酷Q,软件酷Q机器人是一款基于webqq开发的一款自动接收.处理qq消息的软件. 改程序使用易语言编写,精简大量不必要代码,减小了软件体积,优化程序速度,使得酷Q更加轻巧好用. 在消息处理 ...

  5. 【转】在写一个iOS应用之前必须做的7件事(附相关资源)

    转自:http://www.cocoachina.com/ios/20160316/15687.html 本文由CocoaChina--不再犹豫(tao200610704@126.com)翻译 作者: ...

  6. Facebook登录 AndroidStudio

    简介 主页:https://www.facebook.com/ Android开发者支持:https://developers.facebook.com/docs/android/  应用管理:htt ...

  7. .NET基础拾遗(7)多线程开发基础3

    一.如何使用异步模式? 异步模式是在处理流类型时经常采用的一种方式,其应用的领域相当广阔,包括读写文件.网络传输.读写数据库,甚至可以采用异步模式来做任何计算工作.相对于手动编写线程代码,异步模式是一 ...

  8. 前端bug记录---不定时更新

    在项目的开发中难免遇到各种各样的bug,我觉得还是有必要记录一下的,方便日后查询. safari window resize  为满足日常轮播需求,做一个符合当前业务的轮播插件,其中需要考虑windo ...

  9. 【socket.io研究】0.前提准备

    WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...

  10. AngularJs练习Demo13Provider_factory

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...