poj 2566 Bound Found 尺取法 变形

Bound Found

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2277   Accepted: 703   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

Ulm Local 2001

题意:给你n个数字,这些数字可正可负,再给你个数字t,求在这个数列中一个连续的子序列,和的绝对值与t相差最小

错因分析:刚开始看到这道题目觉得很变态,尺取法怎么可能做得出来,这些数字是可正可负的,,后来看了题解,才恍然大悟,正因为是可正可负所以不能直接用尺取法去解题(尺取法必须是找到单调性),需要进行转化,找到单调性,但是a[i]数组是不能改变的,因为要求连续,那么就自然想到对a[i]求前缀和

先贴上别人好的代码,核心思想:对sum进行从小到大的排序,注意需要加入<0,0>这点(为了求得单独的sum,就是从第一个节点到sum[i]对应的i节点),然后尺取法设置左右两个端点,直接在sum数组上进行尺取法,需要注意的是对两个端点移动的判断,假设得出的int temp=p[r].first - p[l].first<k,说明temp偏小,那么r端点右移,否则>k的话,说明temp偏大,需要l端点右移,但是需要注意不能出现l==r的情况,因为序列不能为空,l==r,则序列为空;还有一点很重要的是因为求得是和的绝对值,即|sum[j]-sum[i]|,那么只需要对sum数组进行从小到大排序,用大的减去小的就可以了(这也是去绝对值的体现)

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
pair<int, int > p[100005];
int n, m, k;
void solve(int k)
{
	int l = 0, r = 1, al, ar, av, minn = inf;
	  while (l<=n&&r<=n&&minn!=0)
		{
			int temp=p[r].first - p[l].first;
			if (abs(temp - k) < minn)
			{
				minn = abs(temp - k);
				ar = p[r].second;
				al = p[l].second;
				av = temp;
			}
			if (temp> k)
				l++;
			else if (temp < k)
				r++;
			else
				break;
			if (r == l)
				r++;
	   }
    if(al>ar)
        swap(al,ar);//因为al和ar大小没有必然关系()取绝对值,所以//要交换
	printf("%d %d %d\n", av, al+1, ar);
}
int main()
{
	while (~scanf("%d %d", &n, &m))
	{
		if (!n&&!m) return 0;
		p[0] = make_pair(0, 0);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &p[i].first);
			p[i].first += p[i - 1].first;
			p[i].second = i;
		}
		sort(p, p + n + 1);
		while (m--)
		{
			scanf("%d", &k);
			solve(k);
		}
	}
	return 0;
}

  下面是自己的wa代码

好好找茬

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
pair<int, int > p[100005];
int n, m, k;
void solve(int k)
{
	int l = 0, r = 1, al, ar, av, minn = inf;
	  while (l<=n&&r<=n)
		{
			int temp = p[r].first - p[l].first;
			if (abs(temp - k) < minn)
			{
				minn = abs(temp - k);
				ar = p[r].second;
				al = p[l].second;
				av = temp;
			}
			if (temp > k)
				l++;
			else if (temp < k)
				r++;
			else
				break;
			if (r == l)
				r++;
	}
	printf("%d %d %d\n", av, al+1, ar);
}
int main()
{
	while (~scanf("%d %d", &n, &m))
	{
		if (!n&&!m) return 0;
		p[0] = make_pair(0, 0);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &p[i].first);
			p[i].first += p[i - 1].first;
			p[i].second = i;
		}
		sort(p + 1, p + n + 1);
		while (m--)
		{
			scanf("%d", &k);
			solve(k);
		}
	}
	return 0;
}

  

时间: 2024-10-12 14:47:29

poj 2566 Bound Found 尺取法 变形的相关文章

poj 2566 Bound Found (前缀和,尺取法(two pointer))

Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2010   Accepted: 659   Special Judge Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration

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

题目链接:http://poj.org/problem?id=2566 题意:给定一个序列(n个整数)和一个整数k(m个),求出这个序列的一个子串,使之和的绝对值与k的差最小. 尺取法的题目有两个特性: 1. 所求的序列是一个连续的序列,这样才能将序列抽象成一个头和一个尾来描述. 2. 头尾枚举的序列满足某种单调的性质,这样才能进行尺取的操作. 这个序列是一个随意的序列,不可能直接对其进行操作,先要预处理下,进行前缀和操作,把对应的值和标记放在同一个pair. 然后根据前缀和的值进行排序,这样就

poj 2566 Bound Found(尺取法 好题)

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

poj 3320 复习一下尺取法

尺取法(two point)的思想不难,简单来说就是以下三步: 1.对r point在满足题意的情况下不断向右延伸 2.对l point前移一步 3.  回到1 two point 对连续区间的问题求解有其独到之处 复杂度为0(n) 很实用的 #include<iostream> #include<map> #include<set> #include<vector> #include<cstdio> #define inf 1000002 us

POJ 3061 Subsequence(尺取法)

传送门 Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11284 Accepted: 4694 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) ar

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

题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用先构造前缀和的方式来进行接下来的操作 ( 任意子序列的和都能由某两个前缀和的差表示 ). 二分做法 ==> 我们枚举起点,对于每一个起点 St 二分查找看看 St 后面有没有前缀和是大于或者等于 [ St的前缀和 ] + S 的,如果有说明从当前起点开始有一个终点使得起终之和是大于或者等于 S 的,

POJ - 2566 Bound Found

题意:询问一个静态序列的连续区间和绝对值最接近t的下标. 分析:由于询问的是绝对值,可以用前缀和相减得到区间和,并且和位置前后没有关系.于是把记录下标信息以后把 前缀和排序枚举大的前缀pj,pj-pi ≍ t,满足条件的:有pj-t的plower_bound以及plower_bound-1. 而pj-t也是单调的,再用一个下标i去维护就好. /********************************************************* * -----------------

POJ 2566(尺取法

自己看了半天并没有看出这题怎么用尺取法(虽然一看就觉得肯定是尺取法..),由于是绝对值,那么在计算的时候头和尾的实际位置并不重要,而应用尺取法这个数列肯定得是单调,那么我们把前缀和处理出来排序就可以直接应用尺取法了 #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<utility> #include<vector> #inc

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

http://poj.org/problem?id=2566: 题意:找连续的串,求和绝对值与所给数字最接近的串. 思路:先求下标为1的到其他下的串的值,也就是前缀和:这样可以在O(1)的时间内求出各个串的和.比如S1(1,1),S3(1,3); 那么S3-S1就是S(2,3): 然后对上面所求的前缀和按从小到大排序.(因为取的是绝对值所以abs(Sn-Sk)==abs(Sk-Sn)); 这样就可以用尺取法去做了.复杂度为O(n); 还可以用二分取找值,复杂度为O(n*log(n)); 1 #i