[Swust OJ 1026]--Egg pain's hzf

本文详细介绍了如何使用C++容器解决在一组整数中找到每个数左侧比其小的最大数的问题。通过引入set容器,并利用其lower_bound函数,实现了高效的查找逻辑。同时,文章提供了一个实例输入输出示例,帮助读者理解实现过程。

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

 
 
 
Time limit(ms): 3000    Memory limit(kb): 65535
 

hzf is crazy about reading math recently,and he is thinking about a boring problem.


Now there are n integers Arranged in a line.For each integer,he wants to know the maximum integer in its left which is less than it.

Description

The input consists of multiple test cases.


For each case,the first line contains one integer n(0<n<=100000),indicating the number of integers.


The second line contains n integers,indicating the integers from left to right,marked a1…an.(0<ai<=10^9).

Input

For each case,there are n integers,indicating the maximum integer in ai's left which is less than it.If it doesn's exist,output -1.

Output
1
2
3
4
5
1 2 3 4 5
5
5 4 3 2 1
Sample Input
1
2
-1 1 2 3 4
-1 -1 -1 -1 -1
Sample Output
由于OJ上传数据的BUG,换行请使用"\r\n",非常抱歉

题目大意:给你一排数字,在这个状态下,在当前数的左边找比它小的最大数字,没有就是-1

思路:这个题说白了就是一个容器set的用法(当然其他方法也可以),这个头文件类有点多
给个知识链接:http://www.cnblogs.com/zyxStar/p/4542835.html代码如下
 1 #include <iostream>
 2 #include <set>
 3 using namespace std;
 4 int n, x[100001];
 5 int main(){
 6     while (cin >> n){
 7         set<int>mpt;
 8         set<int>::iterator it;
 9         int i, k;
10         for (i = 0; i < n; i++){
11             cin >> x[i];
12             k = x[i];
13             it = mpt.lower_bound(x[i]);
14             if (it == mpt.begin()) x[i] = -1;
15             else{
16                 it--;
17                 x[i] = *it;
18             }
19             mpt.insert(k);
20         }
21         for (i = 0; i < n; i++){
22             if (i) cout << ' ';
23             cout << x[i];
24         }
25         cout << "\r\n";
26     }
27     return 0;
28 }
View Code

有时候巧用容器也是不错的~~~

转载于:https://www.cnblogs.com/zyxStar/p/4542800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值