题目描述

In the Byteotian Training Centre, the pilots prepare for missions requiring extraordinary precision and control.

One measure of a pilot's capability is the duration he is able to fly along a desired route without deviating too much - simply put, whether he can fly steadily. This is not an easy task, as the simulator is so sensitive that it registers even a slightest move of the yoke1.

At each moment the simulator stores a single parameter describing the yoke's position.

Before each training session a certain tolerance level  is set.

The pilots' task then is to fly as long as they can in such a way that all the yoke's position measured during the flight differ by at most . In other words, a fragment of the flight starting at time  and ending at time  is within tolerance level  if the position measurements, starting with -th and ending with -th, form such a sequence  that for all elements  of this sequence, the inequality  holds.

Your task is to write a program that, given a number  and the sequence of yoke's position measurements, determines the length of the longest fragment of the flight that is within the tolerance level .

给定n,k和一个长度为n的序列,求最长的最大值最小值相差不超过k的序列

输入输出格式

输入格式:

In the first line of the standard input two integers are given,  and  (), separated by a single space, denoting the tolerance level and the number of yoke's position measurements taken.

The second line gives those measurements, separated by single spaces. Each measurement is an integer from the interval from  to .

第一行两个有空格隔开的整数k(0<=k<=2000,000,000),n(1<=n<=3000,000),k代表设定的最大值,n代表序列的长度。第二行为n个由空格隔开的整数ai(1<=ai<=2000,000,000),表示序列。

输出格式:

Your program should print a single integer to the standard output:

the maximum length of a fragment of the flight that is within the given tolerance level.

一个整数代表最大的符合条件的序列

输入输出样例

输入样例#1: 复制

3 9
5 1 3 5 8 6 6 9 10

输出样例#1: 复制

4

说明

样例解释:5, 8, 6, 6 和8, 6, 6, 9都是满足条件长度为4的序列




单调队列有顺序性

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 #define ll long long
 5 ll k,n,a[3000005],q_mx[3000005],q_mn[3000005];
 6 ll head_mx,head_mn,tail_mx,tail_mn,len,pre;
 7 int main(){
 8 //    freopen("1.in","r",stdin);
 9     scanf("%lld%lld",&k,&n);
10     len=0;
11     for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
12     q_mx[1]=1;q_mn[1]=1;pre=1;
13     head_mx=1;tail_mx=1;head_mn=1;tail_mn=1;
14     for(int i=2;i<=n;i++){
15         while(head_mx<=tail_mx&&a[q_mx[tail_mx]]<a[i])tail_mx--;
16         while(head_mn<=tail_mn&&a[q_mn[tail_mn]]>a[i])tail_mn--;
17         q_mx[++tail_mx]=i;q_mn[++tail_mn]=i;
18         while(a[q_mx[head_mx]]-a[q_mn[head_mn]]>k){
19             if(q_mx[head_mx]<q_mn[head_mn]){pre=q_mx[head_mx]+1;head_mx++;}
20             else {pre=q_mn[head_mn]+1;head_mn++;}
21         }
22         //pre=min(q_mx[head_mx],q_mn[head_mn]);
23         len=max(len,i-pre+1);
24     }
25     printf("%lld",len);
26 }

二分:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<deque>
 6 using namespace std;
 7 const int MAXN=3000010;
 8 inline void read(int &n)
 9 {
10     char c=getchar();n=0;bool flag=0;
11     while(c<'0'||c>'9')    c=='-'?flag=1,c=getchar():c=getchar();
12     while(c>='0'&&c<='9')    n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;
13 }
14 struct node
15 {
16     int pos,val;
17     node(){pos=val=0;}
18     node(int a,int b)    {    pos=a;val=b;    }
19 };
20 int a[MAXN];
21 int n,k;
22 int check(int mid)//按顺序的性质二分
23 {
24     deque<node>qmax;// 升
25     deque<node>qmin;// 降
26     for(int i=1;i<=n;i++)
27     {
28         while(qmax.size()>0&&qmax.front().pos<=(i-mid))    qmax.pop_front();
29         while(qmin.size()>0&&qmin.front().pos<=(i-mid))    qmin.pop_front();
30         while(qmax.size()>0&&a[i]>qmax.back().val)    qmax.pop_back();
31         qmax.push_back(node(i,a[i]));
32         while(qmin.size()>0&&a[i]<qmin.back().val)    qmin.pop_back();
33         qmin.push_back(node(i,a[i]));
34     //    printf("%d %d\n",qmax.front().val,qmin.front().val);
35         if(i>=mid&&qmax.front().val-qmin.front().val<=k)
36             return 1;
37     }
38     return 0;
39 }
40 int main()
41 {
42     read(k);read(n);
43     for(int i=1;i<=n;i++)    read(a[i]);
44     int l=1,r=n;
45     int ans=0;
46     while(l<=r)
47     {
48         int mid=l+r>>1;
49         if(check(mid))    ans=mid,l=mid+1;
50         else r=mid-1;
51     }
52     printf("%d",ans);
53     return 0;
54 }

