Advertisement
MathQ_

Untitled

Dec 2nd, 2023
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <set>
  5. #include <algorithm>
  6. #include <numeric>
  7. #include <queue>
  8. #include <deque>
  9. #include <random>
  10. #include <chrono>
  11.  
  12. using namespace std;
  13.  
  14. typedef long long ll;
  15. typedef unsigned long long ull;
  16. typedef long double ld;
  17. typedef pair<int, int> pii;
  18. typedef pair<ll, ll> pll;
  19.  
  20. #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  21. #define file_in freopen("input.txt", "r", stdin);
  22. #define all(x) (x).begin(), (x).end()
  23. #define sz(x) (int)x.size()
  24. #define fi first
  25. #define se second
  26. #define endl '\n'
  27.  
  28. template<typename T> istream& operator>>(istream &in, vector<T> &v) { for (auto &el : v) { in >> el; } return in; }
  29. template<typename T> ostream& operator<<(ostream &out, const vector<T> &v) { for (auto &el : v) { out << el << " "; } return out; }
  30. template<typename T1, typename T2> istream& operator>>(istream &in, pair<T1, T2> &v) { in >> v.fi >> v.se; return in; }
  31. template<typename T1, typename T2> ostream& operator<<(ostream &out, const pair<T1, T2> &v) { out << v.fi << " " << v.se; return out; }
  32.  
  33. mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
  34.  
  35. void fill(vector<int> &state) {
  36.     iota(all(state), 0);
  37.     shuffle(all(state), rd);
  38. }
  39.  
  40. int get(int i, vector<int> &state) {
  41.     int res = 0;
  42.     for (int j = 0; j < sz(state); ++j) {
  43.         res += abs(i - j) == abs(state[i] - state[j]);
  44.     }
  45.     return res - 1;
  46. }
  47.  
  48. const ll K = (1ll << 32) - 1, LIM = 1000;
  49. const ld TIME = 0.99, STEP = 0.001;
  50.  
  51. ld rnd() { return (ld)rd() / K; }
  52. ld sigmoid(ld x) { return 1 / (1 + exp(-x)); }
  53. bool solve(int n, ld start) {
  54.     vector<int> state(n);
  55.     fill(state);
  56.     int cur_cost = 0, same_cost = 0;
  57.     for (int i = 0; i < n; ++i) cur_cost += get(i, state);
  58.     cur_cost >>= 1;
  59.    
  60.     ld tmp = 2;
  61.     while ((clock() - start) / CLOCKS_PER_SEC <= TIME) {
  62.         tmp -= STEP;
  63.         ++same_cost;
  64.         if (same_cost == LIM || cur_cost == 0) {
  65.             break;
  66.         }
  67.         int i = rd() % n, j = rd() % n;
  68.         int new_cost = cur_cost - get(i, state) - get(j, state);
  69.         swap(state[i], state[j]);
  70.         new_cost += get(i, state) + get(j, state);
  71.         if (cur_cost > new_cost || rnd() < exp((ld)(cur_cost - new_cost) / sigmoid(tmp))) {
  72.             cur_cost = new_cost;
  73.             same_cost = 0;
  74.         } else {
  75.             swap(state[i], state[j]);
  76.         }
  77.     }
  78.     if (cur_cost == 0) {
  79.         for (auto &el : state) cout << el + 1 << ' '; cout << '\n';
  80.         return true;
  81.     } else {
  82.         return false;
  83.     }
  84. }
  85.  
  86. int main() {
  87.     fast
  88.     // file_in
  89.  
  90.     int n;
  91.     cin >> n;
  92.     ld start = clock();
  93.     while ((clock() - start) / CLOCKS_PER_SEC <= TIME) {
  94.         if (solve(n, start)) {
  95.             break;
  96.         }
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement