POJ 2566 Bound Found(尺取法,前缀和)

时间:2022-10-13 12:26:54
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 ;
}