Give Me This Pizza

本文介绍了一种高效算法,用于解决给定数列中每个元素右侧最近的大值问题。通过使用栈与数组记录特定位置,避免了传统方法的超时问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Today Abdalrahman and Ali have one hour break, so they decided to spend it in the Innovation Programming Lab to invent new game.

After a deep thinking they invented a new game that is played using an array a. The goal of the game is to find for each value in the array the nearest larger value to it from the right. (i.e. for the ith value in the array, the goal is to find the value in the nearest jth index, such that (1  ≤  i  <  j  ≤  n) and (aj  >  ai)). If the nearest larger value does not exist, then the answer is “-1”.

Abdalrahman and Ali thought that this game is very hard and no one can solve it, so they wrote it in the white board inside the lab, and under it they wrote the following statement: “If you solve this game correctly we will give you a very big pizza”.

Yosef enter the lab after Abdalrahman and Ali left it. When he saw the game, he decided to solve it to take the pizza. Yosef is very hungry currently so you decided to help him to solve the game as soon as possible. Can you?

Input

The first line contains an integer n (1  ≤  n  ≤  105), where n is the length of the array a.

The second line contains n integers a1, a2, …, an (1  ≤  ai  ≤  50), giving the array a.

Output

Print n integers b1, b2, …, bn, where bi is the nearest larger value for the ith value in array a. If such value does not, then bi must be “-1” (without quotes).

Examples

Input
5
2 1 3 9 4

Output
3 3 9 -1 -1

题目大意: 给定一个数列,找到每个数右边比它大的最近的数并输出,若不存在输出-1。
由于传统思路会超时,因此这道题用一个比较骚的操作。

由于数字的范围是1-50,因此开一个长度为50的数组记录每个数出现的位置。



int main(){
    int n,i,j,num[100010],rec[55];
    int fff;
    while(cin>>n){
        stack<int>ans;
        bool flag=1;
        int mmin;
        while(!ans.empty())
            ans.pop();
        memset(rec,0,sizeof(rec));
        for(i=0;i<n;i++)
            cin>>num[i];
        for(i=n-1;i>=0;i--){  //循环从右向左跑
            rec[num[i]]=i;    //记录每个数出现的位置并不断更新(以得到最靠左的那个) 如果数字9在三号位置出现了,就有rec[9]==3;
            mmin=100010;
            for(j=num[i]+1;j<=50;j++){    //循环从当前num[i]的下一个值往上跑(如果num[i]==8,就从9-50里面找一个位置最靠左即表示位置的数值最小的) 
                if(rec[j]&&rec[j]<mmin){
                    mmin=rec[j];
                    fff=j;
                }
            }
            if(j==51&&mmin==100010) ans.push(-1);
            else ans.push(fff);

        }
        while(!ans.empty()){
            if(flag){
            flag=!flag;
            cout<<ans.top();
            }
            else cout<<" "<<ans.top();
            ans.pop();
        }   
        cout<<endl;     
    }

    return 0;
}
内容概要:本文档《opencv高频面试题.docx》涵盖了OpenCV的基础概念、图像处理操作、特征提取与匹配、目标检测与机器学习、实际编程题、性能优化以及进阶问题。首先介绍了OpenCV作为开源计算机视觉库,支持图像/视频处理、目标检测、机器学习等领域,应用于安防、自动驾驶、医学影像、AR/VR等方面。接着详细讲述了图像的存储格式(如Mat类)、通道的概念及其转换方法。在图像处理部分,讲解了图像灰度化、二值化、边缘检测等技术。特征提取方面,对比了Harris和Shi-Tomasi角点检测算法,以及SIFT、SURF、ORB的特征提取原理和优缺点。目标检测部分介绍了Haar级联检测原理,并阐述了如何调用深度学习模型进行目标检测。文档还提供了几个实际编程题示例,如读取并显示图像、图像旋转、绘制矩形框并保存等。最后,探讨了性能优化的方法,如使用cv2.UMat(GPU加速)、减少循环等,以及相机标定、光流等进阶问题。 适合人群:对计算机视觉有一定兴趣,具备一定编程基础的学习者或从业者。 使用场景及目标:①帮助学习者掌握OpenCV的基本概念和技术;②为面试准备提供参考;③为实际项目开发提供技术指导。 阅读建议:由于内容涵盖广泛,建议读者根据自身需求有选择地深入学习相关章节,并结合实际编程练习加深理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值