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 ...
随机推荐
- List集合实现简易学生管理
题目: 代码: package org.wlgzs; import java.util.ArrayList; import java.util.List; import java.util.Scann ...
- C# 查出数据表DataTable 清除一列中的重复项保留其他项
http://bbs.csdn.net/topics/391085792 DataTable 老表= 新表.AsEnumerable().GroupBy(p => p["姓名& ...
- npm教程_脚手架原理以及bootstrap引入
格式:vue init <templateName> <ProjectName> 例子:vue init webpack vue02 运行上面的命令后,脚手架帮忙按照webpa ...
- 【eclipse】聚合工程maven启动Tomcat报错
严重: Error configuring application listener of class严重: Skipped installing application listeners due ...
- python分享题目
目前的分享题目:1 python在云计算虚拟教室领域的应用 2 python与虚拟货币(华三工程师) 3 python在移动游戏的实践(爪子) 4 python互联网敏捷运维实践(蓝雪) 5 pyth ...
- python 同步与异步性能区别
import gevent def task(pid): """ Some non-deterministic task """ geven ...
- python 获取命令行输出结果
status, output = commands.getstatusoutput("sudo rm -rf a.txt") if(not status): print(" ...
- Python 网络编程和Socket
2017-07-24 20:43:49 Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求.Http协议主要的操作流程是req ...
- oracle创建/删除表空间、创建/删除用户并赋予权限
创建表空间 分开执行如下sql语句 --创建临时表空间 CREATE SMALLFILE TEMPORARY TABLESPACE "TEMP11" TEMPFILE 'E:\ap ...
- vs.net应用程序图标以及制作安装程序快捷方式图标设置
我们通常在开发软件完毕后需要打包制作安装程序,这个时侯一般都会需要设置应用程序的图标,使用默认的图标一般都比较难看,那么我们应该怎样设置自定义的图标呢? 我现在要讲的图标设置有两种情况, ...