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

  1. 5 1
  2. -10 -5 0 5 10
  3. 3
  4. 10 2
  5. -9 8 -7 6 -5 4 -3 2 -1 0
  6. 5 11
  7. 15 2
  8. -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
  9. 15 100
  10. 0 0

Sample Output

  1. 5 4 4
  2. 5 2 8
  3. 9 1 1
  4. 15 1 15
  5. 15 1 15

Source

 
按前缀和排序,尺取法解决
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. #define maxn 100005
  10. #define INF 2000000000
  11.  
  12. typedef pair<int,int> pii;
  13.  
  14. int n,k;
  15. pii a[maxn];
  16.  
  17. int Abs(int x) {
  18. return x > ? x : -x;
  19. }
  20.  
  21. void solve(int x) {
  22. int sum = ,s = ,pos = ,v,ans = INF,l,r;
  23. //printf("ans = %d\n",ans);
  24. for(; s <= n && pos <= n;) {
  25. int tem = a[pos].first - a[s].first;
  26. //printf("tem = %d\n",tem);
  27. if( Abs(tem - x) < ans) {
  28. ans = Abs(tem - x);
  29. l = a[s].second;
  30. r = a[pos].second;
  31. v = tem;
  32. }
  33.  
  34. if(tem > x) {
  35. ++s;
  36. } else if(tem < x) {
  37. ++pos;
  38. } else {
  39. break;
  40. }
  41. if(s == pos) ++pos;
  42.  
  43. }
  44. if(l > r) swap(l,r);
  45.  
  46. printf("%d %d %d\n",v,l + ,r);
  47. }
  48.  
  49. int main() {
  50. // freopen("sw.in","r",stdin);
  51.  
  52. while(~scanf("%d%d",&n,&k) ) {
  53. if(!n && !k) break;
  54. int sum = ;
  55. a[] = pii(,);
  56. for(int i = ; i <= n; ++i) {
  57. int ch;
  58. scanf("%d",&ch);
  59. sum += ch;
  60. a[i] = make_pair(sum,i);
  61.  
  62. }
  63.  
  64. sort(a,a + n + );
  65.  
  66. for(int i = ; i <= k; ++i) {
  67. int t;
  68. scanf("%d",&t);
  69. solve(t);
  70. }
  71.  
  72. }
  73. return ;
  74. }

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. rspec的一些常见用法

    这里讲了如何安装rspec,安装使用rspec. 下面介绍一下rspec中常见的使用方法. 下面是一个最简单的测试用例,判断true是不是等于true,should_be是旧的用法,新用法推荐使用ex ...

  2. 多线程基本概论multithread

    多线程 基本概念 进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 通过 活动监视器 可以查看 Mac 系统中所开启的进程 线程 进程要想 ...

  3. C 基于UDP实现一个简易的聊天室

    引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...

  4. 10 款提高开发效率的 jQuery/CSS3 组件

    前端开发是一项十分繁琐而又耗体力的工作,如何更有效率的开发我们的应用,很多人会选择适当地使用一些jQuery插件.今天就要给大家分享10款可以提高开发效率的jQuery/CSS3组件.部分插件可以下载 ...

  5. ORA-01078、ORA-01565、ORA-17503、ORA-29701

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - ...

  6. memcached 高级机制(二)

    memcached删除机制 a) (1)有内存机制里说明了,这里会运用到LRU删除机制.我们知道,当我们在add或set一个值时,我们会设置这个值得期限.当某个值过期后,这个值并没有从内存中删除,我们 ...

  7. Redbean:入门(一) - 增删改查

    <?php require_once 'rb.php'; $tableName = "link"; //链接数据库 R::setup("mysql:host=loc ...

  8. Android greenDao的简单配置和使用

    最近自学做东西的时候用到了一个收藏的功能,然后我想把东西存放到SQLite当中,然而自己传值的时候都是用到的实体类,所以存起来也比较麻烦,所以从网上找到一个greenDao的开源框架非常火,不仅效率高 ...

  9. hdu 1269 迷宫城堡

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个 ...

  10. Objective-C-实例变量与属性的关系

    当在一个类创建一个属性,Xcode编译器就会自动产生一个带下划线的同名实例变量: 一般来说,如果getter这个属性采用下划线的方式获取效率更高,而setter采用self.属性名更加合理. 读取实例 ...