POJ 2566 Bound Found 尺取 难度:1

时间:2022-09-23 19:54:01
Bound Found
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1651   Accepted: 544   Special Judge

Description

Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We'll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t.

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

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 思路: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的更多相关文章

  1. poj 2566 Bound Found 尺取法

    一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...

  2. poj 2566&quot&semi;Bound Found&quot&semi;&lpar;尺取法&rpar;

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

  3. POJ 2566 Bound Found&lpar;尺取法,前缀和)

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

  4. poj 2566 Bound Found 尺取法 变形

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

  5. poj 2566 Bound Found

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4384   Accepted: 1377   Spe ...

  6. POJ:2566-Bound Found&lpar;尺取变形好题&rpar;

    Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...

  7. Subsequence (POJ - 3061)(尺取思想)

    Problem A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...

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

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

  9. B - Bound Found POJ - 2566&lpar;尺取 &plus; 对区间和的绝对值

    B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...

随机推荐

  1. var a&equals;b&equals;c&equals;1&semi; 和 var a&equals;1&comma; b&equals;2&comma; c&equals;3&semi; 的区别。

    function test(){ var a=b=c=1; var a=1, b=2,c=3; } 1中b\c 为全局变量, a为私量 2中a\b\c为私量

  2. JavaEE基础(二十二)&sol;IO流

    1.IO流(序列流) 1.什么是序列流 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推. 2.使用方式 整合两个: ...

  3. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  4. 代码重启SQL命令

    1.net stop mssqlserver --停止mssqlserver 2.net start mssqlserver  --开始mssqlserver

  5. Spring总结&lowbar;02&lowbar;Spring概述

    一.概念准备 1.应用程序:是能完成我们所需要功能的成品,比如购物网站.OA系统. 2.框架:是能完成一定功能的半成品,比如我们可以使用框架进行购物网站开发:框架做一部分功能,我们自己做一部分功能,这 ...

  6. 【spring源码分析】IOC容器初始化(五)

    前言:前几篇文章已经将BeanDefinition的加载过程大致分析完成,接下来继续分析其他过程. AbstractApplicationContext#refresh public void ref ...

  7. 利用反射将IDataReader读取到实体类中效率低下的解决办法

    最开始使用反射一个类型的各个属性,对气进行赋值的代码如下: public static List<T> ToList<T>(IDataReader reader) { //实例 ...

  8. 一对一关联关系基于主键映射的异常 IdentifierGenerationException

    具体异常:org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one pro ...

  9. thinkphp3&period;2&period;3 HTML 页面跳转

    1.   http://域名/index.php(入口文件)/模块/控制器/方法 2.{:U('控制器/方法')}

  10. 微信小程序 入门

    目录结构: app.json  .小程序的全局配置 pages:  当前小程序所有页面路径. window:小程序所有页面的顶部背景颜色,文字颜色定义在这里. tabBar:  设置底部 tab ne ...