Advertisement
tiom4eg

N-Queens Solver (upto 25k in 1000ms)

Apr 21st, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define pii pair <int, int>
  9. #define pll pair <ll, ll>
  10. #define vi vector <int>
  11. #define mi vector <vector <int> >
  12. // Quick functions
  13. #define endl "\n"
  14. #define F first
  15. #define S second
  16. #define all(a) a.begin(), a.end()
  17. #define sz(a) a.size()
  18. #define pb push_back
  19. #define mp make_pair
  20. // Quick fors
  21. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  22. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  23. // Pragmas
  24. #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
  25. //#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  26. #pragma GCC comment(linker, "/stack:200000000")
  27. // PBDS
  28. #include <ext/pb_ds/assoc_container.hpp>
  29. #include <ext/pb_ds/tree_policy.hpp>
  30. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  31. #define ook order_of_key
  32. #define fbo find_by_order
  33. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  34. using namespace std;
  35. using namespace __gnu_pbds;
  36. mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
  37.  
  38. int randint(int n) { // [0; n)
  39.     return rng() % n;
  40. }
  41.  
  42. /*
  43.  ██▓     ███▄ ▄███▓ ▄▄▄       ▒█████
  44. ▓██▒    ▓██▒▀█▀ ██▒▒████▄    ▒██▒  ██▒
  45. ▒██░    ▓██    ▓██░▒██  ▀█▄  ▒██░  ██▒
  46. ▒██░    ▒██    ▒██ ░██▄▄▄▄██ ▒██   ██░
  47. ░██████▒▒██▒   ░██▒ ▓█   ▓██▒░ ████▓▒░
  48. ░ ▒░▓  ░░ ▒░   ░  ░ ▒▒   ▓▒█░░ ▒░▒░▒░
  49. ░ ░ ▒  ░░  ░      ░  ▒   ▒▒ ░  ░ ▒ ▒░
  50.   ░ ░   ░      ░     ░   ▒   ░ ░ ░ ▒
  51.     ░  ░       ░         ░  ░    ░ ░
  52. */
  53. const int ITER = 50000;
  54. vi cnt, rcnt, a;
  55. int n, cur = 0;
  56. void gen() {
  57.     vi p(n); FOR(i, 0, n) p[i] = i;
  58.     FOR(i, 0, n) {
  59.         int k = randint(n - i);
  60.         a[i] = p[k];
  61.         p.erase(p.begin() + k);
  62.     }
  63. }
  64. // all in O(1)
  65. int f(int p) { return cnt[p + a[p]] + rcnt[2 * (n - 1) - a[p] - (n - p - 1)] - 2; }
  66. void add(int p) { cnt[p + a[p]]++, rcnt[2 * (n - 1) - a[p] - (n - p - 1)]++; }
  67. void del(int p) { cnt[p + a[p]]--, rcnt[2 * (n - 1) - a[p] - (n - p - 1)]--; }
  68. void change(int p1, int p2) {
  69.     cur -= 2 * (f(p1) + f(p2));
  70.     del(p1);
  71.     del(p2);
  72.     swap(a[p1], a[p2]);
  73.     add(p1);
  74.     add(p2);
  75.     cur += 2 * (f(p1) + f(p2));
  76. }
  77.  
  78. int main() {
  79.     cin >> n;
  80.     FOR(att, 0, 100) {
  81.         cur = 0;
  82.         cnt.assign(2 * n, 0);
  83.         rcnt.assign(2 * n, 0);
  84.         a.resize(n);
  85.         gen();
  86.         FOR(i, 0, n) add(i);
  87.         FOR(i, 0, n) cur += f(i);
  88.         FOR(i, 0, ITER) {
  89.             if (!cur) break;
  90.             int now = cur;
  91.             int p1 = randint(n), p2 = randint(n);
  92.             while (p2 == p1) p2 = randint(n);
  93.             change(p1, p2);
  94.             if (cur > now) change(p1, p2);
  95.         }
  96.         if (!cur) {
  97.             for (auto& x : a) cout << x + 1 << ' ';
  98.             return 0;
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement