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. 黑马程序员:轻松精通Java学习路线连载1-基础篇!

    编程语言Java,已经21岁了.从1995年诞生以来,就一直活跃于企业中,名企应用天猫,百度,知乎......都是Java语言编写,就连现在使用广泛的XMind也是Java编写的.Java应用的广泛已 ...

  2. Java web中常见编码乱码问题(二)

    根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析:   2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码, ...

  3. 第45篇 js操作打开本地程序

    原文地址:http://blog.laofu.online/2017/06/10/how-js-controlApp/ 背景 假设有这样一个产品,一个web和一个winform客户端,在客户在web的 ...

  4. Hadoop hdfs完全分布式搭建教程

    1.安装环境 ①.四台Linux CentOS6.7 系统 hostname                ipaddress              subnet mask             ...

  5. 修复python的ModuleNotFoundError

    我在项目里面用到了python,但其他的同事并没有安装python环境,为了不强制每个人都安装python,我下载了python-3.6.1-embed-amd64,并将用一个.bat去调用它. 大概 ...

  6. less函数

    问题描述: 在编写页面时,有以下几种按钮: 这时候我们CSS代码毫无疑问写一个公共类,然后再为每个按钮增加独自的样式如下: HTML: <div class="common a&quo ...

  7. node.js零基础详细教程(6):mongodb数据库操作

    第六章 建议学习时间4小时  课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...

  8. python+selenium遇到鼠标悬停不成功可以使用js进行操作

    问题:在定位这种悬停后出现下拉操作的时候,尝试了使用move_to_element的方法 # ele_logout = br.find_element_by_xpath('/html/body/div ...

  9. ASP.NET MVC开发学习过程中遇到的细节问题以及注意事项

    1.datagrid中JS函数传值问题: columns: { field: 'TypeName', title: '分类名称', width: 120, sortable: true, format ...

  10. ionic中应用sass

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