码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P3512 [POI2010]PIL-Pilots

时间:2017-10-15 22:27:22      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:mes   names   格式   amp   res   png   poi   ordinary   determine   

题目描述

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的序列

考虑两个单调队列储存下标

每次用队列头计算答案

当两队列头差值不满足条件时,直接跳到两个最值下表最近的那个的位置

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000003;
int n,k;
int a[maxn];
int q1[maxn],q2[maxn];
int main () {
    scanf("%d%d",&k,&n);
    for(int i=1;i<=n;i++) 
        scanf("%d",a+i);
    q1[1]=q2[1]=1;int be=1;
    int h1=1,t1=1,h2=1,t2=1;
    int ans=0;
    for(int i=2;i<=n;++i) {
        while(h1<=t1&&a[q1[t1]]<a[i])t1--;
        while(h2<=t2&&a[q2[t2]]>a[i])t2--;
        q1[++t1]=q2[++t2]=i;
        while(a[q1[h1]]-a[q2[h2]]>k) {
            if(q1[h1]<q2[h2]) be=q1[h1]+1,h1++;
            else be=q2[h2]+1,h2++;
        }
        ans=max(ans,i-be+1);
    }
    printf("%d\n",ans);
    return 0;
}

 

luogu P3512 [POI2010]PIL-Pilots

标签:mes   names   格式   amp   res   png   poi   ordinary   determine   

原文地址:http://www.cnblogs.com/sssy/p/7674334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!