题意

给出一个整数列,求一段子序列之和最接近所给出的t。输出该段子序列之和及左右端点。

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

分析

这道题可以看得出来是要用尺取法,尺取法的关键是要找到单调性。而序列时正时负,显然是不满足单调性的。

如果记录下前缀和,并排好序,这样就满足单调性了,就可以使用前缀和了

代码

  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<cstdio>
  7. #include<cstring>
  8. #include<iostream>
  9. #include<algorithm>
  10. #define RG register int
  11. #define rep(i,a,b) for(RG i=a;i<=b;++i)
  12. #define per(i,a,b) for(RG i=a;i>=b;--i)
  13. #define ll long long
  14. #define inf (1<<30)
  15. #define maxn 100005
  16. using namespace std;
  17. int n,k;
  18. int num[maxn];
  19. struct P{
  20. int id,s;
  21. inline int operator < (const P &a)const{
  22. return s==a.s?id<a.id:s<a.s;
  23. }
  24. }p[maxn];
  25. inline int read()
  26. {
  27. int x=,f=;char c=getchar();
  28. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  29. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  30. return x*f;
  31. }
  32.  
  33. void solve()
  34. {
  35. int aim=read();
  36. int l=,r=,mn=inf,al,ar,ans;
  37. while(l<=n&&r<=n&&mn)
  38. {
  39. int cal=p[r].s-p[l].s;
  40. if(abs(cal-aim)<mn)
  41. {
  42. mn=abs(cal-aim);
  43. al=p[r].id,ar=p[l].id,ans=cal;
  44. }
  45. if(cal>aim) ++l;
  46. else if(cal<aim) ++r;
  47. else break;
  48. if(l==r) ++r;
  49. }
  50. if(al>ar) swap(al,ar);
  51. printf("%d %d %d\n",ans,al+,ar);
  52. }
  53.  
  54. int main()
  55. {
  56. while()
  57. {
  58. n=read(),k=read();
  59. if(!n&&!k) return ;
  60. rep(i,,n) num[i]=read();
  61. p[]=(P){,};
  62. rep(i,,n) p[i].s=p[i-].s+num[i],p[i].id=i;
  63. sort(p,p++n);
  64. rep(i,,k) solve();
  65. }
  66. return ;
  67. }

Bound Found [POJ2566] [尺取法]的更多相关文章

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

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

  2. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

  3. poj2566 尺取法

    题意: 输入 n m  之后输入n个数  之后m个询问  对于每个询问 输入一个t    输出  三个数 ans l r  表示从l 到 r的所有数的和的绝对值最接近t 且输出这个和ans   思路: ...

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

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

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

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

  6. poj 2566 Bound Found 尺取法 变形

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

  7. POJ 尺取法

    poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...

  8. POJ_2566_Bound_Found_(尺取法+前缀和)

    描述 http://poj.org/problem?id=2566 给出一个整数序列,并给出非负整数t,求数列中连续区间和的绝对值最接近k的区间左右端点以及这个区间和的绝对值. Bound Found ...

  9. poj2566尺取变形

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

随机推荐

  1. 驱动调试(一)-printk

    目录 驱动调试(一)-printk 引入 框架 入口console_setup add_preferred_console register_console s3c24xx_serial_initco ...

  2. python 网络编程之TCP传输&粘包传输

    只有TCP有粘包现象,UDP永远不会粘包. 所谓粘包问题主要还是C/S两端数据传输时 因为接收方不知道消息之间的界限,不知道一次性提取多少字节的数据所造成的 根本原因:粘包是由TCP协议本身造成的,T ...

  3. ZooKeeper 概述

    ZooKeeper:分布式应用程序的分布式协调服务 ZooKeeper:是用于分布式应用程序的分布式开源协调服务 可以简单理解:ZooKeeper = 文件系统 + 通知机制 从设计模式角度理解:Zo ...

  4. H5取经之路——CSS基础语法

    一.CSS常用选择器 [选择器的命名规则] * 1.只能有字母数字下划线组成,不能有其他任何字符 * 2.开头不能是数字 [通用选择器] * 1.写法:*{} * 2.选中页面中的所有标签 * 3.优 ...

  5. print_r print var_dump echo区别

    print_r print_r(mixed $expression [,bool $true]) 显示关于一个变量的易于理解的信息,如果给出的是string/integer/float 将打印变量值本 ...

  6. lnmp 1.5 mysql数据库开启远程访问

    LNMP默认是禁止远程连接数据库的,但是有时候为了方便,我们想要远程,下面是开启远程的方法 sudo su # 切换为root用户模式,省的接下来操作的时候出现权限问题 mysql -u root - ...

  7. HDR拍照

    HDR 拍照:        (High Dynamic Range Imaging)高动态范围成像,是用来实现比普通数字图像技术更大曝光动态范围(即更大的明暗差别)的一组技术.高动态范围成像的目的就 ...

  8. Mockito框架入门教程(二)

    接上一篇,继续学习其它的.... 8.找出冗余的互动(即未被验证到的) @Test(expected = NoInteractionsWanted.class) public void find_re ...

  9. C++入门篇三

    引用:& &放在左边就是引用,放在右边就是取地址 int main() { //引用的类型必须相同,一经引用,不可以在被引用 ; int &b = a;//b引用a之后,两个同 ...

  10. RIDE的下载及安装

    1.本机配置 windows8.1 python3.6.5,已配置环境变量 2.安装RIDE前需要安装的依赖包(使用pip就可以直接安装) 首先必须有robotframework 例如:pip ins ...