• ZCMU--2192: Queue


    Description

    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

    1. 6
    2. 10 8 5 3 50 45

    Output

    2 1 0 -1 0 -1 

    Input

    1. 7
    2. 10 4 6 3 2 8 15

    Output

    4 2 1 0 -1 -1 -1 

    Input

    1. 5
    2. 10 3 1 10 11

    Output

    1 0 -1 -1 -1 

    解析:我们可以用单调栈思想来解决,开一个b[ ]从末尾开始往前记录单调递减的元素,因为如果对于Ak,右边有两个元素 Ai,Aj( i < j ,Ai>Aj),那么Ai其实没有必要存储在b[ ],因此b数组就存储了从末尾开始往前记录单调递减的元素。

    遍历的时候我们记录一个最小值minn,如果Ai等于minn,那么它将输出-1,如果小于minn,它将输出-1,并且将Ai加入到b数组中,如果大于minn,那么我们就二分找到b数组中第一个小于它的元素,因为我们倒着存入b中,所以找到的就是最右边小于它的。

    因为要算两者之间的距离,我们可以开个map来记录每个元素最右边的下标。

    1. #include <stdio.h>
    2. #include <map>
    3. using namespace std;
    4. const int N=100005;
    5. int a[N],b[N];//a记录原始数据且最后更新为答案数组,b记录从后往前单调递减元素
    6. map<int,int> mp;
    7. int main()
    8. {
    9. int n,minn=0x3f3f3f3f,cnt=0;//将minn设置为最大值
    10. scanf("%d",&n);
    11. for(int i=1;i<=n;i++) scanf("%d",&a[i]),mp[a[i]]=i;
    12. for(int i=n;i>=1;i--)
    13. {
    14. if(a[i]==minn) a[i]=-1;//等于minn
    15. else if(a[i]<minn) minn=a[i],b[++cnt]=a[i],a[i]=-1;//小于minn,并将a[i]加入b中
    16. else{//大于minn
    17. int l=1,r=cnt;
    18. while(l<=r)
    19. {
    20. int m=(l+r)>>1;
    21. if(b[m]>=a[i]) l=m+1;
    22. else r=m-1;
    23. }
    24. a[i]=mp[b[l]]-i-1;
    25. }
    26. }
    27. for(int i=1;i<=n;i++)
    28. {
    29. if(i!=1) printf(" ");
    30. printf("%d",a[i]);
    31. }
    32. return 0;
    33. }

  • 相关阅读:
    汽车信息安全导图
    数据库连接池知识点总结-DX的笔记
    Hadoop总结
    ES6 从入门到精通 # 12:数组的扩展方法一
    设计LRU缓存(双向链表+哈希表)
    使用Navicat对比多环境数据库数据差异和结构差异,以及自动DML和DDL脚本
    微信小程序 java网上购物商城系统
    pg 在执行批量插入问题 --chatGPT
    ERC-721 隐私泄露问题凸显 三种方案或能缓解
    Win11快速助手在哪里?Win11打开快速助手的方法
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/127969928