fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. pair<int,string> consistency(int n){
  33.  
  34. string left = "";
  35. string right = "";
  36. int last = 0;
  37.  
  38. int i = 0, j = n-1;
  39. while(i<=j){
  40.  
  41. if(a[i] <= last || a[j] <= last) break;
  42. if(a[i] == a[j]) break;
  43.  
  44. if(a[i] < a[j]){
  45. left.push_back('L');
  46. right.push_back('L');
  47. last = a[i];
  48. i++;
  49. }
  50. else{
  51. left.push_back('R');
  52. right.push_back('R');
  53. last = a[j];
  54. j--;
  55. }
  56. }
  57.  
  58.  
  59. int k = i;
  60. while(k<=j && (k==0 || a[k] > a[k-1])){
  61. left.push_back('L');
  62. k++;
  63. }
  64.  
  65. k = j;
  66. while(k>=i && (k==n-1 || a[k] > a[k+1] )){
  67. right.push_back('R');
  68. k--;
  69. }
  70.  
  71. int sz1 = left.size();
  72. int sz2 = right.size();
  73.  
  74. if(sz1 > sz2){
  75. return {sz1, left};
  76. }
  77. return {sz2, right};
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. pair<int,string> practice(int n){
  95.  
  96. return {};
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. void solve() {
  104.  
  105. int n;
  106. cin>> n;
  107.  
  108. a.resize(n);
  109. for(int i=0; i<n; i++) cin >> a[i];
  110.  
  111. auto ans = consistency(n);
  112. cout << ans.first << endl;
  113. cout << ans.second << endl;
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. int32_t main() {
  123. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  124.  
  125. int t = 1;
  126. // cin >> t;
  127. while (t--) {
  128. solve();
  129. }
  130.  
  131. return 0;
  132. }
Success #stdin #stdout 0s 5320KB
stdin
8
2 3 6 9 10 8 4 5
stdout
6
LLRLLL