Submission #65212780
Source Code Expand
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; struct AHC046 { int N, M; vector<pair<int, int>> pos; vector<vector<int>> board_target; vector<vector<int>> board_block; vector<pair<char, char>> ans; AHC046() { input(); } void input() { cin >> N >> M; pos.resize(M); for (int k = 0; k < M; ++k) { cin >> pos[k].first >> pos[k].second; } board_target.assign(N, vector<int>(N, -1)); board_block.assign(N, vector<int>(N, false)); for (int k = 1; k < M; ++k) { int r = pos[k].first, c = pos[k].second; board_target[r][c] = k; } ans.clear(); } void output() const { for (auto &pr : ans) cout << pr.first << ' ' << pr.second << '\n'; } int calc_score(const vector<pair<char, char>> &seq) const { int cur_r = pos[0].first, cur_c = pos[0].second; int next_target = 1, visited = 0; int maxT = 2 * N * M; vector<vector<int>> local_block = board_block; for (int t = 0; t < (int)seq.size() && t < maxT; ++t) { char act = seq[t].first, dir = seq[t].second; int dr = (dir == 'U' ? -1 : dir == 'D' ? 1 : 0); int dc = (dir == 'L' ? -1 : dir == 'R' ? 1 : 0); if (act == 'M') { int nr = cur_r + dr, nc = cur_c + dc; if (nr < 0 || nr >= N || nc < 0 || nc >= N || local_block[nr][nc]) break; cur_r = nr; cur_c = nc; } else if (act == 'S') { int nr = cur_r, nc = cur_c; while (true) { int tr = nr + dr, tc = nc + dc; if (tr < 0 || tr >= N || tc < 0 || tc >= N || local_block[tr][tc]) break; nr = tr; nc = tc; } cur_r = nr; cur_c = nc; } else if (act == 'A') { int br = cur_r + dr, bc = cur_c + dc; if (br >= 0 && br < N && bc >= 0 && bc < N) local_block[br][bc] = !local_block[br][bc]; } if (next_target < M && board_target[cur_r][cur_c] == next_target) { visited++; next_target++; } } if (visited < M - 1) return visited + 1; return M + max(0, maxT - (int)seq.size()); } int greedy() { ans.clear(); int cur_r = pos[0].first, cur_c = pos[0].second; for (int k = 1; k < M; ++k) { int tr = pos[k].first, tc = pos[k].second; int dr = tr - cur_r; int dc = tc - cur_c; char vdir = dr > 0 ? 'D' : 'U'; char hdir = dc > 0 ? 'R' : 'L'; vector<vector<pair<char, char>>> cands(4); // 0: no slide for (int i = 0; i < abs(dr); ++i) cands[0].emplace_back('M', vdir); for (int i = 0; i < abs(dc); ++i) cands[0].emplace_back('M', hdir); // 1: one horizontal slide for (int i = 0; i < abs(dr); ++i) cands[1].emplace_back('M', vdir); cands[1].emplace_back('S', hdir); int slide_h_c = (hdir == 'R' ? N - 1 : 0); int back_h = abs(slide_h_c - tc); char back_hdir = (slide_h_c > tc ? 'L' : 'R'); for (int i = 0; i < back_h; ++i) cands[1].emplace_back('M', back_hdir); // 2: one vertical slide cands[2].emplace_back('S', vdir); int slide_v_r = (vdir == 'D' ? N - 1 : 0); int back_v = abs(slide_v_r - tr); char back_vdir = (slide_v_r > tr ? 'U' : 'D'); for (int i = 0; i < back_v; ++i) cands[2].emplace_back('M', back_vdir); for (int i = 0; i < abs(dc); ++i) cands[2].emplace_back('M', hdir); // 3: two slides cands[3].emplace_back('S', vdir); cands[3].emplace_back('S', hdir); int slide2_v_r = (vdir == 'D' ? N - 1 : 0); int slide2_h_c = (hdir == 'R' ? N - 1 : 0); int back_v2 = abs(slide2_v_r - tr); char back_v2dir = (slide2_v_r > tr ? 'U' : 'D'); for (int i = 0; i < back_v2; ++i) cands[3].emplace_back('M', back_v2dir); int back_h2 = abs(slide2_h_c - tc); char back_h2dir = (slide2_h_c > tc ? 'L' : 'R'); for (int i = 0; i < back_h2; ++i) cands[3].emplace_back('M', back_h2dir); // pick shortest int best = 0, best_len = cands[0].size(); for (int i = 1; i < 4; ++i) { if ((int)cands[i].size() < best_len) { best = i; best_len = cands[i].size(); } } // append and update current pos for (auto &p : cands[best]) ans.push_back(p); cur_r = tr; cur_c = tc; } return calc_score(ans); } vector<pair<char, char>> new_make_process(const vector<vector<int>> &bblock, int next_tar, int si, int sj) const { vector<int> DR = {1, 0, -1, 0}; vector<int> DC = {0, 1, 0, -1}; vector<char> DIRC = {'D', 'R', 'U', 'L'}; auto make_path = [&](int sr, int sc, int tr, int tc) { vector<vector<int>> dist(N, vector<int>(N, INT_MAX)); struct State { int pr, pc; char act, dir; }; vector<vector<State>> prevv(N, vector<State>(N)); queue<pair<int, int>> q; dist[sr][sc] = 0; q.push({sr, sc}); while (!q.empty()) { auto [r, c] = q.front(); q.pop(); if (r == tr && c == tc) break; int d = dist[r][c]; // Move for (int k = 0; k < 4; ++k) { int nr = r + DR[k], nc = c + DC[k]; if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue; if (bblock[nr][nc]) continue; if (dist[nr][nc] > d + 1) { dist[nr][nc] = d + 1; prevv[nr][nc] = {r, c, 'M', DIRC[k]}; q.push({nr, nc}); } } // Slide for (int k = 0; k < 4; ++k) { int dr = DR[k], dc = DC[k]; int cr = r, cc = c; while (true) { int nr = cr + dr, nc = cc + dc; if (nr < 0 || nr >= N || nc < 0 || nc >= N) break; if (bblock[nr][nc]) break; cr = nr; cc = nc; } if (dist[cr][cc] > d + 1) { dist[cr][cc] = d + 1; prevv[cr][cc] = {r, c, 'S', DIRC[k]}; q.push({cr, cc}); } } } // 経路再構築 if (dist[tr][tc] == INT_MAX) { // 到達不能 return vector<pair<char, char>>(); } vector<pair<char, char>> path; int cr = tr, cc = tc; while (!(cr == sr && cc == sc)) { auto &st = prevv[cr][cc]; path.emplace_back(st.act, st.dir); cr = st.pr; cc = st.pc; } reverse(path.begin(), path.end()); return path; }; vector<pair<char, char>> ret; while (next_tar < M) { int tr = pos[next_tar].first; int tc = pos[next_tar].second; auto path = make_path(si, sj, tr, tc); // 経路なしなら WA if (path.empty() && (si != tr || sj != tc)) { return vector<pair<char, char>>{{'W', 'A'}}; } // 経路を追加 for (auto &p : path) { ret.push_back(p); } si = tr; sj = tc; ++next_tar; } return ret; } void new_make_block(int id, char dir) { vector<pair<int, int>> path; vector<pair<char, char>> new_ans; vector<vector<int>> board_block(N, vector<int>(N, 0)); int cur_r = pos[0].first, cur_c = pos[0].second; int next_tar = 1; for (int i = 0; i < id; ++i) new_ans.push_back(ans[i]); new_ans.push_back({'A', dir}); for (auto [act, dir] : new_ans) { int dr = (dir == 'U' ? -1 : dir == 'D' ? 1 : 0); int dc = (dir == 'L' ? -1 : dir == 'R' ? 1 : 0); if (act == 'M') { int nr = cur_r + dr, nc = cur_c + dc; if (nr < 0 || nr >= N || nc < 0 || nc >= N || board_block[nr][nc]) break; cur_r = nr; cur_c = nc; } else if (act == 'S') { int nr = cur_r, nc = cur_c; while (true) { int tr = nr + dr, tc = nc + dc; if (tr < 0 || tr >= N || tc < 0 || tc >= N || board_block[tr][tc]) break; nr = tr; nc = tc; } cur_r = nr; cur_c = nc; } else if (act == 'A') { int br = cur_r + dr, bc = cur_c + dc; if (br >= 0 && br < N && bc >= 0 && bc < N) board_block[br][bc] ^= 1; else { return; } if (board_block[br][bc] && board_target[br][bc] >= next_tar) { return; } } if (next_tar < M && board_target[cur_r][cur_c] == next_tar) { next_tar++; } } auto add_path = new_make_process(board_block, next_tar, cur_r, cur_c); if (add_path[0].first == 'W') { return; } for (auto p : add_path) { new_ans.push_back(p); } if (ans.size() > new_ans.size()) { ans = new_ans; } } int climbing() { greedy(); // for (int i = 0; i < 1000; ++i) { // int id = rand() % ans.size(); // char dir = "UDLR"[rand() % 4]; // new_make_block(id, dir); // } for (int i = 0; i < 1000; ++i) { if (ans.size() <= i) break; for (auto d = 0; d < 4; ++d) { char dir = "UDLR"[d]; new_make_block(i, dir); } } return calc_score(ans); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); AHC046 solver; solver.climbing(); solver.output(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | A - Skating with Blocks |
User | east1016 |
Language | C++ 23 (gcc 12.2) |
Score | 216851 |
Code Size | 11759 Byte |
Status | AC |
Exec Time | 367 ms |
Memory | 3660 KiB |
Compile Error
Main.cpp: In member function ‘int AHC046::climbing()’: Main.cpp:314:28: warning: comparison of integer expressions of different signedness: ‘std::vector<std::pair<char, char> >::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare] 314 | if (ans.size() <= i) | ~~~~~~~~~~~^~~~
Judge Result
Set Name | test_ALL | ||
---|---|---|---|
Score / Max Score | 216851 / 246000 | ||
Status |
|
Set Name | Test Cases |
---|---|
test_ALL | test_0000.txt, test_0001.txt, test_0002.txt, test_0003.txt, test_0004.txt, test_0005.txt, test_0006.txt, test_0007.txt, test_0008.txt, test_0009.txt, test_0010.txt, test_0011.txt, test_0012.txt, test_0013.txt, test_0014.txt, test_0015.txt, test_0016.txt, test_0017.txt, test_0018.txt, test_0019.txt, test_0020.txt, test_0021.txt, test_0022.txt, test_0023.txt, test_0024.txt, test_0025.txt, test_0026.txt, test_0027.txt, test_0028.txt, test_0029.txt, test_0030.txt, test_0031.txt, test_0032.txt, test_0033.txt, test_0034.txt, test_0035.txt, test_0036.txt, test_0037.txt, test_0038.txt, test_0039.txt, test_0040.txt, test_0041.txt, test_0042.txt, test_0043.txt, test_0044.txt, test_0045.txt, test_0046.txt, test_0047.txt, test_0048.txt, test_0049.txt, test_0050.txt, test_0051.txt, test_0052.txt, test_0053.txt, test_0054.txt, test_0055.txt, test_0056.txt, test_0057.txt, test_0058.txt, test_0059.txt, test_0060.txt, test_0061.txt, test_0062.txt, test_0063.txt, test_0064.txt, test_0065.txt, test_0066.txt, test_0067.txt, test_0068.txt, test_0069.txt, test_0070.txt, test_0071.txt, test_0072.txt, test_0073.txt, test_0074.txt, test_0075.txt, test_0076.txt, test_0077.txt, test_0078.txt, test_0079.txt, test_0080.txt, test_0081.txt, test_0082.txt, test_0083.txt, test_0084.txt, test_0085.txt, test_0086.txt, test_0087.txt, test_0088.txt, test_0089.txt, test_0090.txt, test_0091.txt, test_0092.txt, test_0093.txt, test_0094.txt, test_0095.txt, test_0096.txt, test_0097.txt, test_0098.txt, test_0099.txt, test_0100.txt, test_0101.txt, test_0102.txt, test_0103.txt, test_0104.txt, test_0105.txt, test_0106.txt, test_0107.txt, test_0108.txt, test_0109.txt, test_0110.txt, test_0111.txt, test_0112.txt, test_0113.txt, test_0114.txt, test_0115.txt, test_0116.txt, test_0117.txt, test_0118.txt, test_0119.txt, test_0120.txt, test_0121.txt, test_0122.txt, test_0123.txt, test_0124.txt, test_0125.txt, test_0126.txt, test_0127.txt, test_0128.txt, test_0129.txt, test_0130.txt, test_0131.txt, test_0132.txt, test_0133.txt, test_0134.txt, test_0135.txt, test_0136.txt, test_0137.txt, test_0138.txt, test_0139.txt, test_0140.txt, test_0141.txt, test_0142.txt, test_0143.txt, test_0144.txt, test_0145.txt, test_0146.txt, test_0147.txt, test_0148.txt, test_0149.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
test_0000.txt | AC | 327 ms | 3536 KiB |
test_0001.txt | AC | 259 ms | 3520 KiB |
test_0002.txt | AC | 284 ms | 3460 KiB |
test_0003.txt | AC | 273 ms | 3660 KiB |
test_0004.txt | AC | 257 ms | 3476 KiB |
test_0005.txt | AC | 218 ms | 3440 KiB |
test_0006.txt | AC | 298 ms | 3480 KiB |
test_0007.txt | AC | 300 ms | 3532 KiB |
test_0008.txt | AC | 293 ms | 3476 KiB |
test_0009.txt | AC | 278 ms | 3444 KiB |
test_0010.txt | AC | 342 ms | 3572 KiB |
test_0011.txt | AC | 321 ms | 3448 KiB |
test_0012.txt | AC | 238 ms | 3452 KiB |
test_0013.txt | AC | 214 ms | 3500 KiB |
test_0014.txt | AC | 255 ms | 3660 KiB |
test_0015.txt | AC | 250 ms | 3536 KiB |
test_0016.txt | AC | 257 ms | 3380 KiB |
test_0017.txt | AC | 299 ms | 3468 KiB |
test_0018.txt | AC | 303 ms | 3444 KiB |
test_0019.txt | AC | 298 ms | 3652 KiB |
test_0020.txt | AC | 286 ms | 3512 KiB |
test_0021.txt | AC | 272 ms | 3372 KiB |
test_0022.txt | AC | 367 ms | 3452 KiB |
test_0023.txt | AC | 319 ms | 3448 KiB |
test_0024.txt | AC | 226 ms | 3464 KiB |
test_0025.txt | AC | 251 ms | 3528 KiB |
test_0026.txt | AC | 296 ms | 3520 KiB |
test_0027.txt | AC | 363 ms | 3476 KiB |
test_0028.txt | AC | 245 ms | 3456 KiB |
test_0029.txt | AC | 331 ms | 3460 KiB |
test_0030.txt | AC | 192 ms | 3368 KiB |
test_0031.txt | AC | 280 ms | 3492 KiB |
test_0032.txt | AC | 283 ms | 3472 KiB |
test_0033.txt | AC | 226 ms | 3520 KiB |
test_0034.txt | AC | 278 ms | 3576 KiB |
test_0035.txt | AC | 264 ms | 3376 KiB |
test_0036.txt | AC | 257 ms | 3496 KiB |
test_0037.txt | AC | 283 ms | 3444 KiB |
test_0038.txt | AC | 322 ms | 3468 KiB |
test_0039.txt | AC | 339 ms | 3436 KiB |
test_0040.txt | AC | 296 ms | 3432 KiB |
test_0041.txt | AC | 278 ms | 3432 KiB |
test_0042.txt | AC | 281 ms | 3508 KiB |
test_0043.txt | AC | 223 ms | 3464 KiB |
test_0044.txt | AC | 324 ms | 3580 KiB |
test_0045.txt | AC | 230 ms | 3448 KiB |
test_0046.txt | AC | 274 ms | 3436 KiB |
test_0047.txt | AC | 236 ms | 3480 KiB |
test_0048.txt | AC | 315 ms | 3472 KiB |
test_0049.txt | AC | 254 ms | 3512 KiB |
test_0050.txt | AC | 246 ms | 3536 KiB |
test_0051.txt | AC | 257 ms | 3516 KiB |
test_0052.txt | AC | 339 ms | 3576 KiB |
test_0053.txt | AC | 230 ms | 3464 KiB |
test_0054.txt | AC | 322 ms | 3472 KiB |
test_0055.txt | AC | 288 ms | 3480 KiB |
test_0056.txt | AC | 271 ms | 3368 KiB |
test_0057.txt | AC | 229 ms | 3488 KiB |
test_0058.txt | AC | 351 ms | 3380 KiB |
test_0059.txt | AC | 316 ms | 3512 KiB |
test_0060.txt | AC | 252 ms | 3492 KiB |
test_0061.txt | AC | 266 ms | 3648 KiB |
test_0062.txt | AC | 272 ms | 3536 KiB |
test_0063.txt | AC | 272 ms | 3520 KiB |
test_0064.txt | AC | 296 ms | 3520 KiB |
test_0065.txt | AC | 259 ms | 3660 KiB |
test_0066.txt | AC | 324 ms | 3448 KiB |
test_0067.txt | AC | 344 ms | 3488 KiB |
test_0068.txt | AC | 251 ms | 3448 KiB |
test_0069.txt | AC | 280 ms | 3516 KiB |
test_0070.txt | AC | 295 ms | 3656 KiB |
test_0071.txt | AC | 304 ms | 3460 KiB |
test_0072.txt | AC | 254 ms | 3472 KiB |
test_0073.txt | AC | 249 ms | 3456 KiB |
test_0074.txt | AC | 254 ms | 3476 KiB |
test_0075.txt | AC | 271 ms | 3468 KiB |
test_0076.txt | AC | 218 ms | 3456 KiB |
test_0077.txt | AC | 271 ms | 3504 KiB |
test_0078.txt | AC | 290 ms | 3380 KiB |
test_0079.txt | AC | 305 ms | 3428 KiB |
test_0080.txt | AC | 316 ms | 3468 KiB |
test_0081.txt | AC | 224 ms | 3492 KiB |
test_0082.txt | AC | 253 ms | 3440 KiB |
test_0083.txt | AC | 287 ms | 3508 KiB |
test_0084.txt | AC | 278 ms | 3468 KiB |
test_0085.txt | AC | 306 ms | 3444 KiB |
test_0086.txt | AC | 214 ms | 3472 KiB |
test_0087.txt | AC | 275 ms | 3508 KiB |
test_0088.txt | AC | 283 ms | 3488 KiB |
test_0089.txt | AC | 305 ms | 3464 KiB |
test_0090.txt | AC | 321 ms | 3656 KiB |
test_0091.txt | AC | 263 ms | 3532 KiB |
test_0092.txt | AC | 330 ms | 3492 KiB |
test_0093.txt | AC | 338 ms | 3512 KiB |
test_0094.txt | AC | 256 ms | 3460 KiB |
test_0095.txt | AC | 229 ms | 3476 KiB |
test_0096.txt | AC | 265 ms | 3520 KiB |
test_0097.txt | AC | 251 ms | 3464 KiB |
test_0098.txt | AC | 320 ms | 3440 KiB |
test_0099.txt | AC | 258 ms | 3460 KiB |
test_0100.txt | AC | 211 ms | 3444 KiB |
test_0101.txt | AC | 227 ms | 3432 KiB |
test_0102.txt | AC | 278 ms | 3460 KiB |
test_0103.txt | AC | 233 ms | 3516 KiB |
test_0104.txt | AC | 334 ms | 3656 KiB |
test_0105.txt | AC | 243 ms | 3444 KiB |
test_0106.txt | AC | 284 ms | 3532 KiB |
test_0107.txt | AC | 261 ms | 3476 KiB |
test_0108.txt | AC | 296 ms | 3524 KiB |
test_0109.txt | AC | 230 ms | 3580 KiB |
test_0110.txt | AC | 252 ms | 3540 KiB |
test_0111.txt | AC | 296 ms | 3468 KiB |
test_0112.txt | AC | 269 ms | 3480 KiB |
test_0113.txt | AC | 355 ms | 3476 KiB |
test_0114.txt | AC | 231 ms | 3476 KiB |
test_0115.txt | AC | 298 ms | 3420 KiB |
test_0116.txt | AC | 277 ms | 3584 KiB |
test_0117.txt | AC | 235 ms | 3576 KiB |
test_0118.txt | AC | 335 ms | 3496 KiB |
test_0119.txt | AC | 302 ms | 3476 KiB |
test_0120.txt | AC | 269 ms | 3580 KiB |
test_0121.txt | AC | 268 ms | 3468 KiB |
test_0122.txt | AC | 228 ms | 3448 KiB |
test_0123.txt | AC | 260 ms | 3520 KiB |
test_0124.txt | AC | 228 ms | 3468 KiB |
test_0125.txt | AC | 239 ms | 3576 KiB |
test_0126.txt | AC | 224 ms | 3540 KiB |
test_0127.txt | AC | 225 ms | 3580 KiB |
test_0128.txt | AC | 296 ms | 3656 KiB |
test_0129.txt | AC | 247 ms | 3376 KiB |
test_0130.txt | AC | 325 ms | 3372 KiB |
test_0131.txt | AC | 250 ms | 3656 KiB |
test_0132.txt | AC | 254 ms | 3440 KiB |
test_0133.txt | AC | 329 ms | 3464 KiB |
test_0134.txt | AC | 254 ms | 3572 KiB |
test_0135.txt | AC | 275 ms | 3508 KiB |
test_0136.txt | AC | 241 ms | 3436 KiB |
test_0137.txt | AC | 296 ms | 3448 KiB |
test_0138.txt | AC | 223 ms | 3444 KiB |
test_0139.txt | AC | 295 ms | 3464 KiB |
test_0140.txt | AC | 307 ms | 3380 KiB |
test_0141.txt | AC | 252 ms | 3572 KiB |
test_0142.txt | AC | 314 ms | 3584 KiB |
test_0143.txt | AC | 226 ms | 3492 KiB |
test_0144.txt | AC | 366 ms | 3492 KiB |
test_0145.txt | AC | 234 ms | 3460 KiB |
test_0146.txt | AC | 296 ms | 3372 KiB |
test_0147.txt | AC | 221 ms | 3512 KiB |
test_0148.txt | AC | 316 ms | 3464 KiB |
test_0149.txt | AC | 243 ms | 3436 KiB |