Bound Found POJ - 2566(尺取法)
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
题意:找出一段连续的序列,使得这个序列的和的绝对值和t的差值最小。输出这段区间的绝对值和区间的左右端点。
思路:尺取的前提条件就是序列必须是单调的,否则前进和后退根本找不出规律来,就没有办法尺取了。但是如果堆这个数组排序的话,就不能保证这是连续的了。我们先把这段序列的前缀和数组求出来,然后将前缀和出现的序列号一同保存下来,按照由小到大排序。这样序列就是单调的了。然后两个指针开始尺取,如果当前区间求出的和小于t的话,说明还可以变大一点,那么就r++;如果当前区间求出的和大于t的话,说明还可以变小一点,那么就l++。注意这是前缀和,l和r不能相同。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;const int maxx=1e5+100;
struct node{int num;int pos;bool operator<(const node &a)const{return num<a.num;}
}p[maxx];
int a[maxx];
int n,m;int main()
{while(~scanf("%d%d",&n,&m)&&(n+m)){p[0].num=0;p[0].pos=0;//这里需要解释一下,因为这是前缀和,不是原来的数组,添加一个0是因为有可能从头开始取元素,那样的话,p[l].num需要为0才可以。for(int i=1;i<=n;i++) {scanf("%d",&a[i]);p[i].num=p[i-1].num+a[i];p[i].pos=i;}sort(p,p+1+n);//从0开始排序。while(m--){int t;scanf("%d",&t);int ans=inf,Ans;int L=0,R=0;int l=0,r=1;while(r<=n){int res=p[r].num-p[l].num;if(abs(res-t)<ans){ans=abs(res-t);Ans=res;L=p[l].pos;R=p[r].pos;}if(res<t) r++;else if(res>t) l++;else break;if(l==r) r++;}if(L>R) swap(L,R);printf("%d %d %d\n",Ans,L+1,R);}}return 0;
}
尺取就是需要多思考。
努力加油a啊,(o)/~
Bound Found POJ - 2566(尺取法)相关推荐
- Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势)
题意: 给定一个数组和一个值t,求一个子区间使得其和的绝对值与t的差值最小,如果存在多个,任意解都可行. 题目: Signals of most probably extra-terrestrial ...
- Bound Found POJ - 2566 (尺取+前缀和)
题意 就是给一串序列 在给一个t 求一段区间的加和绝对值与t最小的值与区间左右端点是多少 思路 这道题一开始不知道如何去做 用尺取法求区间但是因为区间中存在负数没有单调性 没有特殊的特征 若对区间求 ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- 尺取法 POJ 3601 Subsequence
题目传送门 1 /* 2 题意:求连续子序列的和不小于s的长度的最小值 3 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 4 1. 记录前i项的总和,求[i, p)长度的最小值,用二分 ...
- POJ 2566 Bound Found
题意 : 给你n个数字,这些数字可正可负,再给你个数字t, 求在这个数列中一个连续的子序列,和的绝对值 与t相差最小: 数据范围较大, 考虑数字没有负数的情况, 能够想到用尺取法解决, (关于尺取法, ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 1 /* 2 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 3 */ 4 #include <cstdio> 5 #include &l ...
- poj 3061(尺取法)
从长为n的全为正数的数组a中,找到个数最少的连续子数列使其和>=m example: n=10,m=15,a[10]={5 1 3 5 10 7 4 9 2 8},结果为2 解题思路:昨天做了一 ...
- 解题报告 (十三) 尺取法
文章目录 尺取法 解题报告 PKU 2100 Graveyard Design PKU 3061 Subsequence PKU 2739 Sum of Consecutive Prime Numbe ...
最新文章
- HTML5 Canvas 绘制库存变化折线 增加超储告罄线
- bubbliiiing/keras-face-recognition
- 试说明机器指令和微指令之间的关系_男女关系之间,是他在说谎吗?观察他的肢体语言说明一切...
- java中打印输出数组内容的三种方式
- 企业安全建设之浅谈数据防泄露
- 计算机诞生发展分类特点及应用,计算机的诞生与发展,及其特点
- JMS-ActiveMQ学习-3 ActiveMQ与Spring集成
- zoj 1366 Cash Machine
- 小米12 Ultra有望春节后登场:主打影像升级 或与徕卡联名
- JHelpers——一个善良忠实的仆人
- gstat | 空间插值(二)——克里金插值之普通克里金
- nova创建instance流程
- 洛谷P3386 【模板】二分图匹配
- 替代left join方法_你应该使用pathlib替代os.path
- (Trie树)leetcode208: Implement Trie,79:Word Search,DFS与BFS(python实现),212:Word Search2...
- FRR BGP协议分析12 -- ZEBRA路由的处理1
- Coverity软件下载安装使用试用
- 如何批量将PNG格式转化为JPG格式
- 滴滴开源的损失!章文嵩将离职,曾是阿里开源“赶集人”,投身开源 20 年
- python如何定义函数k_Python 函数
热门文章
- c++中调用Com组件的方法详解
- linux 运行cmd文件,cmd文件如何在虚拟linux下运行
- php 单例模式原理,PHP单例模式demo详解
- pycharm同一目录下无法import明明已经存在的.py文件
- kali Linux 源更新
- cygwin swoole_swoole入门--------基础概念
- ViewPager+Fragment 组合的预加载和懒加载
- Android开发之快捷键Google官方版本包含Mac版本
- 打字机已经被计算机所取代用英语,无法被电脑所取代的职业
- iphone ios编译ffmpeg