题意

给出一个整数列,求一段子序列之和最接近所给出的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

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

分析

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

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

代码

 #include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<30)
#define maxn 100005
using namespace std;
int n,k;
int num[maxn];
struct P{
int id,s;
inline int operator < (const P &a)const{
return s==a.s?id<a.id:s<a.s;
}
}p[maxn];
inline int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void solve()
{
int aim=read();
int l=,r=,mn=inf,al,ar,ans;
while(l<=n&&r<=n&&mn)
{
int cal=p[r].s-p[l].s;
if(abs(cal-aim)<mn)
{
mn=abs(cal-aim);
al=p[r].id,ar=p[l].id,ans=cal;
}
if(cal>aim) ++l;
else if(cal<aim) ++r;
else break;
if(l==r) ++r;
}
if(al>ar) swap(al,ar);
printf("%d %d %d\n",ans,al+,ar);
} int main()
{
while()
{
n=read(),k=read();
if(!n&&!k) return ;
rep(i,,n) num[i]=read();
p[]=(P){,};
rep(i,,n) p[i].s=p[i-].s+num[i],p[i].id=i;
sort(p,p++n);
rep(i,,k) solve();
}
return ;
}

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. Stanford Local 2016 G "Ground Defense"(线段树)

    传送门 题意: 有 n 个城市,编号 1~n: 有两种操作:Update,Query Update: E i s a d 更新区间[ i,i+d-1 ], i 节点降落 s 人, i+1 节点降落 s ...

  2. MacBook 整个配置过程,供新入手MacBook的同学

    这里记录了MacBook 的整个配置过程,可供新入手MacBook和觉得MacBook比较难用的同学参考. 1. 外围拓展 笔记本电脑的特点是携带方便,缺点是屏幕太小,因此你首先需要再申请领用一个外接 ...

  3. 【SVN】svn 查看项目的 svn 服务器地址目录(脱机状态下)

    #事故现场: 在无法连接到svn服务器地址的情况下,查看本地项目的svn的服务器地址目录: #事故分析 因为无法连接svn服务器,所以只能通过svn在本地存储的信息来获取svn的地址路径信息: #解决 ...

  4. Entity Framework入门教程(9)---离线场景附加实体图集到上下文

    附加离线实体图集到上下文 这节主要内容是通过不同的方法将离线实体附加到上下文中. 在离线场景中,保存一个实体要略微困难一些.当我们保存一个离线的实体图集或一个单独的离线实体时,我们需要做两件事.首先, ...

  5. 常见RPC开源框架

    什么是rpc框架先回答第一个问题:什么是RPC框架? 如果用一句话概括RPC就是:远程调用框架(Remote Procedure Call) 那什么是远程调用?通常我们调用一个php中的方法,比如这样 ...

  6. VSCode CSS自动补充前缀

    1.安装AuotPrefixer. 2.代码里写css样式后,Ctrl+Shift+P,选择AutoPrefix CSS执行 结果如下

  7. 第二章 Android系统与嵌入式开发

    第二章 Android系统与嵌入式开发 第二章首先要先了解Android和嵌入式Lnux系统有什么区别和联系,嵌入式Linux系统是在嵌入式设备中运行Linux系统:Android系统是在嵌入式设备中 ...

  8. 常用window命令

    1.  关闭端口占用程序 先查看端口(8080)占用程序 netstat -ano | findstr 显示结果如下 TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1066 ...

  9. linux select 与 阻塞( blocking ) 及非阻塞 (non blocking)实现io多路复用的示例【转】

    转自:https://www.cnblogs.com/welhzh/p/4950341.html 除了自己实现之外,还有个c语言写的基于事件的开源网络库:libevent http://www.cnb ...

  10. webhook: requestbin

    A Runscope Community Project — Learn more. RequestBin Bin URL Make a request to get started. After m ...