ログイン
新規登録
AtsuoCoder Optimization Contest 001
読込中…
Home
Tasks
Clar
Submissions
Standings
提出 6a8d79fe-8914-4e6b-be50-5373bcc06167
コード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Op { int id, H, W, a, b; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, T; if (!(cin >> N >> T)) return 0; vector<vector<int>> A(N, vector<int>(N)), B(N, vector<int>(N)); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) cin >> A[i][j]; for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) cin >> B[i][j]; vector<Op> ops(T); for (int t = 0; t < T; ++t) { ops[t].id = t; cin >> ops[t].H >> ops[t].W >> ops[t].a >> ops[t].b; } const int LIMIT = 100000; vector<tuple<int, int, int>> out; out.reserve(10000); clock_t clk = clock(); vector<char> used(T, 0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); while ((double)(clock() - clk) / CLOCKS_PER_SEC < 2.5) { shuffle(ops.begin(), ops.end(), rng); for (int t = 0; t < T; ++t) { int id = ops[t].id, H = ops[t].H, W = ops[t].W, a = ops[t].a, b = ops[t].b; int uses = 0; while (used[id] < 3 && (int)out.size() < LIMIT) { vector<vector<ll>> pref(N + 1, vector<ll>(N + 1, 0)); bool anyPossible = false; for (int i = 0; i < N; ++i) { ll rowSum = 0; for (int j = 0; j < N; ++j) { ll w = 0; if (A[i][j] == a) { ll before = (ll)A[i][j] - (ll)B[i][j]; ll after = (ll)b - (ll)B[i][j]; w = after * after - before * before; anyPossible = true; } rowSum += w; pref[i + 1][j + 1] = pref[i][j + 1] + rowSum; } } if (!anyPossible) break; ll bestDelta = 0; int bestX = -1, bestY = -1; for (int x = 0; x + H <= N; ++x) { for (int y = 0; y + W <= N; ++y) { int x2 = x + H, y2 = y + W; ll s = pref[x2][y2] - pref[x][y2] - pref[x2][y] + pref[x][y]; if (s < bestDelta) { bestDelta = s; bestX = x; bestY = y; } } } if (bestX == -1) break; for (int i = bestX; i < bestX + H; ++i) { for (int j = bestY; j < bestY + W; ++j) { if (A[i][j] == a) A[i][j] = b; } } out.emplace_back(id, bestX, bestY); ++used[id]; } if ((int)out.size() >= LIMIT) break; } } cout << out.size() << '\n'; for (auto &tp : out) { int id, x, y; tie(id, x, y) = tp; cout << id << " " << x << " " << y << "\n"; } return 0; }
結果
問題
点数
言語
結果
実行時間
メモリ
A - Replace() は Replace() されました
38305411
C++
AC
2542 ms
4148 KiB