• 洛谷P2251 质量检测


    题目背景

    题目描述

    为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... Am},以及第2至第M + 1件的Q[m + 1], Q[m + 2] ... 最后统计第N - M + 1至第N件的Q[n]。根据Q再做进一步评估。

    请你尽快求出Q序列。

    输入输出格式

    输入格式:

    输入共两行。

    第一行共两个数N、M,由空格隔开。含义如前述。

    第二行共N个数,表示N件产品的质量。

    输出格式:

    输出共N - M + 1行。

    第1至N - M + 1行每行一个数,第i行的数Q[i + M - 1]。含义如前述。

    输入输出样例

    输入样例#1: 复制
    10 4
    16 5 6 9 5 13 14 20 8 12
    
    输出样例#1: 复制
    5
    5
    5
    5
    5
    8
    8
    

    说明

    [数据范围]

    30%的数据,N <= 1000

    100%的数据,N <= 100000

    100%的数据,M <= N, A <= 1 000 000

    单调队列裸题

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<deque>
     7 #define LL long long 
     8 #define lb(x)    ((x)&(-x))
     9 using namespace std;
    10 const int MAXN=1000001;
    11 inline int read()
    12 {
    13     char c=getchar();int x=0,f=1;
    14     while(c<'0'||c>'9')    {if(c=='-')    f=-1;c=getchar();}
    15     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*f;
    16 }
    17 struct node
    18 {
    19     int pos,val;
    20     node(){    pos=val=0;    }
    21     node(int a,int b){    pos=a,val=b;     }
    22 };
    23 deque<node>q;
    24 int a[MAXN];
    25 int main()
    26 {
    27     int n=read(),m=read();
    28     for(int i=1;i<=n;i++)
    29         a[i]=read();
    30     for(int i=1;i<=n;i++)
    31     {
    32         while(q.size()>0&&i-m>=q.front().pos)    q.pop_front();
    33         while(q.size()>0&&a[i]<=q.back().val)        q.pop_back();
    34         q.push_back(node(i,a[i]));
    35         if(i>=m)    printf("%d
    ",q.front().val);    
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    vim tab 和4个空格
    python 入门
    pyenv 以及 virtualenv
    Redis Cluster 理论知识
    使用Redis SETNX 命令实现分布式锁
    go runtime scheduler
    LeetCode Valid Parentheses
    LeetCode Rotate Image
    leetcode
    HDU 3657 Game(取数 最小割)经典
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7711114.html
Copyright © 2020-2023  润新知