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

时间:2021-10-31 02:20:24
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代码如下
 #include <iostream>
#include <set>
using namespace std;
int n, x[];
int main(){
while (cin >> n){
set<int>mpt;
set<int>::iterator it;
int i, k;
for (i = ; i < n; i++){
cin >> x[i];
k = x[i];
it = mpt.lower_bound(x[i]);
if (it == mpt.begin()) x[i] = -;
else{
it--;
x[i] = *it;
}
mpt.insert(k);
}
for (i = ; i < n; i++){
if (i) cout << ' ';
cout << x[i];
}
cout << "\r\n";
}
return ;
}

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