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. 《高性能javascript》读书笔记:P1减少跨作用域的变量访问

    前端优化,有两个意义:1.为了让用户在浏览网页时获得更好的体验 2.降低服务器端的访问压力,节省网络流量. 除了换个好主机连上个千兆网这样的硬件问题,优化部分的实现方式目前也大致两种,一种是页面级别的 ...

  2. 安装使用rspec

    一,安装ruby. 二,运行命令,安装rspec的gem包: gem install rspec 会看到如下的结果: Fetching: rspec-core-2.14.7.gem (100%) Fe ...

  3. R中统计量的中英文解释

    Intercept————截距 formula————公式   Residual standard error残差标准差: 1.319 on 10 degrees of freedom 自由度为10 ...

  4. hdu 1250 Hat's Fibonacci

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...

  5. android开发遇到SDK无法访问谷歌而安装不了的情况

    遇到SDK无法访问谷歌而安装不了的情况 1.修改C:\Windows\System32\drivers\etc的HOSTS文件,添加 #google_android更新203.208.46.146 d ...

  6. 使用VMDepot镜像快速部署CKAN开放数据门户

    最新发布的CKAN VMDepot镜像针对中国用户强化了中文支持,提升了与MS Office办公软件的互操作性,并集成了常用插件和最佳实践配置参数. 使得CKAN原本十分复杂繁琐的部署流程变得非常简单 ...

  7. golang:slice陷阱

    slice陷阱,slice底层指向某个array,在赋值后容易导致array长期被引用而无法释放

  8. 13.首次安装CY7C68013A驱动失败记(结果竟然是这样)

    原文地址:首次安装CY7C68013A驱动失败记(结果竟然是这样)作者:孙茂多 今天把68013A-56焊接在CCD2的CPLD PCB上,配套的EEPROM存储器还没有焊接上,所以想用它试验一下Cy ...

  9. oracle增加表空间的四种方法,查询表空间使用情况

    增加表空间大小的四种方法Meathod1:给表空间增加数据文件ALTER TABLESPACE app_data ADD DATAFILE'D:\ORACLE\PRODUCT\10.2.0\ORADA ...

  10. 关于1>LINK : fatal error LNK1168: 无法打开 ....exe或者....dll进行写入的问题

      我们用VS编译器运行我们的程序时候,可能会出现关于1>LINK : fatal error LNK1168: 无法打开 ...dll 进行写入或者是1>LINK : fatal err ...