poj 2566 尺取法
来源:互联网 发布:什么是波士顿矩阵 编辑:程序博客网 时间:2024/04/29 01:04
Bound Found
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 1732 Accepted: 574 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.
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 10310 2-9 8 -7 6 -5 4 -3 2 -1 05 1115 2-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -115 1000 0
Sample Output
5 4 45 2 89 1 115 1 1515 1 15
Source
Ulm Local 2001
题目是,对于给定t,求出某段连续子序列的和使得其与t的差值最小。输出该和和下标范围。也就是找出区间使得 ||Sn-Sm|-t|最小,Sn代表序列前n项和。
|Sn-Sm|应该尽可能的接近t,因为这里是取绝对值,因此n和m的大小关系可以任意。我们可以先求出前缀和,然后进行从小到大排序,对于以Si,我们要找到j>i,Sj-Si最接近t的值,用尺取法进行查找。取前后两个指针i,j。Sj-Si<t的话,因为从小到大排好序了所以令j++,使区间和不断接近t,并把出现过的最小差值记录。Sj-Si>=t或j==n时,令i++,因此此时j继续增加得到的值是单调递增的,因此必然比之前出现的差值大,所以以i为起点的可能范围已经遍历完了,取下一个起点。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;#define maxn 100005int n, k;int t;int a[maxn];struct node{ int val; int id; bool operator < (const node &a) const { return val < a.val; }} pre[maxn];int main(){ while(scanf("%d%d", &n, &k)!=EOF){ if(n+k==0) break; for(int i = 1; i <= n; i++) scanf("%d", a+i); pre[0].val = 0, pre[0].id = 0; for(int i = 1; i <= n; i++) pre[i].val = pre[i-1].val+a[i], pre[i].id = i; sort(pre, pre+n+1); for(int i = 0; i < k; i++){ scanf("%d", &t); int ll, rr, val, l, r; l = 0, r = 1, val =-(0x3f3f3f3f); while(l<r && r <= n){ int sum = pre[r].val-pre[l].val; if(abs(sum-t) < abs(val-t)){ val = sum; ll = l; rr = r; } if(sum>=t){ l++; if(l == r) r++; } else if(r == n) l++; else r++; } printf("%d %d %d\n", val, min(pre[ll].id, pre[rr].id)+1, max(pre[ll].id, pre[rr].id)); } }}
0 0
- POJ 2566 尺取法
- POJ 2566 尺取法
- poj 2566 尺取法
- poj 2566 尺取法
- poj 2566 Bound Found (尺取法)
- poj 2566 Bound Found(尺取法)
- poj 2566 Bound Found(尺取法)
- POJ - 2566 Bound Found(尺取法)
- POJ 2566 BOUND FOUND 尺取法
- Poj 2566 Bound Found(尺取法)
- POJ 2566 Bound Found (尺取法)
- poj 2566 Bound Found 尺取法 / lower_bound
- poj 2100 尺取法
- poj 3061 尺取法
- poj 3061 尺取法
- poj 3061 尺取法
- poj 3320 尺取法
- poj 3320 尺取法
- pthread_atfork解读
- 1021. 个位数统计 (15)
- 杭电1019
- POJ 2230 Watchcow (欧拉回路)
- Android adb sell中sqlite3 查出来的记录显示乱码的解决办法
- poj 2566 尺取法
- WCF BasicHttpBinding 安全解析(2)BasicHttpBinding安全项
- 木棍(贪心)
- WCF BasicHttpBinding 安全解析(3)默认安全设置(IIS宿主)
- 也谈tomcat之启动就报exception starting filter struts2 java.lang.ClassNotFoundException
- 用Swift完成不同View Controller之间的切换
- 测滑菜单MenuDrawer的使用以及解析
- 1022. D进制的A+B (20)
- Android View.VISIBLE、INVISIBLE、GONE的区别