poj 2566 Bound Found 尺取法
一、首先介绍一下什么叫尺取
过程大致分为四步:
1.初始化左右端点,即先找到一个满足条件的序列。
2.在满足条件的基础上不断扩大右端点。
3.如果第二步无法满足条件则到第四步,否则更新结果。
4.扩大左端点,并且回到第二步。
很明显如果要这样做,那么这个序列要是一个有顺序的序列,因为这样的话保证左端点不变,右端点一直向右延伸一定会使答案靠近结果。(可以看一道题理解一下)
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
Output
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
题意:
这道题是让找一段连续序列,保证找到的这一段序列的(所有数和的绝对值)很靠近那个输入。如果找出来的答案有多个,输出任意一个
题解:
因为这一个输入的n个数的序列不是有序的,是没有办法尺取的(可以想想为什么)
因为我们要找出来一段连续的序列,那么就可以利用前缀和。计算出前缀和之后,对他们排序就可以得到一个有序的序列
这样的话我们保证左端点不变的前提下,右端点向右扩展的话会导致这中间的差值就会越来越大,我们只需要找出来能使右端点减去左端点的值更接近答案的区间就可以(右端点减去左端点的值的几何意义就是左端点在原序列中序号和右端点在原序列中序号之间的和)
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<algorithm>
6 using namespace std;
7 const int maxn=100005;
8 const int INF=0x3f3f3f3f;
9 struct shudui
10 {
11 int first,second;
12 } p[maxn];
13 bool cmp(shudui x,shudui y)
14 {
15 return x.first<y.first;
16 }
17 int n,m;
18 void solve(int k)
19 {
20 int l = 0, r = 1, al, ar, av, minn =INF;
21 while (l<=n&&r<=n&&minn!=0)
22 {
23 int temp=p[r].first - p[l].first;
24 if (abs(temp - k) < minn)
25 {
26 minn = abs(temp - k);
27 ar = p[r].second;
28 al = p[l].second;
29 av = temp;
30 }
31 if (temp> k) //当temp>k之后就不用l++,因为之后的答案肯定不如之前的答案更优
32 l++;
33 else if (temp < k)
34 r++;
35 else
36 break;
37 if (r == l)
38 r++;
39 }
40 if(al>ar)
41 swap(al,ar);//因为al和ar大小没有必然关系()取绝对值,所以要交换
42 printf("%d %d %d\n", av, al+1, ar);
43 }
44 int main()
45 {
46 int k;
47
48 while(~scanf("%d%d",&n,&m))
49 {
50 if (!n&&!m) return 0;
51 p[0].second=p[0].first=0; //这一个初始化要放在里面
52 for (int i = 1; i <= n; i++)
53 {
54 scanf("%d", &p[i].first);
55 p[i].first += p[i - 1].first;
56 p[i].second = i;
57 }
58 sort(p, p + n + 1,cmp);
59 while (m--)
60 {
61 scanf("%d", &k);
62 solve(k);
63 }
64 }
65 return 0;
66 }
poj 2566 Bound Found 尺取法的更多相关文章
- POJ 2566 Bound Found(尺取法,前缀和)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
- poj 2566 Bound Found 尺取法 变形
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2277 Accepted: 703 Spec ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- poj 3061(二分 or 尺取法)
传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...
- POJ 3061 Subsequence ( 尺取法)
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...
- poj 3320 复习一下尺取法
尺取法(two point)的思想不难,简单来说就是以下三步: 1.对r point在满足题意的情况下不断向右延伸 2.对l point前移一步 3. 回到1 two point 对连续区间的问题求 ...
- POJ 3061 Subsequence ( 二分 || 尺取法 )
题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...
随机推荐
- 解压rpm文件
rpm2cpio zabbix-2.2.2-0.el6.zbx.src.rpm |cpio -div
- mysqlbinlog :unknown variable 'default-character-set=gbk'
mysqlbinlog :unknown variable 'default-character-set=gbk' 解决方法: 在命令行中加入--no-defaults开关,使用mysqlbinlo ...
- show engine innodb status
TRANSACTIONS------------Trx id counter 2003909(当前事务号)Purge done for trx's n:o < 2003905 (清理线程完成到了 ...
- Sklearn 与 TensorFlow 机器学习实战—一个完整的机器学习项目
本章中,你会假装作为被一家地产公司刚刚雇佣的数据科学家,完整地学习一个案例项目.下面是主要步骤: 项目概述. 获取数据. 发现并可视化数据,发现规律. 为机器学习算法准备数据. 选择模型,进行训练. ...
- 解决JS获取中文参数出现的乱码问题
在代码中增加如下js函数: function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + " ...
- Android iText向pdf模板插入数据和图片
一.需求 这些日志在写App程序,有这么一个需求,就是需要生成格式统一的一个pdf文件,并向固定表格中填充数据,并且再在pdf中追加两页图片. 二.方案 手工设计一个pdf模板,这个具体步骤就不再赘述 ...
- CSS奇思妙想 -- 使用 CSS 创造艺术
本文属于 CSS 绘图技巧其中一篇.之前有过一篇:在 CSS 中使用三角函数绘制曲线图形及展示动画 想写一篇关于 CSS 创造艺术的文章已久,本文主要介绍如何借助 CSS-doodle ,利用 CSS ...
- JavaScript的数据类型和数据类型的检测
数据类型 JavaScript的基础数据类型有,NaN string undefined Null Boolen Symbol Bigint 这些都是基础数据类 ...
- Simple decorator that intercepts connection errors and ignores these if settings specify this.
django-redis/cache.py at master · jazzband/django-redis https://github.com/jazzband/django-redis/blo ...
- assert False 与 try 结合 在开发中的使用
让错误抛出 发现其中的问题 # coding=utf-8 from rest_framework.views import exception_handler from rest_framework. ...