-
Notifications
You must be signed in to change notification settings - Fork 0
/
PROBLEM 96 b INCREASING SUBSEQUENCE (HARD VERSION)
105 lines (105 loc) · 1.66 KB
/
PROBLEM 96 b INCREASING SUBSEQUENCE (HARD VERSION)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
#include <string>
using namespace std;
#define int long long
#define endl "\n"
const int mod=1e9+7;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define clr(a,x) memset(a,x,sizeof(a))
#define T int t;cin>>t; while(t--)
#define vll vector<int>
#define pb push_back
#define sz size
#define str to_string
#define intt stoll
int32_t main()
{
int n,x=0;
cin>>n;
vll a;
int p=0,q=n-1,l=-1,k=0;
for(int i=0;i<n;i++)
{
cin>>x;
a.pb(x);
}
string s="";
for(int i=0;i<n;i++)
{
if(a[p]>l && a[q]>l && a[p]!=a[q])
{
l=min(a[p],a[q]);
if(l==a[p])
{
p++;
k++;
s+='L';
}
else
{
q--;
k++;
s+='R';
}
// cout<<"a"<<" ";
}
else if(a[p]==a[q] && p!=q && a[p]>l)
{
int y=0,z=0;
x=a[p];
for(int i=p+1;i<q;i++)
{
if(a[i]>x)
{
x=a[i];
y++;
}
else{break;}
}
x=a[q];
for(int i=q-1;i>p;i--)
{
if(a[i]>x)
{
x=a[i];
z++;
}
else
{break; }
}
if(y>z)
{
l=a[p];
p++;
k++;
s+='L';
}
else
{
l=a[q];
q--;
k++;
s+='R';
}
// cout<<"b"<<" "<<y<<" "<<z;
}
else if(a[p]>l)
{
l=a[p];
p++;
k++;
s+='L';
// cout<<"c"<<" ";
}
else if(a[q]>l)
{
l=a[q];
q--;
k++;
s+='R';
//cout<<"d"<<" ";
}
//cout<<k<<" "<<l<<endl<<s<<endl;
}
cout<<k<<endl<<s;
}