转载于:https://www.cnblogs.com/zhangbuang/p/10312866.html

P3512 [POI2010]PIL-Pilots(单调队列+二分)相关推荐

  1. P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)

    P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...

  2. CodeForces - 91B Queue(单调队列+二分)

    题目链接:点击查看 题目大意:给出一个队列,队列中按照顺序有n只海豹在排队,每一只海豹都有一个数值表示年龄,如果在某只前面有年龄比他小的海豹,他会变得很不开心,不开心的值就是在比他年龄小的海豹中选择距 ...

  3. jzoj3169-[GDOI2013模拟4]生产汽车【斜率优化dp,单调队列,二分】

    正题 题目大意 有nnn个人mmm辆车. 人有tit_iti​,车有fjf_jfj​.第i个人修第j俩车时间是ti∗fjt_i*f_jti​∗fj​. 一辆车要每个人都修一遍,且一个人修好后要求下一个 ...

  4. BZOJ 1012 单调队列+二分

    思路: 维护一个单减的序列 序号是单增的 每回二分查找第一个比询问的大的值 我手懒 用得lower_bound //By SiriusRen #include <cstdio> #incl ...

  5. DP_单调队列优化 DP_绿色通道

    题目:绿色通道 做法:二分答案+动态规划+单调队列 二分答案:二分不超过的空题长度,并计算再该情况下的所用时间最小值,如果还有时间则缩短空题长度,否则增加. 状态表示:f[i]f[i]f[i] 表示写 ...

  6. 点分治问题 ----------- luoguP2942 [WC2010]重建计划 [点分治 + bfs + 单调队列 + 预处理建树 + 二分 + 01分数规划]

    题目链接 解题思路: 1.对于这个Avgvalue=∑e∈sv(e)∣s∣Avgvalue = \frac{\sum_{e\in s}v(e)}{|s|}Avgvalue=∣s∣∑e∈s​v(e)​ ...

  7. 【题解】P1419 寻找段落(二分+单调队列)难度⭐⭐⭐★

    P1419 寻找段落 首先二分答案,即:二分最大平均值. 我们将a全部减去mid,问题转化为判断是否存在一个长度在s~t范围内的区间它的和为正,如果有说明还有更大的平均值. 用前缀和和单调队列维护. ...

  8. luoguP1419 寻找段落(二分答案+单调队列)

    题意 给定一个长度为n的序列a1~an,从中选取一段长度在s到t之间的连续一段使其平均值最大.(n<=100000) 题解 二分答案平均值. judge时把每一个a[i]-mid得到b[i] 在 ...

  9. BZOJ 2806 Luogu P4022 [CTSC2012]Cheat (广义后缀自动机、DP、二分、单调队列)

    题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2806 (luogu) https://www.luogu.org/pro ...

最新文章

  1. android单线字体,Android自定义字体
  2. python通过requirements.txt文件批量安装依赖包的实现步骤
  3. java at发短信_发送短信(1)
  4. 获取WebView加载的网页内容并进行动态修改
  5. nagios监控mysql
  6. mysql5.7复制集_技术分享 | 克隆:更优雅的创建 MySQL 实例副本
  7. Flutter入门一——W7环境下使用VSCode配置Flutter开发环境(脱离Android Studio安装)...
  8. 用极大似然法估计因子载荷矩阵_关于因子分析|stata
  9. 大专计算机课教案,计算机课教案
  10. python3 ZIP文件密码破解
  11. Visual Studio 2013如何显示代码行数
  12. 服务器iis短文件名漏洞,IIS短文件名漏洞分析及一个实例
  13. 施密特正交化(Schmidt)
  14. hello world漫游
  15. 美元兑人民币今天涨156个基点达到6.4044,人民币是否会持续贬值???
  16. Android 信号查看,安卓Android手机怎么快速查看系统信号强度
  17. 手把手教你破解Linux系统root密码——无需任何工具,有手就行
  18. 项目奖金一般是多少_MPAcc职业发展|看看国内券商、投行、四大一年能挣多少?...
  19. Healthcare Cloud on Windows Azure (0) 开篇
  20. 如何使用手机在线抠图工具进行一键抠图?

热门文章

  1. 使用Fiddler对IPhone手机的应用数据进行抓包分析
  2. Cordova+Ionic之坑
  3. HDU3507-Print Article-斜率dp入门题
  4. Deepin2014 QT Creator安装
  5. virsh default启动失败原因分析及解决
  6. CPU上跑深度学习模型,FPS也可以达100帧
  7. 重磅快讯:CCF发布最新版推荐中文科技期刊目录
  8. FoveaBox:目标检测新纪元,无Anchor时代来临!
  9. CVPR 2019 | 文本检测算法PSENet解读与开源实现
  10. 35行代码利用python生成字符画,非常适合初学者练习,附源码!