#include #include #include #include #include #include #include #include #include #include #include #include #include #include using ll = long long; enum : int { M = (int)1e9 + 7 }; enum : ll { MLL = (ll)1e18L + 9 }; using namespace std; #ifdef LOCAL #include"rprint2.hpp" #include"debug_deque.hpp" #define vector DebugDeque #else #define FUNC(name) template void name(T&&...){ } FUNC(prints) FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde); #endif template istream& operator >> (istream& in, pair& p){ in >> p.first >> p.second; return in; } template istream& operator >> (istream& in, vector& v){ for(auto& e : v){ in >> e; } return in; } #define LIMIT 0.9 mt19937 mt; timeval start; double elapsed(){ timeval now; gettimeofday(&now, nullptr); return (now.tv_sec - start.tv_sec) + (now.tv_usec - start.tv_usec) / 1e6; } struct Ans { int a, b, c, d; friend ostream& operator << (ostream& out, Ans& e) { return out << e.a + 1 << ' ' << e.b + 1 << ' ' << e.c + 1 << ' ' << e.d + 1; } bool operator < (const Ans&) const { return false; } }; int main(){ gettimeofday(&start, nullptr); cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector ls(k); cin >> ls; vector as(n); cin >> as; for(auto& a : as){ for(auto& e : a){ e -= '0'; } } auto as0 = as; auto calc = [&](){ int num = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ num += as[i][j]; } } return num; }; int score0 = calc(); vector ans(k); // vector> ps(k); // for(int i = 0; i < k; i++){ // ps[i] = {ls[i], i}; // } // sort(ps.rbegin(), ps.rend()); // for(auto p : ps){ // int i = p.second; auto rev = [](auto& e){ e = !e; }; auto put = [&](Ans a){ int ret = 0; for(int y = a.a; y <= a.c; y++){ for(int x = a.b; x <= a.d; x++){ rev(as[y][x]); ret += as[y][x] * 2 - 1; } } return ret; }; auto findBest = [&](int l){ pair ret(-1, {}); for(int y = 0; y < n; y++){ for(int x = 0; x < n - l; x++){ pair cand(0, {y, x, y, x + l - 1}); for(int j = 0; j < l; j++){ cand.first += as[y][x + j]; } ret = max(ret, cand); } } for(int y = 0; y < n - l; y++){ for(int x = 0; x < n; x++){ pair cand(0, {y, x, y + l - 1, x}); for(int j = 0; j < l; j++){ cand.first += as[y + j][x]; } ret = max(ret, cand); } } return ret; }; // for(int i = 0; i < k; i++){ // put(ans[i] = findBest(ls[i]).second); // } // printde(score0 - calc()); int maxi = 0; auto as1 = as; auto ans1 = ans; vector is(k); iota(is.begin(), is.end(), 0); for(int _ = 0; _ < 10; _++){ as = as0; for(auto i : is){ put(ans[i] = findBest(ls[i]).second); } int cand = score0 - calc(); if(maxi < cand){ maxi = cand; ans1 = ans; as1 = as; } printde(cand); shuffle(is.begin(), is.end(), mt); } as = as1; ans = ans1; while(elapsed() < LIMIT){ for(int _ = 0; _ < 100; _++){ // for(elapsed() < LIMIT){ for(int i = 0; i < 100; i++){ int i = mt() % k; auto& a = ans[i]; put(a); a = findBest(ls[i]).second; put(a); if(a.a == a.c){ if(a.d + 1 < n && as[a.a][a.b] && as[a.a][a.d + 1]){ rev(as[a.a][a.b]); rev(as[a.a][a.d + 1]); a.b++; a.d++; } if(a.b && as[a.a][a.b - 1] && as[a.a][a.d]){ rev(as[a.a][a.b - 1]); rev(as[a.a][a.d]); a.b--; a.d--; } } if(a.b == a.d){ if(a.c + 1 < n && as[a.a][a.b] && as[a.c + 1][a.b]){ rev(as[a.a][a.b]); rev(as[a.c + 1][a.b]); a.a++; a.c++; } if(a.a && as[a.a - 1][a.b] && as[a.c][a.b]){ rev(as[a.a - 1][a.b]); rev(as[a.c][a.b]); a.a--; a.c--; } } } } for(auto& e : ans){ cout << e << '\n'; } vector> rs; for(int i = 0; i < k; i++){ int cnt = 0; for(int y = ans[i].a; y <= ans[i].c; y++){ for(int x = ans[i].b; x <= ans[i].d; x++){ cnt += as[y][x]; } } rs.emplace_back(1. * cnt / ls[i], i); } sort(rs.rbegin(), rs.rend()); // for(int i = 0; i < 10; i++){ // printe(rs[i].first, ls[rs[i].first]); // } int score = score0 - calc(); printde(score); }