POJ 2566 Bound Found 尺取 难度:1
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 1651 | Accepted: 544 | Special Judge |
Description
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
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 思路:sum[i][j]=sum[0][j]-sum[0][i-1],所以可以把部分和问题转换成求两个和之间的差最接近T的问题
但是差可能有负也有正,那就把和排序一遍,这样就只能得到非负数差,可以用尺取,记录下编号小的在前就行了
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=;
int n,k,T;
typedef pair<long long ,int> P;
P sum[maxn];int nsts,nste;
long long nstt;
long long calc(int s,int e){
return sum[e].first-sum[s].first;
}
int main(){
while(scanf("%d%d",&n,&k)==&&n&&k){
long long s=;
sum[].first=;
sum[].second=;//这个不能在结果中出现,为了使得0存在而加入,是不含元素的和
for(int i=;i<=n;i++){
int tmp;
scanf("%d",&tmp);
s+=tmp;
sum[i].first=s;
sum[i].second=i;
}
nsts=nste=;nstt=sum[].first;
sort(sum,sum+n+);
for(int i=;i<k;i++){
int l=,r=;
scanf("%d",&T);
while(l<r&&r<=n){
long long tmp=calc(l,r);
if(abs(tmp-T)<abs(nstt-T)){
nstt=tmp;
nsts=min(sum[l].second,sum[r].second)+;
nste=max(sum[l].second,sum[r].second);
}
if(tmp>T&&l<r-){
l++;
}
else {
r++;
}
}
printf("%I64d %d %d\n",nstt,nsts,nste);
}
}
return ;
}
POJ 2566 Bound Found 尺取 难度:1的更多相关文章
- poj 2566 Bound Found 尺取法
一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...
- 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 2566 Bound Found
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4384 Accepted: 1377 Spe ...
- POJ:2566-Bound Found(尺取变形好题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...
- Subsequence (POJ - 3061)(尺取思想)
Problem A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值
B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...
随机推荐
- Python跨平台打包
对于pyinstaller,可以完成在windows,linux,和mac下的python脚本编译,生成exe,elf,.app文件: 1.使用方法: 在pyinstaller的官网上下载,一般是源码 ...
- Farey Sequence (素筛欧拉函数/水)题解
The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...
- Java初始化块的作用
1.使代码更简洁 eg: public class Test { private void init() { System.out.println("初始化状态"); } publ ...
- 指数循环节 求A的B次方模C
phi(c)为欧拉函数, 欧拉定理 : 对于互质的正整数 a 和 n ,有 aφ(n) ≡ 1 mod n . A^x = A^(x % Phi(C) + Phi(C)) (mod C) (x & ...
- ubuntu-server14.04 网络配置
一.启动网卡 ubuntu server 安装后,ifconfig 发现只有一个lo 和一个p1p1 网卡, 先查看服务器网卡:ifconfig -a ,发现有lo ,p1p1,p2p1,p3p1,p ...
- Cassandra学习笔记
CASSANDRA在工作中用过,但是用的项目少,能用却了解的不全面.今天来稍加学习下: http://zqhxuyuan.github.io/2015/10/15/Cassandra-Daily/ ...
- Python day7_set集合的常用方法以及常用格式化的总结
1.集合的定义:集合是无序的,没有重合元素的集合 集合外使用{}符号,各元素用,连接 2.集合的常用方法 1.add增加元素 2.clear清除元素 3.copy浅拷贝 4.difference差集( ...
- Java JDK5新特性-可变参数
2017-10-31 00:19:07 可变参数:定义方法的时候不知道该定义多少个参数 格式:修饰符 返回值类型 方法名(数据类型... 变量名){} 注意:这里的变量其实是一个数组 ...
- Lua脚本语言入门学习其应用教程
Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用.不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,Ini等文件格式,并且更 ...
- 雷林鹏分享:C# 异常处理
C# 异常处理 异常是在程序执行期间出现的问题.C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零. 异常提供了一种把程序控制权从某个部分转移到另一个部分的方式.C# 异常处理时建立 ...