Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton.
He is provided an array Arr[ ] of N elements.
For each element Arr [ i ], he needs to find the largest element Arr [ j ] where j < i and Arr [ j ] < Arr [ i ]
Help him to achieve this task.
Input
First line comprises N- the number of elements in the array.
Second line contains N space separated integers denoting the elements of the array.
Output
Print N lines denoting the required answer as specified above. In case no such value exists for some
Arr [ i ], print "-1" (without quotes).
Constraints
- 1 ≤ N ≤ 200000
- 1 ≤ Arr [ i ] ≤ 1015
Example
5
1 2 3 5 4
Output:
-1
1
2
3
3
通过 set 或 map 就可轻松解决;
关键字 : set ,map , lower_bound()函数
1.学长的代码
5 # include <bits/stdc++.h> 6 using namespace std; 7 set<long long> s; 8 int main () 9 { 10 int n; 11 long long x; 12 scanf("%d", &n); 13 for (int i = 1; i <= n; ++i) { 14 scanf("%lld", &x); 15 x = -x; 16 auto iter = s.lower_bound(x + 1); 17 if (iter == s.end()) puts("-1"); 18 else printf("%lld ", -*iter); 19 s.insert(x); 20 } 21 return 0; 22 }
2 . 一开始不会使用set ,用map自己做的
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 map<long long,int> imap; 6 long long t; 7 int n; 8 cin>>n; 9 for(int i =1;i<=n;i++) 10 { 11 scanf("%lld",&t); 12 t = -t; 13 //map<long long,int>::iterator it; 14 auto it = imap.lower_bound(t+1); 15 if(it == imap.end()) cout<<"-1"<<endl; 16 else 17 cout<<-(it->first)<<endl; 18 imap[t] = i; 19 } 20 return 0; 21 }