zoukankan      html  css  js  c++  java
  • CodeForces 91B Queue (线段树,区间最值)

    http://codeforces.com/problemset/problem/91/B

    B. Queue

    time limit per test:
    2 seconds
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.

    The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai> aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.

    The airport manager asked you to count for each of n walruses in the queue his displeasure.

    Input

    The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai(1 ≤ ai≤ 109).

    Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.

    Output

    Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

    Examples

    input

    6
    10 8 5 3 50 45

    output

    2 1 0 -1 0 -1 

    input

    7
    10 4 6 3 2 8 15

    output

    4 2 1 0 -1 -1 -1 

    input

    5
    10 3 1 10 11

    output

    1 0 -1 -1 -1 

    题意:

    给一个序列,对于第i个数字a[i],在右边找到一个比它小的数,并且最靠右的位置k,输出k-i-1,如果一个都找不到,输出-1。对于序列的每个元素都要输出。


    解题思路:

    可以用线段树,每个节点表示该段的最小值, 循环一遍数组,每个位置找到最靠右且比它小的数后(已经处理过),将其改为INF,并对线段树进行最小值更新。

    查询之前, 先询问整个数组的最小值和当前值的比较,满足最小值小于当前值才有查询的必要,如果满足查询的条件,右边不成立就一定在左边。

    上代码:

     1 #include <bits/stdc++.h>
     2 const int INF=0x3f3f3f3f;
     3 typedef long long LL;
     4 const double eps =1e-8;
     5 const int mod=1e9+7;
     6 const int maxn=1e5+10;
     7 using namespace std;
     8 
     9 struct node
    10 {
    11     int l;
    12     int r;
    13     int val;
    14 }Tr[maxn<<2];
    15 
    16 int a[maxn];    //存放数据
    17 int ans[maxn];    //存放答案 
    18 
    19 void PushUp(int u)
    20 {
    21     Tr[u].val=min(Tr[u<<1].val,Tr[u<<1|1].val);
    22 }
    23 
    24 void Build(int l,int r,int u)
    25 {
    26     Tr[u].l=l; Tr[u].r=r; Tr[u].val=0;
    27     if(l==r)
    28     {
    29         Tr[u].val=a[l];
    30         return ;
    31     }
    32     int mid=(l+r)>>1;
    33     Build(l,mid,u<<1);
    34     Build(mid+1,r,u<<1|1);
    35     PushUp(u);
    36 }
    37 
    38 void Update(int u,int pos)
    39 {
    40     int l=Tr[u].l; int r=Tr[u].r;
    41     if(l==r)
    42     {
    43         Tr[u].val=INF;    //pos位置的答案已得出,将其变为INF 
    44         return ;
    45     }
    46     int mid=(l+r)>>1;
    47     if(pos<=mid)
    48         Update(u<<1,pos);
    49     else
    50         Update(u<<1|1,pos);
    51     PushUp(u);    //向上更新最小值 
    52 }
    53 
    54 void Query(int u,int pos)
    55 {
    56     int l=Tr[u].l; int r=Tr[u].r;
    57     if(l==r)
    58     {
    59         ans[pos]=l-pos-1;    //得出pos位置的答案 
    60         return ;
    61     }
    62     int mid=(l+r)>>1;
    63     if(Tr[u<<1|1].val<a[pos])    //因为是最右边的最小值,先判断右边 
    64         Query(u<<1|1,pos);
    65     else
    66         Query(u<<1,pos);
    67 }
    68 
    69 
    70 int main()
    71 {
    72     #ifdef DEBUG
    73     freopen("sample.txt","r",stdin);
    74     #endif
    75     
    76     int n;
    77     scanf("%d",&n);
    78     for(int i=1;i<=n;i++)
    79         scanf("%d",&a[i]);
    80     Build(1,n,1);
    81     for(int i=1;i<=n;i++)    //遍历求每一个答案 
    82     {
    83         if(Tr[1].val>=a[i]) ans[i]=-1;    //不满足查询条件 
    84         else Query(1,i);
    85         Update(1,i);    //更新最小值
    86     }        
    87     for(int i=1;i<=n;i++)
    88         printf(i==n? "%d
    ":"%d ",ans[i]);
    89     
    90     return 0;
    91 }

    这题也可以用单调队列,从最后一个数开始处理,若该数比队列中最后一个都小,则是-1,并加入队尾,否则就对队列中的数进行二分(直接粘的题解,学完单调队列再回来填坑)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <queue>
     7 #include <vector>
     8 #include <set>
     9 #include <stack>
    10 #include <map>
    11 #include <climits>
    12  
    13 using namespace std;
    14  
    15 #define LL long long
    16 const int INF=0x3f3f3f3f;
    17 const int MAXN=1000010;
    18  
    19 int a[MAXN],ans[MAXN];
    20 int x[MAXN],p[MAXN];
    21  
    22 int main()
    23 {
    24     int n;
    25     while(~scanf("%d",&n))
    26     {
    27         for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    28         int sum=0;
    29         for(int i=n; i>=1; i--)
    30         {
    31             if(sum==0||x[sum-1]>=a[i])
    32             {
    33                 x[sum]=a[i];
    34                 p[sum++]=i;
    35                 ans[i]=-1;
    36             }
    37             else
    38             {
    39                 int k,l=0,r=sum-1;
    40                 while(l<=r)
    41                 {
    42                     int mid=(l+r)>>1;
    43                     if(x[mid]<a[i]) {k=mid;r=mid-1;}
    44                     else l=mid+1;
    45                 }
    46                 ans[i]=p[k]-i-1;
    47             }
    48         }
    49         printf("%d",ans[1]);
    50         for(int i=2; i<=n; i++)
    51             printf(" %d",ans[i]);
    52         printf("
    ");
    53     }
    54     return 0;
    55 }

    以下题解来自于:https://www.cnblogs.com/sineatos/p/3870790.html

    分析题目,我们发现题目需要我们求的值的要求有两个:①最右边的,②比当前考察的值小的。
      于是我们需要维护一个这样的序列,这个序列保存着已扫描的值里面的最小值,同时这个序列具有单调性,从开始到当前,序列需要保持递减,对于插入的时候如果无法保持序列的单调性的话,就不要需要插入的值,否则就插入。在求结果的时候,我们可以二分求出比考察点小的,里考察点最远(最右)的那个点,然后求出答案即可。

      这里记录一下维护序列单调性的两种方法:①要插入的元素一定会插入,通过抛弃原有的元素来保持单调性。②如果带插入的元素加入会打破单调性的话就不会插入到序列里面。
      如果用一个单调队列的维护一个序列,单调队列里面的元素是已经扫描过的元素的最值,次值,次次值······,同时,如果里面是拥有窗口的话,单调队列里面记录的元素可以是元素保存位置的下标,这样就可以更好地维护元素弹出。

      对于单调性的研究还需继续。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <vector>
     4 #include <algorithm>
     5 #define MAX 100002
     6 using namespace std;
     7 
     8 int a[MAX],ans[MAX];
     9 vector<int> v,num;
    10 
    11 int main()
    12 {
    13     int n;
    14     //freopen("data.txt","r",stdin);
    15     while(~scanf("%d",&n)){
    16         for(int i=0;i<n;i++) scanf("%d",&a[i]);
    17         v.clear();
    18         num.clear();
    19         for(int i=n-1;i>=0;i--){
    20             if(v.size()==0 || v.back()>=a[i]){
    21                 v.push_back(a[i]); num.push_back(i);
    22                 ans[i]=-1;
    23             }else{
    24                 int j = (lower_bound(v.rbegin(),v.rend(),a[i]) - v.rbegin());
    25                 j = (int)v.size() - j - 1;
    26                 ans[i] = num[j+1] - i - 1;
    27             }
    28         }
    29         for(int i=0;i<n;i++){
    30             if(i) printf(" ");
    31             printf("%d",ans[i]);
    32         }
    33         printf("
    ");
    34     }
    35     return 0;
    36 }
    37 
    38 91B
  • 相关阅读:
    js将数字转为千分位/清除千分位
    mybatis中的$和#的区别
    js处理title超长问题
    mybatis错误 Mapped Statements collection does not contain value for
    bootstrap添加多个模态对话框支持
    java.util.Collections.copy()方法注意点
    list通过比较器进行排序
    jquery对radio和checkbox的操作
    c++类简介
    c++ 类
  • 原文地址:https://www.cnblogs.com/jiamian/p/11282004.html
Copyright © 2011-2022 走看看