Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ull unsigned long long
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define vi vector <int>
- #define mi vector <vector <int> >
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- #define sz(a) a.size()
- #define pb push_back
- #define mp make_pair
- #define min(a, b) ((a < b) ? (a) : (b))
- #define max(a, b) ((a > b) ? (a) : (b))
- // Quick fors
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- // Pragmas
- #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
- #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
- #pragma GCC comment(linker, "/stack:200000000")
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- #define ook order_of_key
- #define fbo find_by_order
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- using namespace __gnu_pbds;
- mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
- int randint(int n) { // [0; n)
- return rng() % n;
- }
- const int ITER = 400000;
- vi a;
- int n, cur = 0;
- int g[128][128], c[128][128], hori[128], vert[128];;
- void gen() {
- vi p(n); FOR(i, 0, n) p[i] = i;
- FOR(i, 0, n) {
- int k = randint(n - i);
- a[i] = p[k];
- p.erase(p.begin() + k);
- }
- FOR(i, 0, n) hori[i] = a[i], vert[a[i]] = i;
- }
- int penalty() {
- int cnt = 0;
- FOR(i, 0, n) FOR(j, 0, n) {
- if (g[i][j] > g[i][hori[i]] && g[i][j] < g[vert[j]][j]) cnt++;
- if (g[i][j] < g[i][hori[i]] && g[i][j] > g[vert[j]][j]) cnt++;
- }
- FOR(i, 0, n) cnt -= c[i][hori[i]];
- return cnt;
- }
- void change(int p1, int p2) {
- swap(a[p1], a[p2]);
- FOR(i, 0, n) hori[i] = a[i], vert[a[i]] = i;
- }
- void MLE() { string a[20000000]; }
- int main() {
- cin >> n;
- a.resize(n);
- FOR(i, 0, n) FOR(j, 0, n) cin >> g[i][j];
- FOR(i, 0, n) FOR(j, 0, n) cin >> c[i][j];
- int best = 1e9;
- FOR(att, 0, 5) {
- a.resize(n);
- gen();
- cur = penalty();
- FOR(i, 0, ITER) {
- int now = cur;
- int p1 = randint(n), p2 = randint(n);
- while (p2 == p1) p2 = randint(n);
- change(p1, p2);
- cur = penalty();
- if (cur > now) {
- change(p1, p2);
- cur = now;
- }
- }
- best = min(best, cur);
- }
- cout << -best;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement