Bound Found
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4297   Accepted: 1351   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

Source

题意:就是找一个连续的子区间,使它的和的绝对值最接近target, 区间内的元素可正可负。
思路:待会再补。。。
代码:
 #include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define db double
#define inf 0x3f3f3f
#define mj
//#define ll long long
#define unsigned long long ull;
using namespace std;
const int mod = ;
const int N=1e6+;
const double eps = 1e-;
typedef pair<int, int > pii;
pii p[N];
int n, m, k;
void f(int k)
{
int l = , r = , ll, rr, v, mi = inf;
while (l<=n&&r<=n&&mi!=)
{
int tmp=p[r].first - p[l].first;
if (abs(tmp - k) < mi)
{
mi = abs(tmp - k);
rr = p[r].second;
ll = p[l].second;
v = tmp;
}
if (tmp> k)
l++;
else if (tmp < k)
r++;
else
break;
if (r == l)
r++;
}
if(ll>rr) swap(ll,rr);//因为ll和rr大小没有必然关系()取绝对值,所以//要交换
printf("%d %d %d\n", v, ll+, rr);
}
int main()
{
while (scanf("%d %d", &n, &m)==,n||m)
{
p[]=make_pair(, );
for (int i = ; i <= n; i++)
{
scanf("%d", &p[i].first);
p[i].first += p[i - ].first;
p[i].second = i;
}
sort(p, p + n + );
while (m--)
{
scanf("%d", &k);
f(k);
}
}
return ;
}

POJ 2566 尺取法(进阶题)的更多相关文章

  1. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

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

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

  3. B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值

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

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

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

  5. hdu 5056(尺取法思路题)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. 【尺取法好题】POJ2566-Bound Found

    [题目大意] 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. [思路] ……前缀和比较神奇的想法.一般来说,我们必须要保证数列单调性,才能使用尺取法. 预处理出前i个 ...

  7. poj 2100(尺取法)

    Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 ...

  8. POJ 3320 (尺取法+Hash)

    题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...

  9. poj 1184 广搜进阶题

    起初的想法果然就是一个6000000的状态的表示. 但是后面觉得还是太过于幼稚了. 可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小. #include<iostream> # ...

随机推荐

  1. Buffer深入分析

    Buffer Buffer的类图如下: 除了Boolean,其他基本数据类型都有对应的Buffer,但是只有ByteBuffer才能和Channel交互.只有ByteBuffer才能产生Direct的 ...

  2. xfire调用webservice接口的实现方式

    package com.test; import java.net.URL; import org.codehaus.xfire.client.Client; import org.codehaus. ...

  3. ionic中应用sass

    在学习ionic过程中看到sass,总结了一下基本用法和问题解决办法1.首先需要一个ionic项目,并执行下面的命令ionic start CustomSass blank && cd ...

  4. Jmeter-BeanShell PostProcessor提取请求及响应结果并保存到本地文件

    1.新建一个本地csv文件,存放请求需要使用的变量值account,password,并配置CSV Data Set Config 2.添加一个HTTP请求 3.添加正则提取器用来提取响应结果中的re ...

  5. java 获得当前时间 年月日时分秒 星期几

    <%SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");//设置日期格式SimpleDat ...

  6. 大数据 Hadoop,Spark和Storm

    大数据(Big Data)   大数据,官方定义是指那些数据量特别大.数据类别特别复杂的数据集,这种数据集无法用传统的数据库进行存储,管理和处理.大数据的主要特点为数据量大(Volume),数据类别复 ...

  7. eclipse从SVN检出项目之后,项目出错

    今天公司把我分配到另一个项目组工作,然后下午使用SVN检出项目,出了问题 1.从SVN检出项目之后,要导入jar包.结果右键项目找不到Build Path,问了大牛才知道是这里的问题,一共四个步骤解决 ...

  8. OpenShitf 使用教程

    OpenShitf https://www.openshift.com 1.注册openshitf账号 https://www.openshift.com/app/account/new 2.gett ...

  9. 想从事IT行业的你,一定看看这篇文章

    很多想从事IT行业的小伙伴都会问: 我该如何学习技术? 我应该选择什么样的方向来深入学习并以此来就业? 如何证明自己的技术很牛? 什么是程序员的核心竞争力? 如何成为一名优秀的工程师? 对于这些疑问, ...

  10. 使用matplotlib绘制多轴图

    一个绘图对象(figure)可以包含多个轴(axis),在Matplotlib中用轴表示一个绘图区域,可以将其理解为子图.上面的第一个例子中,绘图对象只包括一个轴,因此只显示了一个轴(子图).我们可以 ...