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 <iostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #include <bitset>
- #include <random>
- #include <cassert>
- #include <unordered_map>
- #include <unordered_set>
- #include <math.h>
- #include <cstdlib>
- using namespace std;
- #define all(a) a.begin(), a.end()
- typedef long long ll;
- typedef long double ld;
- double temp;
- double t0 = 1000;
- double gen_rand()
- {
- int x = rand() + 1, y = rand() + 1;
- x %= y;
- return x / (double)y;
- }
- pair<ll, string> get(vector<ll>& a,vector<ll> &check) {
- ll itl = 0, itr = (ll)a.size() - 1;
- string sub = "";
- ll cnt0 = 0, cnt1 = 0;
- for (ll i = 0; i < (ll)check.size(); i++) {
- if (check[i]) {
- while (cnt1 > itr) {
- itr--;
- sub += 'l';
- }
- while (cnt1 > itl) {
- itl++;
- sub += 'R';
- }
- cnt1++;
- }
- else {
- ll lol = (ll)a.size() - cnt0 - 1;
- while (lol < itl) {
- itl--;
- sub += 'L';
- }
- while (lol < itr) {
- itr--;
- sub += 'r';
- }
- cnt0++;
- }
- }
- return { (ll)sub.size(),sub };
- }
- 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);
- for (ll i = 0; i < n; i++) {
- cin >> a[i];
- }
- vector<ll> prev(2 * n);
- for (ll i = 0; i < 2*n; i++) {
- prev[i] = (i < n);
- }
- //shuffle(all(prev), rand());
- string ans = "#";
- ll time = 0;
- ll best_len = INT_MAX;
- while (true) {
- if (clock() / (double)CLOCKS_PER_SEC >= 0.9){
- cout << ans << endl;
- return 0;
- }
- pair<ll, string> last_val = get(a, prev);
- if (last_val.first < best_len) {
- ans = last_val.second;
- best_len = last_val.first;
- }
- vector<ll> is_best = prev;
- for (ll i = 0; i < n * temp / t0; i++){
- swap(is_best[rand() % (2*n)], is_best[rand() % (2*n)]);
- }
- pair<ll,string>now = get(a, is_best);
- if (now.first < best_len) {
- ans = now.second;
- best_len = now.first;
- }
- double ok = exp(-(now.first - last_val.first) / temp);
- if (now.first < last_val.first || gen_rand() <= ok) {
- prev = is_best;
- }
- temp *= 0.999995;
- }
- cout << ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement