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,换行请使用" ",非常抱歉
题目大意:给你一排数字,在这个状态下,在当前数的左边找比它小的最大数字,没有就是-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 << " "; 26 } 27 return 0; 28 }
有时候巧用容器也是不错的~~~