forked from isanchez-aguilar/CodeForces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1157C - Increasing Subsequence (hard version).cpp
92 lines (78 loc) · 1.4 KB
/
1157C - Increasing Subsequence (hard version).cpp
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
/*
* User: Isanchez_Aguilar
* Problem: CodeForces 1157C - Increasing Subsequence (hard version)
*/
#include <bits/stdc++.h>
using namespace std;
using Long = long;
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> vals(n);
for (int i = 0; i < n; ++i)
cin >> vals[i];
int left = 0;
int lastVal = 0;
int right = n - 1;
string ans = "";
while (left <= right)
{
int leftVal = vals[left];
int rightVal = vals[right];
if (leftVal <= lastVal and rightVal <= lastVal)
break;
if (rightVal <= lastVal)
{
++left;
ans += "L";
lastVal = leftVal;
}
else if (leftVal <= lastVal)
{
--right;
ans += "R";
lastVal = rightVal;
}
else
{
if (leftVal < rightVal)
{
++left;
ans += "L";
lastVal = leftVal;
}
else if (rightVal < leftVal)
{
--right;
ans += "R";
lastVal = rightVal;
}
else
{
int i = 1;
int j = 1;
while (left + i <= right and vals[left + i - 1] < vals[left + i])
++i;
while (right - j >= left and vals[right - j + 1] < vals[right - j])
++j;
if (j < i)
{
++left;
ans += "L";
lastVal = leftVal;
}
else
{
--right;
ans += "R";
lastVal = rightVal;
}
}
}
}
cout << ans.length() << "\n" << ans << "\n";
return 0;
}