• Source
    1.  
    2. #include <bits/stdc++.h>
    3.  
    4. using namespace std;
    5.  
    6. #define int long long int
    7. #define F first
    8. #define S second
    9. #define pb push_back
    10. #define si set <int>
    11. #define vi vector <int>
    12. #define pii pair <int, int>
    13. #define vpi vector <pii>
    14. #define vpp vector <pair<int, pii>>
    15. #define mii map <int, int>
    16. #define mpi map <pii, int>
    17. #define spi set <pii>
    18. #define endl "\n"
    19. #define sz(x) ((int) x.size())
    20. #define all(p) p.begin(), p.end()
    21. #define double long double
    22. #define que_max priority_queue <int>
    23. #define que_min priority_queue <int, vi, greater<int>>
    24. #define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
    25. #define print(a) for(auto x : a) cout << x << " "; cout << endl
    26. #define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
    27. #define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
    28.  
    29. inline int power(int a, int b)
    30. {
    31. int x = 1;
    32. while (b)
    33. {
    34. if (b & 1) x *= a;
    35. a *= a;
    36. b >>= 1;
    37. }
    38. return x;
    39. }
    40.  
    41. template <typename Arg1>
    42. void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
    43. template <typename Arg1, typename... Args>
    44. void __f (const char* names, Arg1&& arg1, Args&&... args)
    45. {
    46. const char* comma = strchr (names + 1, ',');
    47. cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
    48. }
    49.  
    50. const int N = 200005;
    51.  
    52. struct Node
    53. {
    54. int idx,val,len;
    55. string psf;
    56. Node(int idx,int val,int len,string s){
    57. this->idx=idx;
    58. this->val=val;
    59. this->len=len;
    60. psf=s;
    61. }
    62. };
    63.  
    64.  
    65. void solve() {
    66.  
    67. int n;
    68. cin>>n;
    69. vi v(n);
    70. for (int i = 0; i < n; i++)
    71. {
    72. cin>>v[i];
    73. }
    74. vi dp(n,0);
    75. int lis=0,idx=-1;
    76. for(int i=0; i<n; i++){
    77. int maxx=0;
    78. for(int j=0; j<i; j++){
    79. if(v[j]<=v[i]){
    80. maxx=max(maxx,dp[j]);
    81. }
    82. }
    83. if(lis<1+maxx){idx=i;};
    84. lis=max(1+maxx,lis);
    85. dp[i]=max(dp[i],1+maxx);
    86. }
    87. cout<<lis<<endl;
    88. queue<Node>q;
    89. for (int i = 0; i < n; i++)
    90. {
    91. if(dp[i]==lis){
    92. q.push(Node(i,v[i],lis,to_string(v[i])));
    93. }
    94. }
    95.  
    96.  
    97.  
    98. while(!q.empty()){
    99. Node f=q.front();
    100. q.pop();
    101. if(f.len==1)cout<<f.psf<<endl;
    102. for(int i=f.idx-1; i>=0; i--){
    103. if(dp[i]==f.len-1 && v[i]<=f.val){
    104. q.push(Node(i,v[i],f.len-1,to_string(v[i])+" -> "+f.psf));
    105. }
    106. }
    107. }
    108.  
    109.  
    110. }
    111.  
    112. int32_t main()
    113. {
    114. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    115.  
    116.  
    117. clock_t z = clock();
    118.  
    119. int t = 1;
    120. // cin >> t;
    121. while (t--) solve();
    122.  
    123.  
    124.  
    125. return 0;
    126. }
    127.