Advertisement
ivnikkk

Untitled

Mar 26th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #pragma GCC optimize("03")
  2. #pragma GCC optimize("Ofast")
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include "bits/stdc++.h>
  5. using namespace std;
  6. using namespace std::chrono;
  7. #define all(a)            a.begin(), a.end()
  8. typedef long long ll;
  9. typedef long double ld;
  10. vector<ll> posit;
  11. ll timer = 0;
  12. //  элемент - индекс;
  13. double rnd() { return double(rand()) / RAND_MAX; }
  14.  
  15. double gen_rand()
  16. {
  17.     int x = rand() + 1, y = rand() + 1;
  18.     x %= y;
  19.     return x / (double)y;
  20. }
  21. ll time_check = 1e6;
  22. string ans = "";
  23. bool break_point = false;
  24. ll get_size(vector<ll>& a, vector<ll>& check) {
  25.     ll it_pos = 0, it_val = 0;
  26.     ll siz = 0;
  27.     for (ll i = 0; i < check.size(); i++) {
  28.         timer++;
  29.         ll to = posit[check[i]];
  30.         if (timer % time_check == 0) {
  31.             if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  32.             {
  33.                 break_point = true;
  34.                 cout << ans << endl;
  35.                 exit(0);
  36.             }
  37.         }
  38.         if (it_pos < to) {
  39.             siz += (to - it_pos);
  40.             it_pos = to;
  41.         }
  42.         else if (it_pos > to) {
  43.             siz += (it_pos - to);
  44.             it_pos = to;
  45.         }
  46.         ll val = check[i];
  47.         if (it_val < val) {
  48.             siz += (val - it_val);
  49.             it_val = val;
  50.         }
  51.         else if (it_val > val) {
  52.             siz += (it_val - val);
  53.             it_val = val;
  54.         }
  55.     }
  56.     return { siz };
  57. }
  58. pair<ll, string> get(vector<ll>& a, vector<ll>& check) {
  59.     ll it_pos = 0, it_val = 0;
  60.     string cur = "";
  61.     for (ll i = 0; i < check.size(); i++) {
  62.         timer++;
  63.         ll to = posit[check[i]];
  64.         if (timer % time_check == 0) {
  65.             if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  66.             {
  67.                 break_point = true;
  68.                 cout << ans << endl;
  69.                 exit(0);
  70.             }
  71.         }
  72.         while (it_pos < to) {
  73.             it_pos++;
  74.             cur += 'R';
  75.             timer++;
  76.             if (timer % time_check == 0) {
  77.                 if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  78.                 {
  79.                     break_point = true;
  80.                     cout << ans << endl;
  81.                     exit(0);
  82.                 }
  83.             }
  84.         }
  85.         while (it_pos > to) {
  86.             it_pos--;
  87.             cur += 'L';
  88.             timer++;
  89.             if (timer % time_check == 0) {
  90.                 if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  91.                 {
  92.                     break_point = true;
  93.                     cout << ans << endl;
  94.                     exit(0);
  95.                 }
  96.             }
  97.         }
  98.         ll val = check[i];
  99.         while (it_val < val) {
  100.             it_val++;
  101.             cur += 'r';
  102.             timer++;
  103.             if (timer % time_check == 0) {
  104.                 if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  105.                 {
  106.                     break_point = true;
  107.                     cout << ans << endl;
  108.                     exit(0);
  109.                 }
  110.             }
  111.         }
  112.         while (it_val > val) {
  113.             it_val--;
  114.             cur += 'l';
  115.             timer++;
  116.             if (timer % time_check == 0) {
  117.                 if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  118.                 {
  119.                     break_point = true;
  120.                     cout << ans << endl;
  121.                     exit(0);
  122.                 }
  123.             }
  124.         }
  125.     }
  126.     return { (ll)cur.size(),cur };
  127. }
  128.  
  129. signed main() {
  130.     //#ifdef _DEBUG
  131.       //  freopen("input.txt", "r", stdin);
  132.         //freopen("output.txt", "w", stdout);
  133.     //#endif
  134.     ios_base::sync_with_stdio(false);
  135.     cin.tie(nullptr);
  136.     srand(time(nullptr));
  137.     ll n;
  138.     cin >> n;
  139.     vector<ll> a(n);
  140.     posit.resize(n);
  141.     for (ll i = 0; i < n; i++) {
  142.         cin >> a[i];
  143.         a[i]--;
  144.         posit[a[i]] = i;
  145.     }
  146.     vector<ll> prev(n);
  147.  
  148.     for (ll i = 0; i < n; i++) {
  149.         prev[i] = i;
  150.     }
  151.     ll cnt = 0;
  152.     double temp = 1000;
  153.     pair<ll, string> last_val = get(a, prev);
  154.     if (break_point) {
  155.         return 0;
  156.     }
  157.     ans = last_val.second;
  158.     ll best_len = last_val.first;
  159.     double t0 = 1000;
  160.     ll last_siz = get_size(a, prev);
  161.     while (1) {
  162.         timer++;
  163.         if (timer % time_check == 0) {
  164.             if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  165.             {
  166.                 break_point = true;
  167.                 cout << ans << endl;
  168.                 return 0;
  169.             }
  170.         }
  171.         vector<ll> is_best = prev;
  172.         for (ll i = 0; i < n * temp / t0; i++) {
  173.             timer++;
  174.             if (timer % time_check == 0) {
  175.                 if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
  176.                 {
  177.                     break_point = true;
  178.                     cout << ans << endl;
  179.                     exit(0);
  180.                 }
  181.             }
  182.             swap(is_best[rand() % (n)], is_best[rand() % (n)]);
  183.         }
  184.         ll now_siz = get_size(a, is_best);
  185.         if (break_point) {
  186.             return 0;
  187.         }
  188.         if (now_siz < best_len) {
  189.             auto buf = get(a, is_best);
  190.  
  191.             //cout << (ans.size()==best_len?"true":"false") << ' ' << ans.size() << ' ' << now_siz << ' ' << buf.first;
  192.             //cout << endl;
  193.             ans = buf.second;
  194.             best_len = buf.first;
  195.         }
  196.  
  197.         double go = exp(-(now_siz - last_siz) / temp);
  198.         if (now_siz < last_siz || gen_rand() <= go) {
  199.             prev = is_best;
  200.             last_siz = now_siz;
  201.         }
  202.         temp *= 0.999995;
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement