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. XOR 加密简介

    本文介绍一种简单高效.非常安全的加密方法:XOR 加密. 一. XOR 运算 逻辑运算之中,除了 AND 和 OR,还有一种 XOR 运算,中文称为"异或运算". 它的定义是:两个 ...

  2. 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)

    前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...

  3. Java NIO学习笔记一 Java NIO概述

    Java NIO概述 Java NIO(新的IO)是Java的替代IO API(来自Java 1.4),这意味着替代标准的 java IO和java Networking API.Java NIO提供 ...

  4. java面试基础题(三)

    程序员面试之九阴真经 谈谈final, finally, finalize的区别: final:::修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此 ...

  5. python基础之字典dict和集合set

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...

  6. javaScript的一些奇妙动画

         今天我给大家讲一下JavaScript中的显示隐藏.淡入淡出的效果 显示与隐藏动画效果 show()方法: show()方法会动态地改变当前元素的高度.宽度和不透明度,最终显示当前元素,此时 ...

  7. 用ingress的方式部署jenkins,启动后提示没有下载插件,未解决

    [root@node2 .docker]# docker logs 5c3dd117a10dRunning from: /usr/share/jenkins/jenkins.warwebroot: E ...

  8. Spring+TaskExecutor实例

    1 taskExcutor package com.test; import org.springframework.core.task.TaskExecutor; public class Main ...

  9. jQuery Ajax封装(附带加载提示和请求结果提示模版)

    1.创建HTML文件(demo) <!doctype html> <html lang="en"> <head> <meta charse ...

  10. 浅谈Cordova框架的一些理解

    前言 因为工作原因,最近需要研究Cordova框架,看了其中的源码和实现方式,当场在看的时候马上能理解,但是事后再回去看相关源码时候却发现之前理解的内容又忘记了,又不得不重新开始看,所以总觉得需要记录 ...