zoukankan      html  css  js  c++  java
  • 【单调队列+二分查找】bzoj 1012: [JSOI2008]最大数maxnumber

    【题意】

    维护一个单调递减的q数组,用id数组记录q数组的每个下标对应在原数组的位置,那么id数组一定有单调性(q数组中越靠后,原数组中也靠后),然后二分查找这个数

    【AC】

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 int n,d;
     5 const int maxn=2e5+2;
     6 int q[maxn],id[maxn];
     7 int tail;int cnt;
     8 int last;
     9 void add(int x)
    10 {
    11     while(tail&&q[tail]<=x) tail--;
    12     q[++tail]=x;id[tail]=++cnt;
    13 }
    14 int query(int x)
    15 {
    16     int l=cnt-x+1;
    17     int pos=lower_bound(id+1,id+tail+1,l)-id;
    18     return q[pos];
    19 }
    20 void init()
    21 {
    22     memset(q,0,sizeof(q));
    23     memset(id,0,sizeof(id));
    24     tail=0;
    25     cnt=0;
    26     last=0;
    27 }
    28 int main()
    29 {
    30     while(~scanf("%d%d",&n,&d))
    31     {
    32         init();
    33         char op[2];int x;
    34         while(n--)
    35         {
    36             scanf("%s%d",op,&x); 
    37             if(op[0]=='A')
    38             {
    39                 add((x+last)%d);
    40             }
    41             else
    42             {
    43                 printf("%d
    ",last=query(x));
    44             }
    45         }
    46     }
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    妹妹
    小猴和北极熊
    盛趣->盛大
    运维
    操之过急
    修马路
    博人传
    醉酒
    【跨域】SpringBoot跨域,拦截器中,第一次获取的请求头为NULL,发送两次请求的处理方式
    【Linux】Linux安装Tomcat
  • 原文地址:https://www.cnblogs.com/itcsl/p/7443281.html
Copyright © 2011-2022 走看看