zoukankan      html  css  js  c++  java
  • P3512 [POI2010]PIL-Pilots-洛谷luogu

    刚研究完单调队列和单调栈

    于是就找题做了

    发现了这道蓝题

    以为很简单

    就着手来写了

    然而

    并不是我想的那样

    只是有一点点思路

    无奈

    还是看了题解

    好吧题解是真的挺好的

    --------------------------------------------------------------

    题目描述

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

    ---------------------------------------------------------------------------------------

    n<=3000000,单调队列。

    因为要同时维护最大值和最小值,所以需要两个单调队列,记录序号和最大最小值,然后

    从左往右枚举一遍O(n)的复杂度即可。

    单调队列的实现以最大值为例,当队列非空且队尾的数没有现在枚举到的这个数大时,就

    让队尾的数出队,因为在接下来的情况中它们永远不会是一个区间的最大值。最小值的队列同理。把当前位置加入最大最小值队列。然后比较最大值队列的队首即最大值和最小值,如果它们的差超过了k,就看一下最大值和最小值的队首哪个的序号更小,让序号更小的出队,同时更新当前队列的最前节点的位置,继续比较两个的队首,直到最大最小值的差小于等于k时,用当前位置减去当前队列的最前节点的位置再加一,就是目前的符合条件的序列长度,与最长值比较即可。

    当前队列的最前节点的位置初始值为1.

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    ll hx = 1,tx = 1,hn = 1,tn = 1,len=0,pre=1;   //最大值最小值的“头指针”“尾指针” 
    ll k,a[3000005],qx[3000005],qn[3000005],n;  //qx存最大值,qn存最小值 
    
    inline ll read()
    {
        ll ans = 0,op = 1;
        char ch = getchar();
        while(ch <'0' && ch >'9')
        {
            if(ch == '-')
                op = -1;
            ch = getchar();
        }
        while(ch >= '0' &&ch <= '9')
        {
            (ans *= 10) += (ch - '0');
            ch = getchar();
        }
        return ans * op;
    } //快读 
        
    int main()
    {
        k = read();
        n = read();
        for(ll i=1; i<=n; i++)
            a[i] = read();
        qx[1]=1;
        qn[1]=1;
        for(int i=2;i<=n;i++)
        {
            while(hx <= tx && a[qx[tx]] < a[i])
                tx--;
            while(hn <= tn && a[qn[tn]] > a[i])
                tn--;
            qx[++tx] = i;
            qn[++tn] = i;
            while(a[qx[hx]] - a[qn[hn]] >k)
            {
                if(qx[hx] < qn[hn])
                {
                    pre = qx[hx]+1;
                    hx++;
                }
                else
                {
                    pre = qn[hn]+1;
                    hn++;
                }
            }
            len=max(len,i-pre+1);
        }
        printf("%lld",len);
        return 0;
    }
  • 相关阅读:
    js和jquery 两种写法 鼠标经过图片切换背景效果
    phpStudy如何修改端口及WWW目录
    模仿淘宝上传图片之后在图片中单击按钮?
    资源汇总
    标题类型-整型
    VB6 内存释放
    在Textbox中按回车键后继续获取焦点
    ADO 读写文本文件
    VBA 拷贝文件
    VBA 获取文件夹内的文件列表
  • 原文地址:https://www.cnblogs.com/darlingroot/p/10342248.html
Copyright © 2011-2022 走看看