• CodeForces


    先上题目:

    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. Thedispleasure 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.

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

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

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

    上代码:

     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 }
    91B
  • 相关阅读:
    Django DRF 分页
    django DRF理解
    Docker使用
    基于Django+FastDFS文件管理系统搭建
    django+centos+mariadb读写分离完美实现(上)-mysql主从备份实现
    Python协程理解——基于爬虫举例
    基于Python的大数据的分页模型代码
    NOIP模拟——矩阵分组
    NOIP模拟——聚会
    NOIP模拟——卡牌游戏
  • 原文地址:https://www.cnblogs.com/sineatos/p/3870790.html
Copyright © 2020-2023  润新知