Advertisement
tiom4eg

local opti

May 15th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 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 ull unsigned long long
  9. #define pii pair <int, int>
  10. #define pll pair <ll, ll>
  11. #define vi vector <int>
  12. #define mi vector <vector <int> >
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. #define sz(a) a.size()
  19. #define pb push_back
  20. #define mp make_pair
  21. #define min(a, b) ((a < b) ? (a) : (b))
  22. #define max(a, b) ((a > b) ? (a) : (b))
  23. // Quick fors
  24. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  25. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  26. // Pragmas
  27. #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
  28. #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  29. #pragma GCC comment(linker, "/stack:200000000")
  30. // PBDS
  31. #include <ext/pb_ds/assoc_container.hpp>
  32. #include <ext/pb_ds/tree_policy.hpp>
  33. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  34. #define ook order_of_key
  35. #define fbo find_by_order
  36. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  37. using namespace std;
  38. using namespace __gnu_pbds;
  39. mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
  40.  
  41. int randint(int n) { // [0; n)
  42.     return rng() % n;
  43. }
  44.  
  45. const int ITER = 400000;
  46. vi a;
  47. int n, cur = 0;
  48. int g[128][128], c[128][128], hori[128], vert[128];;
  49. void gen() {
  50.     vi p(n); FOR(i, 0, n) p[i] = i;
  51.     FOR(i, 0, n) {
  52.         int k = randint(n - i);
  53.         a[i] = p[k];
  54.         p.erase(p.begin() + k);
  55.     }
  56.     FOR(i, 0, n) hori[i] = a[i], vert[a[i]] = i;
  57. }
  58. int penalty() {
  59.     int cnt = 0;
  60.     FOR(i, 0, n) FOR(j, 0, n) {
  61.         if (g[i][j] > g[i][hori[i]] && g[i][j] < g[vert[j]][j]) cnt++;
  62.         if (g[i][j] < g[i][hori[i]] && g[i][j] > g[vert[j]][j]) cnt++;
  63.     }
  64.     FOR(i, 0, n) cnt -= c[i][hori[i]];
  65.     return cnt;
  66. }
  67. void change(int p1, int p2) {
  68.     swap(a[p1], a[p2]);
  69.     FOR(i, 0, n) hori[i] = a[i], vert[a[i]] = i;
  70. }
  71.  
  72. void MLE() { string a[20000000]; }
  73.  
  74. int main() {
  75.     cin >> n;
  76.     a.resize(n);
  77.     FOR(i, 0, n) FOR(j, 0, n) cin >> g[i][j];
  78.     FOR(i, 0, n) FOR(j, 0, n) cin >> c[i][j];
  79.     int best = 1e9;
  80.     FOR(att, 0, 5) {
  81.         a.resize(n);
  82.         gen();
  83.         cur = penalty();
  84.         FOR(i, 0, ITER) {
  85.             int now = cur;
  86.             int p1 = randint(n), p2 = randint(n);
  87.             while (p2 == p1) p2 = randint(n);
  88.             change(p1, p2);
  89.             cur = penalty();
  90.             if (cur > now) {
  91.                 change(p1, p2);
  92.                 cur = now;
  93.             }
  94.         }
  95.         best = min(best, cur);
  96.     }
  97.     cout << -best;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement