POJ 2566 尺取法(进阶题)

时间:2022-09-23 20:11:34
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 尺取法&lpar;基础题)

    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&lpar;尺取 &plus; 对区间和的绝对值

    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&lpar;尺取法思路题&rpar;

    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&lpar;尺取法&rpar;

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

  8. POJ 3320 &lpar;尺取法&plus;Hash&rpar;

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

  9. poj 1184 广搜进阶题

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

随机推荐

  1. 页面静态化技术Freemarker技术的介绍及使用实例&period;

    一.FreeMarker简介 1.动态网页和静态网页差异 在进入主题之前我先介绍一下什么是动态网页,动态网页是指跟静态网页相对应的一种网页编程技术.静态网页,随着HTML代码的生成,页面的内容和显示效 ...

  2. Java笔记之String

    1. String s="a",t="b"; t.concat(s); 之后,t仍然是"b",而不是"ba",要使t是& ...

  3. linux入门教程&lpar;六&rpar; Linux文件与目录管理

    在linux中什么是一个文件的路径呢,说白了就是这个文件存在的地方,例如在上一章提到的/root/.ssh/authorized_keys 这就是一个文件的路径.如果你告诉系统这个文件的路径,那么系统 ...

  4. Apache、Tomcat、JBoss、WebLogic的区别与关系

    Weblogic: 是一个企业级的应用服务器,其中包括j2ee中的各类应用如jsp,servlet,ejb等 Tomcat:   是一个初级的应用服务器,支持sp和servlet,不支持EJB,如需E ...

  5. nginx源码分析——configure脚本

    源码:nginx 1.13.0-release   一.前言      在分析源码时,经常可以看到类似 #if (NGX_PCRE) .... #endif 这样的代码段,这样的设计可以在不改动源码的 ...

  6. php学习之目录

    一. 关于php中dirname(_file_)的使用 php中定义了一个很有用的常数,即 __file__ 这个内定常数是当前php程序的就是完整路径(路径+文件名). 即使这个文件被其他文件引用( ...

  7. opencv 图片旋转

    import cv2 as cv import numpy as np # 图片旋转 img = cv.imread('../images/face.jpg', flags=1) # flags=1读 ...

  8. swift UIview上添加视频播放

    1. /// 是否显示过广告 private lazy var isLoadAV = false /// 15秒宣传视频 private var play: AVPlayer? /// 宣传视频背景 ...

  9. 运维笔记--docker高效查看后台日志

    场景描述: 应用程序运行在 Docker环境中,经常使用的查看后台日志的命令是:docker attach 容器名该命令优点:实时输出:不足之处:日志大量输出的时候,屏幕一闪而过,不便于调试,并且有一 ...

  10. 如何查找Power BI本地报表服务器产品密钥

     Power BI 报表服务器产品密钥,以便在生产环境中安装服务器. 已下载 Power BI 报表服务器,并已购买 SQL Server Enterprise 软件保障协议. 或者,已购买 Powe ...