标签:ret contain span ast each div lin sts nts
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.
First line comprises N- the number of elements in the array.
Second line contains N space separated integers denoting the elements of the array.
Print N lines denoting the required answer as specified above. In case no such value exists for some
Arr [ i ], print "-1" (without quotes).
5
1 2 3 5 4
Output:
-1
1
2
3
3
通过 set 或 map 就可轻松解决;
1.学长的代码
1 /*
从前往后遍历数组,维护一个setset,对于数组的当前元素acurrentacurrent,使用setset的lower_boundlower_bound函数查找
setset里面满足≥acurrent≥acurrent的元素里的最小元素,再把acurrentacurrent插入setset里面就行了。 2 但是题目问的是当前元素前面的元素里满足<acurrent<acurrent的元素里的最大元素。 3 怎么办? 4 首先把数组里的每个元素取个相反数就行了。
*/ 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\n", -*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 }
标签:ret contain span ast each div lin sts nts
原文地址:https://www.cnblogs.com/darkboy/p/9379891.html