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

 

题意:给n和k,输入n个数和k个t,求一个连续子序列使得这个连续子序列的和最接近t,输出这个子序列的和和它的左右端点

解题思路:尺取法.要使用尺取法必须要保证数列的单调性,而这道题输入的n个数并不是单调的,然后借鉴了大佬的博客...这里用到了前缀和,预处理出前i个数的前缀和,和编号i一起放入pair中,然后根据前缀和从小到大排序,此时sum[r]-sum[l]就有了单调性。可以通过比较sum[r]-sum[l]与t的关系不断进行更新,如果sum[r]-sum[l]<t,说明和可以更大,所以r++;如果sum[r]-sum[l]>t,说明和可以更小,所以l++;如果sum[r]-sum[l]=t,必定是最小答案。由于序列不能为空,所以当l=r时,r++。更新答案的时候左右区间端点为乱序,输出的时候交换一下即可

因为前缀和不单调,所以需要先排序。在原数组开头添加0,求出前缀数组。题目即转化为在前缀数组中找pre[i],pre[j],两者之差最接近t,。对于每次找到的2个下标分别为i和j的2个数,所对应a的区间为[min(i, j) + 1, max(i, j)]。

那么前缀数组排序后,尺取法便可以求得最接近t的值。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct Node
{
int id, tot;
};
Node a[maxn];
int n, q, ans, ansl, ansr;
int cmp(Node a, Node b)
{
if (a.tot == b.tot) return a.id<b.id;
else return a.tot<b.tot;
}
int main()
{
while (scanf("%d%d", &n, &q) != EOF && (n + q))
{
a[].id = , a[].tot = ;
for (int i = ; i <= n; i++)
{
int x;
scanf("%d", &x);
a[i].id = i;
a[i].tot = a[i - ].tot + x;
}
sort(a, a + n + , cmp);
for (int i = ; i<q; i++)
{
int t;
scanf("%d", &t);
int l = , r = , minx = INF;
while (l <= r && r <= n)
{
int tmp = a[r].tot - a[l].tot;
if (abs(tmp - t)<minx)
{
minx = abs(tmp - t);
ans = tmp;
ansl = a[l].id;
ansr = a[r].id;
}
if (tmp<t) r++;
else if (tmp>t) l++;
else break;
if (l == r) r++;
}
if (ansl>ansr) swap(ansl, ansr);
printf("%d %d %d\n", ans, ansl + , ansr);
}
}
return ;
}

POJ 2566 Bound Found(尺取法,前缀和)的更多相关文章

  1. poj 2566 Bound Found 尺取法 变形

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2277   Accepted: 703   Spec ...

  2. poj 2566"Bound Found"(尺取法)

    传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...

  3. poj 2566 Bound Found 尺取法

    一.首先介绍一下什么叫尺取 过程大致分为四步: 1.初始化左右端点,即先找到一个满足条件的序列. 2.在满足条件的基础上不断扩大右端点. 3.如果第二步无法满足条件则到第四步,否则更新结果. 4.扩大 ...

  4. POJ 2566 Bound Found 尺取 难度:1

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1651   Accepted: 544   Spec ...

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

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

  6. POJ_2566_Bound_Found_(尺取法+前缀和)

    描述 http://poj.org/problem?id=2566 给出一个整数序列,并给出非负整数t,求数列中连续区间和的绝对值最接近k的区间左右端点以及这个区间和的绝对值. Bound Found ...

  7. poj 3061(二分 or 尺取法)

    传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...

  8. POJ 3061 Subsequence ( 二分 || 尺取法 )

    题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...

  9. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

随机推荐

  1. CMDB后台管理(AutoServer)

    1.表结构设计 from django.db import models class UserProfile(models.Model): """ 用户信息 " ...

  2. final方法,abstract方法和abstract类,native方法

    final方法 1.为了确保某个函数的行为在继承过程中保持不变,并且不能被覆盖(override),可以使用final方法. 2.为了效率上的考虑,将方法声明为final,让编译器对此方法的调用进行优 ...

  3. react中路由的跳转

    1.react-router-dom 使用react-router-dom 4.4.2 在页面中直接使用 引入 i mport { Link } from 'react-router-dom' 使用 ...

  4. Java API操作ZK node

    创建会话 建立简单连接 /** * 测试创建Zk会话 * Created by liuhuichao on 2017/7/25. */ public class ZooKeeper_Construct ...

  5. Vim技能修炼教程(3) - 语法高亮进阶

    语法高亮进阶 首先我们复习一下上节学到的三个命令: * syntax match用于定义正则表达式和规则的对应 * highlight default定义配色方案 * highlight link将正 ...

  6. 使用编译时注解简单实现类似 ButterKnife 的效果

    这篇文章是学习鸿洋前辈的 Android 如何编写基于编译时注解的项目 的笔记,用于记录我的学习收获. 读完本文你将了解: 什么是编译时注解 APT 编译时注解如何使用与编写 举个例子 思路 创建注解 ...

  7. ss-libev 源码解析udp篇 (4)

    本篇分析remote_recv_cb,这是整个udp转发的反方向,即读取从后端发送过来的数据再发送给前端.对于ss-server,读取到的数据是目标地址的udp服务器发送回来的响应数据,ss-serv ...

  8. ptr_fun

    ptr_fun 分类: C/C++2012-05-05 20:21 593人阅读 评论(0) 收藏 举报 functionclassfunobjectreturningtypes   目录(?)[-] ...

  9. 设置checkbox不能选中,复选框不能选中

    Web开发:设置复选框的只读效果 在Web开发中,有时候需要显示一些复选框(checkbox),表明这个地方是可以进行勾选操作的,但是有时候是只想告知用户"这个地方是可以进行勾选操作的&qu ...

  10. hdu1575 Tr A 矩阵初识

    A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= ...