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>
- #include <chrono>
- using namespace std;
- using namespace std::chrono;
- #define all(a) a.begin(), a.end()
- typedef long long ll;
- typedef long double ld;
- ll time1 = 0;
- double temp;
- double t0 = 1000;
- double rnd() { return double(rand()) / RAND_MAX; }
- 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';
- time1++;
- }
- 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;
- high_resolution_clock::time_point t1 = high_resolution_clock::now();
- 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 count=0;
- ll best_len = INT_MAX;
- ll cnt = 0;
- while (true) {
- high_resolution_clock::time_point t2 = high_resolution_clock::now();
- duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
- double sup = time_span.count();
- if (sup >= 1.8) {
- cout << ans ;
- break;
- }
- cnt++;
- 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 go = exp((now.first - last_val.first) / temp);
- if (now.first < last_val.first || rnd() <= go) {
- prev = is_best;
- }
- temp *= 0.999995;
- }
- if (!cnt)return 132;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement