Bound Found [POJ2566] [尺取法]
题意
给出一个整数列,求一段子序列之和最接近所给出的t。输出该段子序列之和及左右端点。
Input
Output
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] [尺取法]的更多相关文章
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- poj2566 尺取法
题意: 输入 n m 之后输入n个数 之后m个询问 对于每个询问 输入一个t 输出 三个数 ans l r 表示从l 到 r的所有数的和的绝对值最接近t 且输出这个和ans 思路: ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- POJ 2566 Bound Found(尺取法,前缀和)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
- poj 2566 Bound Found 尺取法 变形
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2277 Accepted: 703 Spec ...
- POJ 尺取法
poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...
- POJ_2566_Bound_Found_(尺取法+前缀和)
描述 http://poj.org/problem?id=2566 给出一个整数序列,并给出非负整数t,求数列中连续区间和的绝对值最接近k的区间左右端点以及这个区间和的绝对值. Bound Found ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
随机推荐
- 驱动调试(一)-printk
目录 驱动调试(一)-printk 引入 框架 入口console_setup add_preferred_console register_console s3c24xx_serial_initco ...
- python 网络编程之TCP传输&粘包传输
只有TCP有粘包现象,UDP永远不会粘包. 所谓粘包问题主要还是C/S两端数据传输时 因为接收方不知道消息之间的界限,不知道一次性提取多少字节的数据所造成的 根本原因:粘包是由TCP协议本身造成的,T ...
- ZooKeeper 概述
ZooKeeper:分布式应用程序的分布式协调服务 ZooKeeper:是用于分布式应用程序的分布式开源协调服务 可以简单理解:ZooKeeper = 文件系统 + 通知机制 从设计模式角度理解:Zo ...
- H5取经之路——CSS基础语法
一.CSS常用选择器 [选择器的命名规则] * 1.只能有字母数字下划线组成,不能有其他任何字符 * 2.开头不能是数字 [通用选择器] * 1.写法:*{} * 2.选中页面中的所有标签 * 3.优 ...
- print_r print var_dump echo区别
print_r print_r(mixed $expression [,bool $true]) 显示关于一个变量的易于理解的信息,如果给出的是string/integer/float 将打印变量值本 ...
- lnmp 1.5 mysql数据库开启远程访问
LNMP默认是禁止远程连接数据库的,但是有时候为了方便,我们想要远程,下面是开启远程的方法 sudo su # 切换为root用户模式,省的接下来操作的时候出现权限问题 mysql -u root - ...
- HDR拍照
HDR 拍照: (High Dynamic Range Imaging)高动态范围成像,是用来实现比普通数字图像技术更大曝光动态范围(即更大的明暗差别)的一组技术.高动态范围成像的目的就 ...
- Mockito框架入门教程(二)
接上一篇,继续学习其它的.... 8.找出冗余的互动(即未被验证到的) @Test(expected = NoInteractionsWanted.class) public void find_re ...
- C++入门篇三
引用:& &放在左边就是引用,放在右边就是取地址 int main() { //引用的类型必须相同,一经引用,不可以在被引用 ; int &b = a;//b引用a之后,两个同 ...
- RIDE的下载及安装
1.本机配置 windows8.1 python3.6.5,已配置环境变量 2.安装RIDE前需要安装的依赖包(使用pip就可以直接安装) 首先必须有robotframework 例如:pip ins ...