Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("03")
- #pragma GCC optimize("Ofast")
- #define _CRT_SECURE_NO_WARNINGS
- #include "bits/stdc++.h>
- using namespace std;
- using namespace std::chrono;
- #define all(a) a.begin(), a.end()
- typedef long long ll;
- typedef long double ld;
- vector<ll> posit;
- ll timer = 0;
- // элемент - индекс;
- double rnd() { return double(rand()) / RAND_MAX; }
- double gen_rand()
- {
- int x = rand() + 1, y = rand() + 1;
- x %= y;
- return x / (double)y;
- }
- ll time_check = 1e6;
- string ans = "";
- bool break_point = false;
- ll get_size(vector<ll>& a, vector<ll>& check) {
- ll it_pos = 0, it_val = 0;
- ll siz = 0;
- for (ll i = 0; i < check.size(); i++) {
- timer++;
- ll to = posit[check[i]];
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- if (it_pos < to) {
- siz += (to - it_pos);
- it_pos = to;
- }
- else if (it_pos > to) {
- siz += (it_pos - to);
- it_pos = to;
- }
- ll val = check[i];
- if (it_val < val) {
- siz += (val - it_val);
- it_val = val;
- }
- else if (it_val > val) {
- siz += (it_val - val);
- it_val = val;
- }
- }
- return { siz };
- }
- pair<ll, string> get(vector<ll>& a, vector<ll>& check) {
- ll it_pos = 0, it_val = 0;
- string cur = "";
- for (ll i = 0; i < check.size(); i++) {
- timer++;
- ll to = posit[check[i]];
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- while (it_pos < to) {
- it_pos++;
- cur += 'R';
- timer++;
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- }
- while (it_pos > to) {
- it_pos--;
- cur += 'L';
- timer++;
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- }
- ll val = check[i];
- while (it_val < val) {
- it_val++;
- cur += 'r';
- timer++;
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- }
- while (it_val > val) {
- it_val--;
- cur += 'l';
- timer++;
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- }
- }
- return { (ll)cur.size(),cur };
- }
- signed main() {
- //#ifdef _DEBUG
- // freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- //#endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- srand(time(nullptr));
- ll n;
- cin >> n;
- vector<ll> a(n);
- posit.resize(n);
- for (ll i = 0; i < n; i++) {
- cin >> a[i];
- a[i]--;
- posit[a[i]] = i;
- }
- vector<ll> prev(n);
- for (ll i = 0; i < n; i++) {
- prev[i] = i;
- }
- ll cnt = 0;
- double temp = 1000;
- pair<ll, string> last_val = get(a, prev);
- if (break_point) {
- return 0;
- }
- ans = last_val.second;
- ll best_len = last_val.first;
- double t0 = 1000;
- ll last_siz = get_size(a, prev);
- while (1) {
- timer++;
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- return 0;
- }
- }
- vector<ll> is_best = prev;
- for (ll i = 0; i < n * temp / t0; i++) {
- timer++;
- if (timer % time_check == 0) {
- if (clock() / (double)CLOCKS_PER_SEC >= 1.9)
- {
- break_point = true;
- cout << ans << endl;
- exit(0);
- }
- }
- swap(is_best[rand() % (n)], is_best[rand() % (n)]);
- }
- ll now_siz = get_size(a, is_best);
- if (break_point) {
- return 0;
- }
- if (now_siz < best_len) {
- auto buf = get(a, is_best);
- //cout << (ans.size()==best_len?"true":"false") << ' ' << ans.size() << ' ' << now_siz << ' ' << buf.first;
- //cout << endl;
- ans = buf.second;
- best_len = buf.first;
- }
- double go = exp(-(now_siz - last_siz) / temp);
- if (now_siz < last_siz || gen_rand() <= go) {
- prev = is_best;
- last_siz = now_siz;
- }
- temp *= 0.999995;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement