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. C++(笔记)浅析vector容器的实例

    http://www.runoob.com/w3cnote/cpp-vector-container-analysis.html 转载请注明出处 浅然的专栏 https://blog.csdn.net ...

  2. UI-基本控件的简单使用

    1.IBAction:   //====================== 1> 能保证方法可以连线 2> 相当于void 2.IBOutlet: 1> 能保证属性可以连线 3.常 ...

  3. droppable放置组件

    Droppable 放置组件 所谓放置,就将一个事物入一个事物内触发各种效果,这个组件不依赖于其他组件.1.加载方式 //class 调用 <div id="dd" clas ...

  4. Kconfig介绍

    https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt

  5. React状态管理之redux

    其实和vue对应的vuex都是差不多的东西,这里稍微提一下(安装Redux略过): import { createStore, combineReducers, applyMiddleware } f ...

  6. kmeans实现文本聚类

    需求 拿到的需求是输入n个文本,对文本进行聚类,由于这些输入不能通过历史数据进行训练,所以这个主要就是用无监督学习来解决. kmeans 谈到聚类就会想到kmeans,它的核心思想是给定的K值和K个初 ...

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

    UDP relay的代码基本都在udprelay.c中,无论ss-local还是ss-server的代码都在一起,使用宏MODULE_LOCAL,MODULE_REMOTE等区分开.代码虽然不是很多, ...

  8. Ubuntu 12.04.1 OK335xS busybox-1.24.1 文件系统编译错误及解决方案

    Ubuntu OK335xS busybox- 文件系统编译错误及解决方案 一.参考文档: 编译busybox的一些错误: http://blog.csdn.net/hshl1214/article/ ...

  9. css工具类封装

    温馨提示:一下css封装,建议按需使用,否则会造成很大的代码冗余,且很多样式会造成不符合预期的效果,建议合理使用 <a href="https://meyerweb.com/eric/ ...

  10. laravel 修改重置密码模板

    Laravel里我们可以使用php artisan make:auth来生成一套默认的登陆注册重置邮箱的Authentication System,但是如何修改系统发送给用户的重置密码邮件的样式和内容 ...