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

 
按前缀和排序,尺取法解决
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define maxn 100005
#define INF 2000000000 typedef pair<int,int> pii; int n,k;
pii a[maxn]; int Abs(int x) {
return x > ? x : -x;
} void solve(int x) {
int sum = ,s = ,pos = ,v,ans = INF,l,r;
//printf("ans = %d\n",ans);
for(; s <= n && pos <= n;) {
int tem = a[pos].first - a[s].first;
//printf("tem = %d\n",tem);
if( Abs(tem - x) < ans) {
ans = Abs(tem - x);
l = a[s].second;
r = a[pos].second;
v = tem;
} if(tem > x) {
++s;
} else if(tem < x) {
++pos;
} else {
break;
}
if(s == pos) ++pos; }
if(l > r) swap(l,r); printf("%d %d %d\n",v,l + ,r);
} int main() {
// freopen("sw.in","r",stdin); while(~scanf("%d%d",&n,&k) ) {
if(!n && !k) break;
int sum = ;
a[] = pii(,);
for(int i = ; i <= n; ++i) {
int ch;
scanf("%d",&ch);
sum += ch;
a[i] = make_pair(sum,i); } sort(a,a + n + ); for(int i = ; i <= k; ++i) {
int t;
scanf("%d",&t);
solve(t);
} }
return ;
}

POJ 2566的更多相关文章

  1. 尺取法 poj 2566

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

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

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

  3. POJ 2566:Bound Found(Two pointers)

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

  4. Greedy:Bound Found(POJ 2566)

       神奇密码 题目大意:就是给你一个数组,要你找出连续的数的绝对值的和最接近t的那一串,并且要找出数组的上界和下界的下标,并显示他们的和 因为这一题的数有正有负,所以必须要先把和求出来,然后排序,然 ...

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

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

  6. POJ 2566 尺取法(进阶题)

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4297   Accepted: 1351   Spe ...

  7. poj 2566 Bound Found

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

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

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

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

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

随机推荐

  1. [笔记]--Ubuntu安装Sublime Text 2

    sublime text 2 有两种安装方式,一种是添加软件源,然后用命令安装.另外一种是下载安装包.解压手动安装.Sublime Text 2 入门及技巧 一.下载安装 1.在Sublime Tex ...

  2. 【转载】alter table move 和 alter table shrink space的区别

    move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法:  alter table TABLE_NAME shrink space [com ...

  3. [原创]EnterpriseDB测试key申请方法

    各位有对EnterpriseDB感兴趣的朋友,可以通过邮件方式申请测试key: 发送邮件至:zws@focus-soft.com,官方收到邮件后会有专人与您联系,一般情况都会很快得到一个测试key. ...

  4. Hadoop和大数据:60款顶级大数据开源工具

    一.Hadoop相关工具 1. Hadoop Apache的Hadoop项目已几乎与大数据划上了等号.它不断壮大起来,已成为一个完整的生态系统,众多开源工具面向高度扩展的分布式计算. 支持的操作系统: ...

  5. .net 使用validator做数据校验

    概述 在把用户输入的数据存储到数据库之前一般都要对数据做服务端校验,于是想到了.net自带的数据校验框架validator.本文对validator的使用方法进行介绍,并分析下校验的的原理. 使用va ...

  6. 解决Autofac MVC 自动注入在 Areas拆分到不同dll下的注入失败问题

    由于项目业务复杂,创建了多个Areas 并把他们放在了不同的项目中,项目使用AutoFac做的IOC 配置代码为 public class MvcApplication : System.Web.Ht ...

  7. 学习jax-ws(一)

    1.生成文件时提示class not find ,需要加个cp .,这样就行了 E:\mylearn\learn_webservice\learnJax-ws\bin>wsgen -cp . w ...

  8. JAVA类与对象(六)------实例变量与类变量的区别,实例方法和类方法的区别

    实例变量 实例变量声明在一个类中,但在方法.构造方法和语句块之外: 当一个对象被实例化之后,每个实例变量的值就跟着确定: 实例变量在对象创建的时候创建,在对象被销毁的时候销毁: 实例变量的值应该至少被 ...

  9. MATLAB GUI程序设计中ListBox控件在运行期间消失的原因及解决方法

    在运行期间,ListBox控件突然消失,同时给出如下错误提示: Warning: single-selection listbox control requires that Value be an ...

  10. 替换APK中的jar包文件

    [Qboy] 2014年12月21日 这几天,我第一次做的android游戏(WE!青春纪)马上就要上线.上线之前需要把各个渠道的SDK加入到我们游戏中,与渠道进行联运.但是商务很给力,一下子联系了1 ...