Skip to content

Commit 0627fc3

Browse files
authored
Create day005sol01.cpp
1 parent 549e67c commit 0627fc3

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Submission Link: https://cses.fi/problemset/result/15792090/
2+
3+
/*
4+
we are going through the array ans checking if the number right left to it is smaller to it then answer for that is (i-1) else we will check position ans[i-1]
5+
if we encounter ans[i]=-1 we know there is no number smaller than that so the ans is -1
6+
*/
7+
8+
#include<bits/stdc++.h>
9+
using namespace std;
10+
void solve(){
11+
int n;
12+
cin>>n;
13+
vector<int> vec(n);
14+
for(int i=0;i<n;i++){
15+
cin>>vec[i];
16+
}
17+
vector<int> ans(n);
18+
ans[0]=-1;
19+
for(int i=1;i<n;i++){
20+
if(vec[i]>vec[i-1]){
21+
ans[i]=i-1;
22+
}else{
23+
int a=ans[i-1];
24+
bool ok=true;
25+
while(a>-1){
26+
if(vec[a]<vec[i]){
27+
ans[i]=a;
28+
ok=false;
29+
break;
30+
}
31+
a=ans[a];
32+
}
33+
if(ok) {
34+
ans[i]=-1;
35+
}
36+
}
37+
}
38+
for(int i=0;i<n;i++){
39+
cout<<ans[i]+1<<" ";
40+
}
41+
}
42+
int main(){
43+
solve();
44+
}

0 commit comments

Comments
 (0)