题意:

给定一个数组和一个值t,求一个子区间使得其和的绝对值与t的差值最小,如果存在多个,任意解都可行。

题目:

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

分析:

1、 什么情况下能使用尺取法?

尺取法通常适用于选取区间有一定规律,或者说所选取的区间有一定的变化趋势的情况,通俗地说,在对所选取区间进行判断之后,我们可以明确如何进一步有方向地推进区间端点以求解满足条件的区间,如果已经判断了目前所选取的区间,但却无法确定所要求解的区间如何进一步得到根据其端点得到,那么尺取法便是不可行的。。首先,明确题目所需要求解的量之后,区间左右端点一般从最整个数组的起点开始,之后判断区间是否符合条件在根据实际情况变化区间的端点求解答案。

摘自博客

(1)我在补挑战程序设计,所以明确知道这道题使用尺取法,但我对于 问题分析后,完全没看出来选取区间有啥规律(由于题目是求一个子区间使得其和的绝对值与t的差值最小,如果存在多个,任意解都可行。存在负数)所以对于区间端点的推进是无法正常进行的。
(2)对前缀和sum排序。然后这时候的sum就有单调性了,根据每次两个区间端点作差的值与目标值比较,对端点尝试更新。
(3)原问题就转化为了在一个升序区间中,取两个区间端点作差,求最接近 s 的值。
(4)需要care:
1、当左右端点相等时,此时为前缀和,没有意义且不可能出现,导致错误,所以我们要排除。
2、sort排序要带上起始的初始化零点,一是答案可能导致错误,二一个我们是用前缀和进行比较,所以不可或缺。

AC模板:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=1e5+10;
int n,m,k,l,r,x,y,ans,mi;
struct node
{int id,sum;
}s[M];
bool cmp(node x,node y)
{return  x.sum<y.sum;
}
void solve()
{l=0,r=1,mi=inf;while(l<=n&&r<=n&&mi!=0){int temp=s[r].sum-s[l].sum;if(abs(temp-k)<mi){mi=abs(temp-k);x=s[l].id;y=s[r].id;ans=temp;}if(temp>k) l++;else if(temp<k) r++;else break;if(r==l) r++;}if(x>y) swap(x,y);printf("%d %d %d\n",ans,x+1,y);
}
int main()
{while(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;s[0].sum=s[0].id=0;for(int i=1;i<=n;i++){scanf("%d",&s[i].sum);s[i].sum+=s[i-1].sum;s[i].id=i;}sort(s,s+1+n,cmp);/**要带上0项*/while(m--){scanf("%d",&k);solve();}}return 0;
}

备战ccpc分站赛ing ,题目分析简略,见谅,转载请注明出处。。。。。

Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势)相关推荐

  1. Bound Found POJ - 2566(尺取法)

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

  2. Bound Found POJ - 2566 (尺取+前缀和)

    题意 就是给一串序列 在给一个t 求一段区间的加和绝对值与t最小的值与区间左右端点是多少 思路 这道题一开始不知道如何去做 用尺取法求区间但是因为区间中存在负数没有单调性  没有特殊的特征 若对区间求 ...

  3. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  4. poj3061尺取法/前缀和 二分(java)

    今天遇到这题因为以前没见到过,当时就是想着应该有着一个很简单的方法可以过但是奈何就是没思路.后来看了别人思路写了下来.学习了尺取法 poj3061 题目介绍: Description A sequen ...

  5. 尺取法 — 详解 + 例题模板(全)

    尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.尺取法比直接暴力枚举区间效率高很多 ...

  6. ACM常用的解题技巧:尺取法

    常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.之所以需要 ...

  7. ACM常用的解题技巧:尺取法及相关例题

    常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.之所以需要 ...

  8. ACM尺取法常见题解

    大佬博客地址 常用的解题技巧:尺取法 尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答 ...

  9. NOI2016区间bzoj4653(线段树,尺取法,区间离散化)

    题目描述 在数轴上有 \(N\) 个闭区间 \([l_1,r_1],[l_2,r_2],...,[l_n,r_n]\) .现在要从中选出 \(M\) 个区间,使得这 \(M\) 个区间共同包含至少一个 ...

最新文章

  1. 百科知识 .tar.xz文件如何打开
  2. 【面向代码】学习 Deep Learning Convolution Neural Network(CNN)
  3. 生成注释_java基础- Java编程规范与注释
  4. FileProvider N 7.0 升级 安装APK 选择文件 拍照 临时权限 MD
  5. 使用Blend开发Silverlight VSM
  6. 在Windows端安装kafka提示错误:找不到或无法加载主类的解决方案
  7. 解决apache的the requested operation has failed
  8. 在Linux下如何使用GCC编译程序、简单生成静态库及动态库。
  9. 通过bindservice方式调用服务方法里面的过程
  10. 系列文章-- SSIS学习
  11. 系泊系统 matlab 代码,系泊系统的设计
  12. 具体数学_计算机科学基础(第2版)pdf
  13. php 高德地图经纬度,高德地图php 换取经纬度 地址
  14. python解决乱码转成中文
  15. pyqt5 失焦 监听无操作 定时器
  16. 基于JAVA的游戏补丁共享网站实现
  17. 工具传送门(持续更新)
  18. echarts图片的打印问题
  19. JavaScript 进阶教程(2)---面向对象实战之贪吃蛇小游戏
  20. <Rasa实战> 内容摘要(四)

热门文章

  1. Android之Canvas的drawRoundRect()
  2. IOS之Xcode之快捷键
  3. C++之用std::nothrow分配内存失败不抛异常
  4. C和C++语言编程里面常用函数或者编程技巧总结(不断更新)
  5. access 子窗体 鼠标滚动不工作_Python GUI项目实战(五)明细信息窗体的完善
  6. 吵架后女生和男生的夜晚!所有男生都这样吗?
  7. 数据时代,信息的无处遁形
  8. 什么样的程序员会让人讨厌
  9. mysql映射超_Hibernate的映射类型 hibernate mysql映射类型
  10. sql两个列值以下划线拼接得到一个新的列_面试必备sql知识点——MySQL基础