├── .gitignore ├── a57_f3_drainage.cpp ├── a57_f5_diceroll.cpp ├── a57_m3_tile.cpp ├── a57_m4_gaa.cpp ├── a57_m5_mcs2.cpp ├── a57_​f4_​journey.cpp ├── a58_m3_hanamard.cpp ├── a58_m4_supply.cpp ├── a58_m4_supply.exe ├── a58_q1_triple.cpp ├── a58_q3_p3a_hacking.cpp ├── a58_q3_p4_columbia2.cpp ├── a58_​q3_​p3_​PandG.cpp ├── a58_​q3_​p3a_​hacking.cpp ├── a58_​q3_​p4a_​chokepoint.cpp ├── a59_midp1_lcs.cpp ├── a59_midp3_similar.cpp ├── a59_midp4_mcsw.cpp ├── a59_q1_ones.cpp ├── a59_q3_cycle.cpp ├── a59_​q1_​quicksum.cpp ├── a59_​q3_​cycle.cpp ├── a59_​q3_​hole.cpp ├── a60_q1_matmod.cpp ├── a60_q1_number1.cpp ├── a60a_midp1_knapsack.cpp ├── a60a_midp2_avl_distance.cpp ├── a60a_midp3_bigger.cpp ├── a60a_midp4_walk.cpp ├── a60a_q3_component.cpp ├── a60a_q3_xor_road.cpp ├── a60a_​q3_​component.cpp ├── a60a_​q3_​xor_​road.cpp ├── a60b_mid_p1_buffet.cpp ├── a60b_mid_p4_merge.cpp ├── a60b_q1_diagsum.cpp ├── a60b_q3_perimeter.cpp ├── a60b_​mid_​p2_​rope.cpp ├── a60b_​mid_​p3_​divide.cpp ├── a60b_​q2_​pareto.cpp ├── a60b_​q3_​ikea.cpp ├── a61_q1_thanos.cpp ├── a61_q2_farthest.cpp ├── a61_​q2_​farthest.cpp ├── a62_q1a_recur.cpp ├── a62_q1b_chocolate.cpp ├── a62_q1b_virus.cpp ├── a62_q2a_line_graph.cpp ├── a62_q2a_pandemic.cpp ├── a62_​q1a_​jumping.cpp ├── a62_​q1b_​chocolate.cpp ├── a62_​q2a_​line_​graph.cpp ├── a63_q1a_101.cpp ├── a63_q1a_buffet.cpp ├── a63_q1b_2side.cpp ├── a63_q1b_virus2.cpp ├── a64_q1_lawn_mowing.cpp ├── a64_q1_permu_before.cpp ├── a64_q2_table_no_11.cpp ├── a64_q2_two_mcs.cpp ├── a64_q4_path_sum.cpp ├── a64_​q1_​lawn_​mowing.cpp ├── a64_​q3_​shortest_​start.cpp ├── a64_​q3_​wildfire.cpp ├── a64_​q4_​closest_​subset.cpp ├── a65_q0_degrees.cpp ├── a65_q1_mt_matrix.cpp ├── a65_q1_tower_defense.cpp ├── a65_q1b_street_art.cpp ├── a65_q2a_distance_one.cpp ├── a65_q2a_huge_array.cpp ├── a65_q2b_arch_match.cpp ├── a65_q3a_expressway.cpp ├── a65_q4_collector.cpp ├── a65_​q3a_​in_​the_​middle.cpp ├── a65_​q4_​tower_​defense_​2.cpp ├── a66_f1_largest_diff_path.cpp ├── a66_f1_make_zero.cpp ├── a66_​f1_​ev_​truck.cpp ├── a66_​q1a_​day_​cover.cpp ├── a66_​q1a_​day_​cover_bitset.cpp ├── a66_​q1a_​double_​end_​sequence.cpp ├── a66_​q1b_​triforce.cpp ├── a66_​q2a_​eating_​futomaki.cpp ├── a66_​q2a_​insertion_​count.cpp ├── a66_​q2b_​multi_​recurrence.cpp ├── a66_​q3a_​hex_​map.cpp ├── a66_​q3a_​sandworm.cpp ├── a66_​q3b_​hex_​map_​v2.cpp ├── a66_​q4a_​billboard_​feng_​shui.cpp ├── a66_​q4a_​clique.cpp ├── da66_f1_buffet_flight.cpp ├── da66_f1_linear_recurrence.cpp ├── da66_f1_map_walk.cpp ├── da66_f1_sea_bridge.cpp ├── da66_f2_grid_loop.cpp ├── da66_f2_new_game_plus.cpp ├── da66_m1_line_paint.cpp ├── ex00e4.cpp ├── ex00e5.cpp ├── ex01e2.cpp ├── ex01e3.cpp ├── ex01e4.cpp ├── ex01h1.cpp ├── ex01m1.cpp ├── ex01m2.cpp ├── ex01m3.cpp ├── ex02e1_fibo.cpp ├── ex02e2_​cnk.cpp ├── ex02e3_bs.cpp ├── ex02h1_closest.cpp ├── ex02m1_triangle.cpp ├── ex02m2_barcode.cpp ├── ex03e1_​lcs.cpp ├── ex03e2_​mcm.cpp ├── ex03e3_​coin.cpp ├── ex03h1_​square.cpp ├── ex03m1_billboardex03m1_billboard.cpp ├── ex03m2_lis.cpp ├── ex03m3.cpp ├── ex04e1_interval.cpp ├── ex04e1_​interval.cpp ├── ex04e2_fknapsack.cpp ├── ex04e2_​fknapsack.cpp ├── ex04e3_teamwork.cpp ├── ex04h1_montblanc.cpp ├── ex04m1_sort.cpp ├── ex04m1_​sort.cpp ├── ex04m2_​salesman.cpp ├── ex05e1_shortest.cpp ├── ex05e2_​planning.cpp ├── ex05e3_catapult.cpp ├── ex05h1_​rank.cpp ├── ex05m1_cycle.cpp ├── ex05m2_connection.cpp ├── ex06e1_cable.cpp ├── ex06e1_​cable.cpp ├── ex06e2_shortest.cpp ├── ex06e3_columbia.cpp ├── ex06h1_karin.cpp ├── ex06m1_​massrelay.cpp ├── ex06m2_exchange.cpp ├── ex07e1_nqueen.cpp ├── ex07e2_iss.cpp ├── ex07e3_coloring.cpp ├── ex07m1_​knapsack.cpp ├── ex07m2_15puzzle.cpp ├── midterm64.pdf └── midterm65.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | **/a.exe 2 | **/test.cpp 3 | **/.vscode 4 | **.in 5 | **.out -------------------------------------------------------------------------------- /a57_f3_drainage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, L; 8 | cin >> N >> L; 9 | 10 | vector p(N); 11 | for (auto &x : p) cin >> x; 12 | 13 | sort(p.begin(), p.end()); 14 | 15 | int cur = 0, ans = 0; 16 | for (int i = 0; i < N; i++) { 17 | if (cur < p[i]) { 18 | ans++; 19 | cur = p[i] + L - 1; 20 | } 21 | } 22 | cout << ans; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /a57_f5_diceroll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int R, C, P, Q; 10 | cin >> R >> C >> P >> Q; 11 | 12 | vector cost(6); 13 | for (int i = 0; i < 6; i++) cin >> cost[i]; 14 | 15 | vector , int>>> dist(R, vector , int>> (C)); 16 | vector , int>>> cnt(R, vector , int>> (C)); 17 | vector p(6); 18 | iota(p.begin(), p.end(), 0); 19 | do { 20 | for (int i = 0; i < R; i++) { 21 | for (int j = 0; j < C; j++) { 22 | dist[i][j][p] = INF; 23 | } 24 | } 25 | } while (next_permutation(p.begin(), p.end())); 26 | 27 | queue >> q; 28 | vector st({0, 1, 2, 3, 4, 5}); 29 | dist[0][0][st] = 0; 30 | q.emplace(0, 0, st); 31 | while (!q.empty()) { 32 | auto [x, y, u] = q.front(); 33 | q.pop(); 34 | 35 | if (++cnt[x][y][u] > R * C * 720) { 36 | cout << -1; 37 | return 0; 38 | } 39 | 40 | if (y - 1 >= 0) { 41 | vector v(u); 42 | swap(v[0], v[2]); 43 | swap(v[1], v[3]); 44 | swap(v[0], v[1]); 45 | if (dist[x][y][u] + cost[v[0]] < dist[x][y - 1][v]) { 46 | dist[x][y - 1][v] = dist[x][y][u] + cost[v[0]]; 47 | q.emplace(x, y - 1, v); 48 | } 49 | } 50 | if (y + 1 < C) { 51 | vector v(u); 52 | swap(v[0], v[2]); 53 | swap(v[1], v[3]); 54 | swap(v[2], v[3]); 55 | if (dist[x][y][u] + cost[v[0]] < dist[x][y + 1][v]) { 56 | dist[x][y + 1][v] = dist[x][y][u] + cost[v[0]]; 57 | q.emplace(x, y + 1, v); 58 | } 59 | } 60 | if (x - 1 >= 0) { 61 | vector v(u); 62 | swap(v[0], v[4]); 63 | swap(v[1], v[5]); 64 | swap(v[4], v[5]); 65 | if (dist[x][y][u] + cost[v[0]] < dist[x - 1][y][v]) { 66 | dist[x - 1][y][v] = dist[x][y][u] + cost[v[0]]; 67 | q.emplace(x - 1, y, v); 68 | } 69 | } 70 | if (x + 1 < R) { 71 | vector v(u); 72 | swap(v[0], v[4]); 73 | swap(v[1], v[5]); 74 | swap(v[0], v[1]); 75 | if (dist[x][y][u] + cost[v[0]] < dist[x + 1][y][v]) { 76 | dist[x + 1][y][v] = dist[x][y][u] + cost[v[0]]; 77 | q.emplace(x + 1, y, v); 78 | } 79 | } 80 | } 81 | 82 | iota(p.begin(), p.end(), 0); 83 | int ans = INF; 84 | do { 85 | ans = min(ans, dist[P][Q][p]); 86 | } while (next_permutation(p.begin(), p.end())); 87 | cout << ans; 88 | return 0; 89 | } -------------------------------------------------------------------------------- /a57_m3_tile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N, M; 10 | cin >> N >> M; 11 | 12 | vector > dp(N + 1, vector (M + 1, INF)); 13 | dp[0][0] = 0; 14 | for (int i = 1; i <= N; i++) { 15 | int a; 16 | cin >> a; 17 | 18 | for (int j = M; j >= 0; j--) { 19 | for (int k = 1; k <= 100; k++) { 20 | if (j - k * k >= 0) { 21 | dp[i][j] = min(dp[i][j], dp[i - 1][j - k * k] + (a - k) * (a - k)); 22 | } 23 | } 24 | } 25 | } 26 | 27 | if (dp[N][M] == INF) dp[N][M] = -1; 28 | cout << dp[N][M]; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /a57_m4_gaa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char solve(int k, int n, int x) { 5 | int m = (n - k) / 2; 6 | if (x <= m) return solve(k - 1, m, x); 7 | if (x - m <= k) { 8 | if (x - m == 1) return 'g'; 9 | return 'a'; 10 | } 11 | return solve(k - 1, m, x - m - k); 12 | } 13 | 14 | int main() { 15 | cin.tie(nullptr)->sync_with_stdio(false); 16 | 17 | int N; 18 | cin >> N; 19 | 20 | int s = 3, k = 3; 21 | while (s < N) { 22 | k++; 23 | s = s * 2 + k; 24 | } 25 | cout << solve(k, s, N); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /a57_m5_mcs2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | const int INF = 1e9 + 7; 6 | 7 | int A[MAX_N], sum; 8 | 9 | int solve(int l, int r) { 10 | if (l == r) return A[l]; 11 | 12 | int mid = (l + r) / 2; 13 | int res = max(solve(l, mid), solve(mid + 1, r)); 14 | 15 | int minLeft = INF, minRight = INF; 16 | int maxLeft = -INF, maxRight = -INF; 17 | int sumLeft = 0, sumRight = 0; 18 | for (int i = mid; i >= l; i--) { 19 | sumLeft += A[i]; 20 | minLeft = min(minLeft, sumLeft); 21 | maxLeft = max(maxLeft, sumLeft); 22 | } 23 | for (int i = mid + 1; i <= r; i++) { 24 | sumRight += A[i]; 25 | minRight = min(minRight, sumRight); 26 | maxRight = max(maxRight, sumRight); 27 | } 28 | 29 | res = max({res, sum - minLeft - minRight, maxLeft + maxRight}); 30 | return res; 31 | } 32 | 33 | int main() { 34 | cin.tie(nullptr)->sync_with_stdio(false); 35 | 36 | int N; 37 | cin >> N; 38 | 39 | int maxEle = -INF; 40 | for (int i = 1; i <= N; i++) { 41 | cin >> A[i]; 42 | 43 | sum += A[i]; 44 | maxEle = max(maxEle, A[i]); 45 | } 46 | 47 | if (maxEle <= 0) cout << maxEle; 48 | else cout << solve(1, N); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /a57_​f4_​journey.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N; 10 | cin >> N; 11 | 12 | vector > w(N, vector (N)); 13 | for (int i = 0; i < N; i++) { 14 | for (int j = 0; j < N; j++) { 15 | cin >> w[i][j]; 16 | } 17 | } 18 | 19 | vector > dp(N, vector (1< 2 | using namespace std; 3 | 4 | vector v; 5 | 6 | vector solve(int l, int r) { 7 | if (l == r) return {v[l]}; 8 | 9 | int m = (l + r) / 2; 10 | vector res[4]; 11 | res[0] = solve(l, m); 12 | res[1] = solve(m + 1, r); 13 | for (int i = 0; i < m - l + 1; i++) { 14 | res[2].push_back(res[0][i]); 15 | res[3].push_back(-res[1][i]); 16 | } 17 | for (int i = 0; i < m - l + 1; i++) { 18 | res[0][i] += res[1][i]; 19 | res[2][i] += res[3][i]; 20 | } 21 | res[0].insert(res[0].end(), res[2].begin(), res[2].end()); 22 | return res[0]; 23 | } 24 | 25 | int main() { 26 | cin.tie(nullptr)->sync_with_stdio(false); 27 | 28 | int n; 29 | cin >> n; 30 | 31 | v.resize(n); 32 | for (auto &x : v) cin >> x; 33 | 34 | vector ans(solve(0, n - 1)); 35 | for (auto x : ans) cout << x << ' '; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /a58_m4_supply.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, M, K; 8 | cin >> N >> M >> K; 9 | 10 | vector D(K), E(K), L(K); 11 | for (int i = 0; i < K; i++) cin >> D[i] >> E[i] >> L[i]; 12 | 13 | vector idx(K); 14 | iota(idx.begin(), idx.end(), 0); 15 | sort(idx.begin(), idx.end(), [&](const int &a, const int &b) { 16 | return D[a] < D[b]; 17 | }); 18 | 19 | queue plants, stores; 20 | for (int i = 0; i < K; i++) { 21 | int ans = 0; 22 | if (E[idx[i]] == 0) { 23 | if (!plants.empty()) { 24 | ans = plants.front(); 25 | plants.pop(); 26 | } 27 | else stores.push(L[idx[i]]); 28 | } 29 | else { 30 | if (!stores.empty()) { 31 | ans = stores.front(); 32 | stores.pop(); 33 | } 34 | else plants.push(L[idx[i]]); 35 | } 36 | cout << ans << '\n'; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /a58_m4_supply.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JomnoiZ/AlgorithmDesign/f492f02cfa6869710c51744e7f0a8f17daef1ea2/a58_m4_supply.exe -------------------------------------------------------------------------------- /a58_q1_triple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int n, m; 8 | cin >> n >> m; 9 | 10 | vector a(n); 11 | for (auto &x : a) cin >> x; 12 | 13 | while (m--) { 14 | int q; 15 | cin >> q; 16 | 17 | bool ok = false; 18 | for (int i = 0; i < n - 2; i++) { 19 | if (ok == true) break; 20 | 21 | int j = i + 1, k = n - 1; 22 | while (j < k) { 23 | if (a[i] + a[j] + a[k] == q) { 24 | ok = true; 25 | break; 26 | } 27 | 28 | if (a[i] + a[j] + a[k] < q) j++; 29 | else k--; 30 | } 31 | } 32 | 33 | if (ok == true) cout << "YES\n"; 34 | else cout << "NO\n"; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /a58_q3_p3a_hacking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, m, k; 10 | cin >> n >> m >> k; 11 | 12 | vector s(k), c(n); 13 | vector > graph(n); 14 | for (int i = 0; i < k; i++) cin >> s[i]; 15 | for (int i = 0; i < n; i++) cin >> c[i]; 16 | while (m--) { 17 | int a, b; 18 | cin >> a >> b; 19 | 20 | graph[a].push_back(b); 21 | graph[b].push_back(a); 22 | } 23 | 24 | vector dist(n, INF); 25 | vector visited(n, false); 26 | 27 | priority_queue > pq; 28 | for (int i = 0; i < k; i++) { 29 | dist[s[i]] = c[s[i]]; 30 | pq.emplace(-c[s[i]], s[i]); 31 | } 32 | 33 | while (!pq.empty()) { 34 | auto [d, u] = pq.top(); 35 | pq.pop(); 36 | 37 | if (visited[u] == true) continue; 38 | visited[u] = true; 39 | 40 | for (auto v : graph[u]) { 41 | if (visited[v] == false and dist[u] + c[v] < dist[v]) { 42 | dist[v] = dist[u] + c[v]; 43 | pq.emplace(-dist[v], v); 44 | } 45 | } 46 | } 47 | 48 | int ans = 0; 49 | for (int i = 0; i < n; i++) ans = max(ans, dist[i]); 50 | cout << ans; 51 | return 0; 52 | } -------------------------------------------------------------------------------- /a58_q3_p4_columbia2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 2e9 + 7; 5 | const int d[5][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}, {0, 0}}; 6 | 7 | int main() { 8 | cin.tie(nullptr)->sync_with_stdio(false); 9 | 10 | int R, C; 11 | cin >> R >> C; 12 | 13 | vector > a(R + 1, vector (C + 1)); 14 | for (int i = 1; i <= R; i++) { 15 | for (int j = 1; j <= C; j++) { 16 | cin >> a[i][j]; 17 | } 18 | } 19 | 20 | vector >> dist(R + 5, vector > (C + 5, vector (3, INF))); 21 | vector >> visited(R + 5, vector > (C + 5, vector (3))); 22 | 23 | priority_queue > pq; 24 | pq.emplace(0, 1, 1, 0); 25 | dist[1][1][0] = 0; 26 | while (!pq.empty()) { 27 | auto [di, ux, uy, k] = pq.top(); 28 | pq.pop(); 29 | 30 | if (visited[ux][uy][k] == true) continue; 31 | visited[ux][uy][k] = true; 32 | 33 | for (int i = 0; i < 4; i++) { 34 | int vx = ux + d[i][0], vy = uy + d[i][1]; 35 | if (vx < 1 or vx > R or vy < 1 or vy > C) continue; 36 | if (visited[vx][vy][k] == false and dist[ux][uy][k] + a[vx][vy] < dist[vx][vy][k]) { 37 | dist[vx][vy][k] = dist[ux][uy][k] + a[vx][vy]; 38 | pq.emplace(-dist[vx][vy][k], vx, vy, k); 39 | } 40 | if (k + 1 < 3) { 41 | for (int j = 0; j < 5; j++) { 42 | int vx2 = vx + d[j][0], vy2 = vy + d[j][1]; 43 | if (vx2 < 1 or vx2 > R or vy2 < 1 or vy2 > C) continue; 44 | if (visited[vx2][vy2][k + 1] == false and dist[ux][uy][k] < dist[vx2][vy2][k + 1]) { 45 | dist[vx2][vy2][k + 1] = dist[ux][uy][k]; 46 | pq.emplace(-dist[vx2][vy2][k + 1], vx2, vy2, k + 1); 47 | } 48 | } 49 | } 50 | } 51 | } 52 | 53 | for (int i = 1; i <= R; i++) { 54 | for (int j = 1; j <= C; j++) cout << min({dist[i][j][0], dist[i][j][1], dist[i][j][2]}) << ' '; 55 | cout << '\n'; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /a58_​q3_​p3_​PandG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | void testcase() { 8 | int R, C, n, T, r, c; 9 | cin >> R >> C >> n >> T >> r >> c; 10 | 11 | vector > distG(R, vector (C, INF)); 12 | priority_queue > pq; 13 | while (n--) { 14 | int ti, ri, ci; 15 | cin >> ti >> ri >> ci; 16 | 17 | if (ti < distG[ri][ci]) { 18 | distG[ri][ci] = ti; 19 | pq.emplace(-ti, ri, ci); 20 | } 21 | } 22 | 23 | vector > table(R, vector (C)); 24 | for (int i = 0; i < R; i++) { 25 | for (int j = 0; j < C; j++) { 26 | cin >> table[i][j]; 27 | } 28 | } 29 | 30 | while (!pq.empty()) { 31 | auto [di, ux, uy] = pq.top(); 32 | pq.pop(); 33 | 34 | if (distG[ux][uy] > -di) continue; 35 | for (int i = 0; i < 4; i++) { 36 | int vx = ux + d[i][0], vy = uy + d[i][1]; 37 | if (vx < 0 or vx >= R or vy < 0 or vy >= C or table[vx][vy] == '#') continue; 38 | 39 | if (distG[ux][uy] + 1 < distG[vx][vy]) { 40 | distG[vx][vy] = distG[ux][uy] + 1; 41 | pq.emplace(-distG[vx][vy], vx, vy); 42 | } 43 | } 44 | } 45 | 46 | queue > q; 47 | vector > distP(R, vector (C, INF)); 48 | distP[r][c] = 0; 49 | q.emplace(r, c); 50 | while (!q.empty()) { 51 | auto [ux, uy] = q.front(); 52 | q.pop(); 53 | 54 | for (int i = 0; i < 4; i++) { 55 | int vx = ux + d[i][0], vy = uy + d[i][1]; 56 | if (vx < 0 or vx >= R or vy < 0 or vy >= C or table[vx][vy] == '#') continue; 57 | 58 | if (distP[vx][vy] == INF and distP[ux][uy] + 1 < distG[vx][vy]) { 59 | distP[vx][vy] = distP[ux][uy] + 1; 60 | q.emplace(vx, vy); 61 | } 62 | } 63 | } 64 | 65 | bool ok = false; 66 | for (int i = 0; i < R; i++) { 67 | for (int j = 0; j < C; j++) { 68 | if (distP[i][j] <= T and T < distG[i][j]) ok = true; 69 | } 70 | } 71 | 72 | if (ok == true) cout << "YES\n"; 73 | else cout << "NO\n"; 74 | } 75 | 76 | int main() { 77 | cin.tie(nullptr)->sync_with_stdio(false); 78 | 79 | int K; 80 | cin >> K; 81 | 82 | while (K--) testcase(); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /a58_​q3_​p3a_​hacking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, m, k; 10 | cin >> n >> m >> k; 11 | 12 | vector dist(n, INF), c(n), st(k); 13 | vector visited(n); 14 | vector > graph(n); 15 | for (auto &x : st) cin >> x; 16 | for (auto &x : c) cin >> x; 17 | while (m--) { 18 | int a, b; 19 | cin >> a >> b; 20 | 21 | graph[a].push_back(b); 22 | graph[b].push_back(a); 23 | } 24 | 25 | priority_queue > pq; 26 | for (auto &x : st) { 27 | dist[x] = c[x]; 28 | pq.emplace(-c[x], x); 29 | } 30 | 31 | while (!pq.empty()) { 32 | int u = pq.top().second; 33 | pq.pop(); 34 | 35 | if (visited[u] == true) continue; 36 | visited[u] = true; 37 | 38 | for (auto &v : graph[u]) { 39 | if (visited[v] == false and dist[u] + c[v] < dist[v]) { 40 | dist[v] = dist[u] + c[v]; 41 | pq.emplace(-dist[v], v); 42 | } 43 | } 44 | } 45 | 46 | int ans = 0; 47 | for (int i = 0; i < n; i++) ans = max(ans, dist[i]); 48 | cout << ans; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /a58_​q3_​p4a_​chokepoint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | int n; 7 | vector graph[MAX_N]; 8 | int sz[MAX_N]; 9 | long long dp[MAX_N]; 10 | 11 | void dfs(int u, int p) { 12 | int sum = 0; 13 | sz[u] = 1; 14 | dp[u] = n - 1; 15 | for (auto v : graph[u]) if (v != p) { 16 | dfs(v, u); 17 | sz[u] += sz[v]; 18 | dp[u] += 1ll * sum * sz[v]; 19 | sum += sz[v]; 20 | } 21 | dp[u] += 1ll * (sz[u] - 1) * (n - sz[u]); 22 | } 23 | 24 | int main() { 25 | cin.tie(nullptr)->sync_with_stdio(false); 26 | 27 | cin >> n; 28 | 29 | for (int i = 1; i < n; i++) { 30 | int a, b; 31 | cin >> a >> b; 32 | 33 | graph[a].push_back(b); 34 | graph[b].push_back(a); 35 | } 36 | 37 | dfs(0, -1); 38 | 39 | for (int i = 0; i < n; i++) cout << dp[i] << '\n'; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /a59_midp1_lcs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | char a[MAX_N], b[MAX_N]; 7 | int dp[MAX_N][MAX_N]; 8 | stack ans; 9 | 10 | void solve(int n, int m) { 11 | if (n == 0 or m == 0) return; 12 | if (dp[n - 1][m - 1] + 1 == dp[n][m]) { 13 | ans.push(a[n - 1]); 14 | solve(n - 1, m - 1); 15 | } 16 | else if (dp[n - 1][m] == dp[n][m]) solve(n - 1, m); 17 | else solve(n, m - 1); 18 | } 19 | 20 | int main() { 21 | cin.tie(nullptr)->sync_with_stdio(false); 22 | 23 | int n, m; 24 | cin >> n >> m; 25 | cin >> a >> b; 26 | 27 | for (int i = 0; i <= n; i++) { 28 | for (int j = 0; j <= m; j++) { 29 | cin >> dp[i][j]; 30 | } 31 | } 32 | 33 | solve(n, m); 34 | 35 | while (!ans.empty()) { 36 | cout << ans.top(); 37 | ans.pop(); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /a59_midp3_similar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool solve(string a, string b) { 5 | int n = a.size(); 6 | if (n == 1) return a == b; 7 | if (a == b) return true; 8 | 9 | int m = n / 2; 10 | return (solve(a.substr(0, m), b.substr(0, m)) && solve(a.substr(m, m), b.substr(m, m))) || 11 | (solve(a.substr(0, m), b.substr(m, m)) && solve(a.substr(m, m), b.substr(0, m))); 12 | } 13 | 14 | int main() { 15 | cin.tie(nullptr)->sync_with_stdio(false); 16 | 17 | string a, b; 18 | cin >> a >> b; 19 | 20 | if (solve(a, b) == true) cout << "YES"; 21 | else cout << "NO"; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /a59_midp4_mcsw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 2e5 + 5; 5 | const int INF = 1e9 + 7; 6 | 7 | int w; 8 | int a[MAX_N]; 9 | 10 | int solve(int l, int r) { 11 | if (l == r) return a[l]; 12 | 13 | int mid = (l + r) / 2; 14 | int res = max(solve(l, mid), solve(mid + 1, r)); 15 | 16 | vector L; 17 | L.push_back(-INF); 18 | int sum = 0, maxRight = 0; 19 | for (int i = mid; i >= max(l, mid - w + 2); i--) { 20 | sum += a[i]; 21 | L.push_back(max(L.back(), sum)); 22 | } 23 | 24 | sum = 0; 25 | for (int i = mid + 1; i <= min(r, mid + w); i++) { 26 | sum += a[i]; 27 | maxRight = max(maxRight, sum); 28 | res = max(res, L[min(w - i + mid, mid - l)] + maxRight); 29 | } 30 | return res; 31 | } 32 | 33 | int main() { 34 | cin.tie(nullptr)->sync_with_stdio(false); 35 | 36 | int n; 37 | cin >> n >> w; 38 | 39 | for (int i = 1; i <= n; i++) cin >> a[i]; 40 | 41 | cout << solve(1, n); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /a59_q1_ones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n, int l) { 5 | if (n < 11) return min(13 - n, n); 6 | 7 | int s = 1; 8 | for (int i = 0; i < l - 1; i++) s = s * 10 + 1; 9 | return min(solve(n - n / s * s, l - 1) + n / s * l, solve((n / s + 1) * s - n, l - 1) + (n / s + 1) * l); 10 | } 11 | 12 | int main() { 13 | cin.tie(nullptr)->sync_with_stdio(false); 14 | 15 | int n; 16 | cin >> n; 17 | 18 | int s = 1, l = 1; 19 | while (s < n) s = s * 10 + 1, l++; 20 | cout << min(solve(n, l - 1), solve(s - n, l) + l); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /a59_q3_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | vector graph[MAX_N]; 7 | int degree[MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int n; 13 | cin >> n; 14 | 15 | for (int i = 0; i < n; i++) { 16 | int u, v; 17 | cin >> u >> v; 18 | 19 | graph[u].push_back(v); 20 | graph[v].push_back(u); 21 | degree[u]++; 22 | degree[v]++; 23 | } 24 | 25 | queue q; 26 | for (int i = 0; i < n; i++) { 27 | if (degree[i] == 1) q.push(i); 28 | } 29 | 30 | while (!q.empty()) { 31 | int u = q.front(); 32 | q.pop(); 33 | 34 | for (auto v : graph[u]) { 35 | degree[u]--; 36 | if (--degree[v] == 1) q.push(v); 37 | } 38 | } 39 | 40 | int ans = 0; 41 | for (int i = 0; i < n; i++) { 42 | if (degree[i] > 1) ans++; 43 | } 44 | cout << ans; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /a59_​q1_​quicksum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, M, K; 8 | cin >> N >> M >> K; 9 | 10 | vector > a(N + 1, vector (M + 1)); 11 | for (int i = 1; i <= N; i++) { 12 | for (int j = 1; j <= M; j++) { 13 | cin >> a[i][j]; 14 | 15 | a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; 16 | } 17 | } 18 | 19 | while (K--) { 20 | int r1, c1, r2, c2; 21 | cin >> r1 >> c1 >> r2 >> c2; 22 | 23 | r1++, c1++, r2++, c2++; 24 | cout << a[r2][c2] - a[r1 - 1][c2] - a[r2][c1 - 1] + a[r1 - 1][c1 - 1] << '\n'; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /a59_​q3_​cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | int ans, start; 7 | vector graph[MAX_N]; 8 | bool visited[MAX_N]; 9 | 10 | bool dfs(int u, int p) { 11 | if (visited[u] == true) { 12 | start = u; 13 | return true; 14 | } 15 | visited[u] = true; 16 | for (auto v : graph[u]) { 17 | if (v == p) continue; 18 | 19 | if (dfs(v, u) == true) { 20 | ans++; 21 | if (start == u) return false; 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | 28 | int main() { 29 | cin.tie(nullptr)->sync_with_stdio(false); 30 | 31 | int n; 32 | cin >> n; 33 | 34 | for (int i = 1; i <= n; i++) { 35 | int x, y; 36 | cin >> x >> y; 37 | 38 | graph[x].push_back(y); 39 | graph[y].push_back(x); 40 | } 41 | 42 | dfs(1, -1); 43 | cout << ans; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /a59_​q3_​hole.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 5 | const int INF = 1e9 + 7; 6 | 7 | int main() { 8 | cin.tie(nullptr)->sync_with_stdio(false); 9 | 10 | int N, a, b; 11 | cin >> N >> a >> b; 12 | 13 | vector > hole(1001, vector (1001)); 14 | while (N--) { 15 | int x, y; 16 | cin >> x >> y; 17 | 18 | hole[x][y] = true; 19 | } 20 | 21 | priority_queue > pq; 22 | vector > dist(1001, vector (1001, INF)); 23 | vector > visited(1001, vector (1001)); 24 | 25 | int ans = INF; 26 | pq.emplace(0, a, b); 27 | dist[a][b] = 0; 28 | while (!pq.empty()) { 29 | auto [di, ux, uy] = pq.top(); 30 | pq.pop(); 31 | 32 | if (visited[ux][uy] == true) continue; 33 | visited[ux][uy] = true; 34 | 35 | for (int i = 0; i < 4; i++) { 36 | int vx = ux + d[i][0], vy = uy + d[i][1]; 37 | if (vx < 1 or vx > 1000 or vy < 1 or vy > 1000) { 38 | ans = min(ans, dist[ux][uy]); 39 | continue; 40 | } 41 | 42 | if (visited[vx][vy] == false and dist[ux][uy] + hole[vx][vy] < dist[vx][vy]) { 43 | dist[vx][vy] = dist[ux][uy] + hole[vx][vy]; 44 | pq.emplace(-dist[vx][vy], vx, vy); 45 | } 46 | } 47 | } 48 | cout << ans; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /a60_q1_matmod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int K; 5 | 6 | vector > matrix_multiply(vector > a, vector > b) { 7 | vector > c(2, vector (2, 0)); 8 | for (int i = 0; i < 2; i++) { 9 | for (int j = 0; j < 2; j++) { 10 | for (int k = 0; k < 2; k++) { 11 | c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % K; 12 | } 13 | } 14 | } 15 | return c; 16 | } 17 | 18 | vector > matrix_expo(vector > a, int N) { 19 | if (N == 0) return {{1, 0}, {0, 1}}; 20 | 21 | vector > c = matrix_expo(a, N / 2); 22 | c = matrix_multiply(c, c); 23 | if (N % 2 == 1) c = matrix_multiply(a, c); 24 | return c; 25 | } 26 | 27 | int main() { 28 | cin.tie(nullptr)->sync_with_stdio(false); 29 | 30 | int N; 31 | vector > a(2, vector (2)); 32 | 33 | cin >> N >> K; 34 | cin >> a[0][0] >> a[0][1] >> a[1][0] >> a[1][1]; 35 | 36 | vector > result = matrix_expo(a, N); 37 | cout << result[0][0] << ' ' << result[0][1] << ' ' << result[1][0] << ' ' << result[1][1]; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /a60_q1_number1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n, int l, int r, int ql, int qr) { 5 | if (r < ql or qr < l) return 0; 6 | if (n == 1) return 1; 7 | 8 | int mid = (l + r) / 2; 9 | return solve(n / 2, l, mid - 1, ql, qr) + solve(n / 2, mid + 1, r, ql, qr) + (ql <= mid and mid <= qr and n % 2 == 1); 10 | } 11 | 12 | int main() { 13 | cin.tie(nullptr)->sync_with_stdio(false); 14 | 15 | int n, l, r; 16 | cin >> n >> l >> r; 17 | 18 | int s = 0; 19 | while (s < n) s = s * 2 + 1; 20 | 21 | cout << solve(n, 1, s, l, r); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /a60a_midp1_knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | 6 | int v[MAX_N], w[MAX_N]; 7 | int V[MAX_N][MAX_N]; 8 | vector ans; 9 | 10 | void solve(int n, int m) { 11 | if (n == 0) return; 12 | if (V[n - 1][m] == V[n][m]) solve(n - 1, m); 13 | else { 14 | ans.push_back(n); 15 | solve(n - 1, m - w[n]); 16 | } 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int n, m; 23 | cin >> n >> m; 24 | 25 | for (int i = 1; i <= n; i++) cin >> v[i]; 26 | for (int i = 1; i <= n; i++) cin >> w[i]; 27 | for (int i = 0; i <= n; i++) { 28 | for (int j = 0; j <= m; j++) { 29 | cin >> V[i][j]; 30 | } 31 | } 32 | 33 | solve(n, m); 34 | 35 | cout << ans.size() << '\n'; 36 | for (auto v : ans) cout << v << ' '; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /a60a_midp2_avl_distance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int k; 5 | vector > dp; 6 | vector > edge; 7 | 8 | int solve(int u) { 9 | dp[u][0] = 1; 10 | int res = 0; 11 | for (auto v : edge[u]) { 12 | res += solve(v); 13 | for (int i = 0; i < k; i++) res += dp[u][i] * dp[v][k - i - 1]; 14 | for (int i = 1; i <= k; i++) dp[u][i] += dp[v][i - 1]; 15 | } 16 | return res; 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int n; 23 | cin >> n >> k; 24 | 25 | edge.resize(n + 1); 26 | dp = vector > (n + 1, vector (k + 1)); 27 | for (int i = 2; i <= n; i++) { 28 | int a, b; 29 | cin >> a >> b; 30 | 31 | edge[a].push_back(b); 32 | } 33 | cout << solve(1); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /a60a_midp3_bigger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector dp(N + 1); 11 | for (int i = 1; i <= N; i++) { 12 | int c; 13 | cin >> c; 14 | 15 | dp[i] = max(dp[i - 1], dp[max(0, i - 3)] + c); 16 | } 17 | cout << dp[N]; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /a60a_midp4_walk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, m; 10 | cin >> n >> m; 11 | 12 | vector > dp(n + 1, vector (m + 1, -INF)); 13 | for (int i = 1; i <= n; i++) { 14 | for (int j = 1; j <= m; j++) { 15 | int a; 16 | cin >> a; 17 | 18 | if (i == 1 and j == 1) dp[i][j] = a; 19 | else { 20 | dp[i][j] = max({dp[i - 1][j], dp[i][j - 1]}) + a; 21 | dp[i][j] = max({dp[i][j], dp[i - 1][j - 1] + 2 * a}); 22 | } 23 | } 24 | } 25 | cout << dp[n][m]; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /a60a_q3_component.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 10005; 5 | 6 | vector graph[MAX_N]; 7 | bool visited[MAX_N]; 8 | 9 | void dfs(int u) { 10 | for (auto v : graph[u]) { 11 | if (visited[v] == false) { 12 | visited[v] = true; 13 | dfs(v); 14 | } 15 | } 16 | } 17 | 18 | int main() { 19 | cin.tie(nullptr)->sync_with_stdio(false); 20 | 21 | int n, m; 22 | cin >> n >> m; 23 | 24 | while (m--) { 25 | int a, b; 26 | cin >> a >> b; 27 | 28 | graph[a].push_back(b); 29 | graph[b].push_back(a); 30 | } 31 | 32 | int ans = 0; 33 | for (int i = 1; i <= n; i++) { 34 | if (visited[i] == false) { 35 | visited[i] = true; 36 | ans++; 37 | dfs(i); 38 | } 39 | } 40 | cout << ans; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /a60a_q3_xor_road.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 2005; 5 | 6 | int a[MAX_N], parent[MAX_N]; 7 | 8 | int find_parent(int u) { 9 | if (u == parent[u]) return u; 10 | return parent[u] = find_parent(parent[u]); 11 | } 12 | 13 | int main() { 14 | cin.tie(nullptr)->sync_with_stdio(false); 15 | 16 | int n; 17 | cin >> n; 18 | 19 | for (int i = 1; i <= n; i++) cin >> a[i]; 20 | 21 | vector > edges; 22 | for (int i = 1; i < n; i++) { 23 | for (int j = i + 1; j <= n; j++) { 24 | edges.emplace_back(a[i] ^ a[j], i, j); 25 | } 26 | } 27 | 28 | sort(edges.rbegin(), edges.rend()); 29 | 30 | long long ans = 0; 31 | iota(parent + 1, parent + n + 1, 1); 32 | for (auto [w, a, b] : edges) { 33 | int u = find_parent(a), v = find_parent(b); 34 | if (u != v) { 35 | ans += w; 36 | parent[v] = u; 37 | } 38 | } 39 | cout << ans; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /a60a_​q3_​component.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 10005; 5 | 6 | vector graph[MAX_N]; 7 | bool visited[MAX_N]; 8 | 9 | void dfs(int u) { 10 | if (visited[u] == true) return; 11 | visited[u] = true; 12 | 13 | for (auto v : graph[u]) dfs(v); 14 | } 15 | 16 | int main() { 17 | cin.tie(nullptr)->sync_with_stdio(false); 18 | 19 | int n, m; 20 | cin >> n >> m; 21 | 22 | while (m--) { 23 | int a, b; 24 | cin >> a >> b; 25 | 26 | graph[a].push_back(b); 27 | graph[b].push_back(a); 28 | } 29 | 30 | int ans = 0; 31 | for (int i = 1; i <= n; i++) { 32 | if (visited[i] == false) { 33 | ans++; 34 | dfs(i); 35 | } 36 | } 37 | cout << ans; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /a60a_​q3_​xor_​road.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 2005; 5 | 6 | int x[MAX_N], parent[MAX_N]; 7 | 8 | int find_parent(int u) { 9 | if (u == parent[u]) return u; 10 | return parent[u] = find_parent(parent[u]); 11 | } 12 | 13 | int main() { 14 | cin.tie(nullptr)->sync_with_stdio(false); 15 | 16 | int n; 17 | cin >> n; 18 | 19 | for (int i = 0; i < n; i++) cin >> x[i]; 20 | 21 | vector > edges; 22 | for (int i = 0; i < n - 1; i++) { 23 | for (int j = i + 1; j < n; j++) { 24 | edges.emplace_back(x[i] ^ x[j], i, j); 25 | } 26 | } 27 | 28 | iota(parent, parent + n, 0); 29 | sort(edges.rbegin(), edges.rend()); 30 | 31 | long long ans = 0; 32 | for (auto [w, a, b] : edges) { 33 | int u = find_parent(a), v = find_parent(b); 34 | if (u != v) { 35 | ans += w; 36 | parent[v] = u; 37 | } 38 | } 39 | cout << ans; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /a60b_mid_p1_buffet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int f, w, n; 8 | cin >> f >> w >> n; 9 | 10 | vector food(f); 11 | for (auto &x : food) cin >> x; 12 | 13 | sort(food.begin(), food.end()); 14 | 15 | int cur = 0, ans = 0; 16 | for (auto f : food) { 17 | if (cur < f) { 18 | cur = f + 2 * w; 19 | ans++; 20 | } 21 | } 22 | cout << ans; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /a60b_mid_p4_merge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | vector vec; 7 | 8 | int solve(int l, int r, int k) { 9 | if (l + 1 == r or k == 1) { 10 | for (int i = l; i < r; i++) vec[i] = i - l + 1; 11 | return 1; 12 | } 13 | 14 | int mid = (l + r) / 2; 15 | int val1 = solve(l, mid, k - 2); 16 | int val2 = solve(mid, r, k - 1 - val1); 17 | 18 | for (int i = l; i < mid; i++) vec[i] += r - mid; 19 | return val1 + val2 + 1; 20 | } 21 | 22 | int main() { 23 | cin.tie(nullptr)->sync_with_stdio(false); 24 | 25 | int n, k; 26 | cin >> n >> k; 27 | 28 | vec.resize(n); 29 | reverse(vec.begin(), vec.end()); 30 | 31 | int x = solve(0, n, k); 32 | if (x != k) cout << "-1\n"; 33 | else for (auto v : vec) cout << v << ' '; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /a60b_q1_diagsum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | const int INF = 1e9 + 7; 6 | 7 | int a[MAX_N][MAX_N]; 8 | vector vec[2 * MAX_N]; 9 | 10 | int main() { 11 | cin.tie(nullptr)->sync_with_stdio(false); 12 | 13 | int n; 14 | cin >> n; 15 | 16 | for (int i = 1; i <= n; i++) { 17 | for (int j = 1; j <= n; j++) { 18 | cin >> a[i][j]; 19 | 20 | vec[i - j + 1000].push_back(a[i][j]); 21 | } 22 | } 23 | 24 | int ans = -INF; 25 | for (int i = 1; i < 2000; i++) { 26 | int sum = 0; 27 | for (auto v : vec[i]) { 28 | sum = max(sum + v, v); 29 | ans = max(ans, sum); 30 | } 31 | } 32 | cout << ans << '\n'; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /a60b_q3_perimeter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | vector graph[MAX_N]; 7 | bool visited[MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int n, m, k; 13 | cin >> n >> m >> k; 14 | 15 | while (m--) { 16 | int a, b; 17 | cin >> a >> b; 18 | 19 | graph[a].push_back(b); 20 | graph[b].push_back(a); 21 | } 22 | 23 | int ans = 0; 24 | queue > q; 25 | q.emplace(0, 0); 26 | visited[0] = true; 27 | while (!q.empty()) { 28 | auto [u, c] = q.front(); 29 | q.pop(); 30 | 31 | if (c == k) { 32 | ans++; 33 | continue; 34 | } 35 | for (auto v : graph[u]) { 36 | if (visited[v] == false) { 37 | visited[v] = true; 38 | q.emplace(v, c + 1); 39 | } 40 | } 41 | } 42 | cout << ans; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /a60b_​mid_​p2_​rope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, a, b, c; 10 | cin >> n >> a >> b >> c; 11 | 12 | vector dp(n + 1, -INF); 13 | dp[0] = 0; 14 | for (int i = 1; i <= n; i++) { 15 | if (i - a >= 0) dp[i] = max(dp[i], dp[i - a] + 1); 16 | if (i - b >= 0) dp[i] = max(dp[i], dp[i - b] + 1); 17 | if (i - c >= 0) dp[i] = max(dp[i], dp[i - c] + 1); 18 | } 19 | cout << dp[n]; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /a60b_​mid_​p3_​divide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | const int MOD = 1997; 6 | 7 | int dp[MAX_N][MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int n, k; 13 | cin >> n >> k; 14 | 15 | dp[0][0] = 1; 16 | for (int i = 1; i <= n; i++) { 17 | for (int j = 1; j <= k; j++) { 18 | dp[i][j] = (dp[i - 1][j] * j % MOD + dp[i - 1][j - 1]) % MOD; 19 | } 20 | } 21 | cout << dp[n][k]; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /a60b_​q2_​pareto.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | int X[MAX_N], Y[MAX_N]; 7 | 8 | vector pareto(int l, int r) { 9 | if (l == r) return {l}; 10 | 11 | int m = (l + r) / 2; 12 | vector L = pareto(l, m), R = pareto(m + 1, r); 13 | 14 | int i = 0, j = 0; 15 | vector result; 16 | while (i < L.size() or j < R.size()) { 17 | if (i == L.size()) result.push_back(R[j++]); 18 | else if (j == R.size()) result.push_back(L[i++]); 19 | else { 20 | if (!(X[L[i]] <= X[R[j]] and Y[L[i]] <= Y[R[j]])) result.push_back(L[i++]); 21 | else { 22 | while (i < L.size() and X[L[i]] <= X[R[j]] and Y[L[i]] <= Y[R[j]]) i++; 23 | result.push_back(R[j++]); 24 | } 25 | } 26 | } 27 | return result; 28 | } 29 | 30 | int main() { 31 | cin.tie(nullptr)->sync_with_stdio(false); 32 | 33 | int N; 34 | cin >> N; 35 | 36 | vector > point; 37 | for (int i = 0; i < N; i++) { 38 | cin >> X[i] >> Y[i]; 39 | 40 | point.emplace_back(X[i], Y[i]); 41 | } 42 | 43 | sort(point.begin(), point.end()); 44 | 45 | for (int i = 0; i < N; i++) X[i] = point[i].first, Y[i] = point[i].second; 46 | 47 | cout << pareto(0, N - 1).size(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /a60b_​q3_​ikea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int n, m; 8 | cin >> n >> m; 9 | 10 | vector > edges; 11 | while (m--) { 12 | int a, b; 13 | cin >> a >> b; 14 | 15 | edges.emplace_back(a, b); 16 | } 17 | 18 | for (int q = 0; q < 5; q++) { 19 | vector pos(n + 1); 20 | for (int i = 0; i < n; i++) { 21 | int s; 22 | cin >> s; 23 | 24 | pos[s] = i; 25 | } 26 | 27 | bool ok = true; 28 | for (auto [a, b] : edges) { 29 | if (pos[a] > pos[b]) ok = false; 30 | } 31 | 32 | if (ok == true) cout << "SUCCESS\n"; 33 | else cout << "FAIL\n"; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /a61_q1_thanos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long A, B; 5 | vector a; 6 | 7 | long long solve(long long l, long long r) { 8 | int cnt = upper_bound(a.begin(), a.end(), r) - lower_bound(a.begin(), a.end(), l); 9 | if (cnt == 0) return A; 10 | 11 | long long res = cnt * B * (r - l + 1); 12 | 13 | if (l < r) { 14 | long long mid = (l + r) / 2; 15 | res = min(res, solve(l, mid) + solve(mid + 1, r)); 16 | } 17 | return res; 18 | } 19 | 20 | int main() { 21 | cin.tie(nullptr)->sync_with_stdio(false); 22 | 23 | int p, k; 24 | cin >> p >> k >> A >> B; 25 | 26 | a.resize(k); 27 | for (auto &x : a) cin >> x; 28 | 29 | sort(a.begin(), a.end()); 30 | 31 | cout << solve(1, 1< 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | const int INF = 1e9 + 7; 6 | 7 | vector > graph[MAX_N]; 8 | int dist[MAX_N]; 9 | bool visited[MAX_N]; 10 | 11 | int main() { 12 | cin.tie(nullptr)->sync_with_stdio(false); 13 | 14 | int N; 15 | cin >> N; 16 | 17 | for (int i = 1; i <= N; i++) { 18 | for (int j = 1; j <= N; j++) { 19 | int w; 20 | cin >> w; 21 | 22 | if (w != -1) graph[i].emplace_back(j, w); 23 | } 24 | } 25 | 26 | for (int i = 1; i <= N; i++) dist[i] = INF; 27 | 28 | priority_queue > pq; 29 | pq.emplace(0, 1); 30 | dist[1] = 0; 31 | while (!pq.empty()) { 32 | auto [d, u] = pq.top(); 33 | pq.pop(); 34 | 35 | if (visited[u] == true) continue; 36 | visited[u] = true; 37 | 38 | for (auto [v, w] : graph[u]) { 39 | if (visited[v] == false and dist[u] + w < dist[v]) { 40 | dist[v] = dist[u] + w; 41 | pq.emplace(-dist[v], v); 42 | } 43 | } 44 | } 45 | 46 | int ans = 0; 47 | for (int i = 1; i <= N; i++) { 48 | if (dist[i] == INF) { 49 | ans = -1; 50 | break; 51 | } 52 | else ans = max(ans, dist[i]); 53 | } 54 | cout << ans; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /a61_​q2_​farthest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N; 10 | cin >> N; 11 | 12 | vector >> graph(N + 1); 13 | for (int i = 1; i <= N; i++) { 14 | for (int j = 1; j <= N; j++) { 15 | int w; 16 | cin >> w; 17 | 18 | if (w > 0) graph[i].emplace_back(j, w); 19 | } 20 | } 21 | 22 | vector dist(N + 1, INF); 23 | 24 | priority_queue > pq; 25 | pq.emplace(0, 1); 26 | dist[1] = 0; 27 | while (!pq.empty()) { 28 | int u = pq.top().second; 29 | pq.pop(); 30 | 31 | for (auto [v, w] : graph[u]) { 32 | if (dist[u] + w < dist[v]) { 33 | dist[v] = dist[u] + w; 34 | pq.emplace(-dist[v], v); 35 | } 36 | } 37 | } 38 | 39 | int ans = 0; 40 | for (int i = 1; i <= N; i++) ans = max(ans, dist[i]); 41 | 42 | if (ans == INF) ans = -1; 43 | cout << ans; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /a62_q1a_recur.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 300; 5 | 6 | int table[MAX_N][MAX_N]; 7 | 8 | void solve(int x1, int y1, int x2, int y2, int b) { 9 | if (x1 == x2 and y1 == y2) { 10 | table[x1][y1] = b; 11 | return; 12 | } 13 | 14 | int mx = (x1 + x2) / 2; 15 | int my = (y1 + y2) / 2; 16 | solve(x1, y1, mx, my, b); 17 | solve(x1, my + 1, mx, y2, b - 1); 18 | solve(mx + 1, y1, x2, my, b + 1); 19 | solve(mx + 1, my + 1, x2, y2, b); 20 | } 21 | 22 | int main() { 23 | cin.tie(nullptr)->sync_with_stdio(false); 24 | 25 | int a, b; 26 | cin >> a >> b; 27 | 28 | int n = (1< 2 | using namespace std; 3 | 4 | const int MOD = 1e6 + 3; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, k; 10 | cin >> n >> k; 11 | 12 | vector s(k); 13 | for (auto &x : s) cin >> x; 14 | 15 | vector dp(n + 1, 0); 16 | dp[0] = 1; 17 | for (int i = 1; i <= n; i++) { 18 | for (int j = 0; j < k; j++) { 19 | if (i - s[j] >= 0) dp[i] = (dp[i] + dp[i - s[j]]) % MOD; 20 | } 21 | } 22 | cout << dp[n]; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /a62_q1b_virus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 300; 5 | 6 | int pat[MAX_N]; 7 | 8 | bool solve(int l, int r) { 9 | if (l + 1 == r) return pat[l] == 0 and pat[r] == 1; 10 | 11 | int m = (l + r) / 2; 12 | if (solve(m + 1, r) == false) return false; 13 | if (solve(l, m) == true) return true; 14 | 15 | reverse(pat + l, pat + m + 1); 16 | bool res = solve(l, m); 17 | reverse(pat + l, pat + m + 1); 18 | return res; 19 | } 20 | 21 | int main() { 22 | cin.tie(nullptr)->sync_with_stdio(false); 23 | 24 | int n, k; 25 | cin >> n >> k; 26 | 27 | while (n--) { 28 | for (int i = 0; i < (1<> pat[i]; 29 | 30 | if (solve(0, (1< 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | vector graph[MAX_N]; 7 | bool visited[MAX_N]; 8 | int degree[MAX_N]; 9 | int cnt[3]; 10 | 11 | void dfs(int u) { 12 | if (degree[u] > 2) cnt[2]++; 13 | else if (degree[u] < 2) cnt[degree[u]]++; 14 | for (auto v : graph[u]) { 15 | if (visited[v] == false) { 16 | visited[v] = true; 17 | dfs(v); 18 | } 19 | } 20 | } 21 | 22 | int main() { 23 | cin.tie(nullptr)->sync_with_stdio(false); 24 | 25 | int n, m; 26 | cin >> n >> m; 27 | 28 | while (m--) { 29 | int a, b; 30 | cin >> a >> b; 31 | 32 | graph[a].push_back(b); 33 | graph[b].push_back(a); 34 | degree[a]++, degree[b]++; 35 | } 36 | 37 | int ans = 0; 38 | for (int i = 0; i < n; i++) { 39 | if (visited[i] == false) { 40 | cnt[0] = cnt[1] = cnt[2] = 0; 41 | visited[i] = true; 42 | dfs(i); 43 | if ((cnt[0] == 1 or cnt[1] == 2) and cnt[2] == 0) ans++; 44 | } 45 | } 46 | cout << ans; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /a62_q2a_pandemic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | int a[MAX_N][MAX_N]; 8 | int dist[MAX_N][MAX_N]; 9 | 10 | int main() { 11 | cin.tie(nullptr)->sync_with_stdio(false); 12 | 13 | int r, c, t; 14 | cin >> r >> c >> t; 15 | 16 | for (int i = 1; i <= r; i++) for (int j = 1; j <= c; j++) cin >> a[i][j]; 17 | 18 | queue > q; 19 | for (int i = 1; i <= r; i++) { 20 | for (int j = 1; j <= c; j++) { 21 | dist[i][j] = -1; 22 | if (a[i][j] == 1) { 23 | q.emplace(i, j); 24 | dist[i][j] = 0; 25 | } 26 | } 27 | } 28 | while (!q.empty()) { 29 | auto [ux, uy] = q.front(); 30 | q.pop(); 31 | 32 | for (int i = 0; i < 4; i++) { 33 | int vx = ux + d[i][0], vy = uy + d[i][1]; 34 | if (vx < 1 or vx > r or vy < 1 or vy > c) continue; 35 | 36 | if (a[vx][vy] == 0 and dist[vx][vy] == -1 and dist[ux][uy] + 1 <= t) { 37 | a[vx][vy] = 1; 38 | dist[vx][vy] = dist[ux][uy] + 1; 39 | q.emplace(vx, vy); 40 | } 41 | } 42 | } 43 | 44 | int ans = 0; 45 | for (int i = 1; i <= r; i++) for (int j = 1; j <= c; j++) if (a[i][j] == 1) ans++; 46 | cout << ans << '\n'; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /a62_​q1a_​jumping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n; 10 | cin >> n; 11 | 12 | vector dp(n + 1, -INF); 13 | cin >> dp[1]; 14 | for (int i = 2; i <= n; i++) { 15 | int a; 16 | cin >> a; 17 | 18 | if (i - 1 >= 1) dp[i] = max(dp[i], dp[i - 1] + a); 19 | if (i - 2 >= 1) dp[i] = max(dp[i], dp[i - 2] + a); 20 | if (i - 3 >= 1) dp[i] = max(dp[i], dp[i - 3] + a); 21 | } 22 | cout << dp[n]; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /a62_​q1b_​chocolate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 1000003; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, k; 10 | cin >> n >> k; 11 | 12 | vector c(k); 13 | for (auto &x : c) cin >> x; 14 | 15 | vector dp(n + 1); 16 | dp[0] = 1; 17 | for (int i = 1; i <= n; i++) { 18 | for (int j = 0; j < k; j++) { 19 | if (i - c[j] >= 0) dp[i] = (dp[i] + dp[i - c[j]]) % MOD; 20 | } 21 | } 22 | cout << dp[n]; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /a62_​q2a_​line_​graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | int cnt; 7 | vector graph[MAX_N]; 8 | bool visited[MAX_N]; 9 | 10 | void dfs(int u) { 11 | if (visited[u] == true) return; 12 | visited[u] = true; 13 | 14 | if (graph[u].size() == 1) cnt++; 15 | for (auto v : graph[u]) dfs(v); 16 | } 17 | 18 | int main() { 19 | cin.tie(nullptr)->sync_with_stdio(false); 20 | 21 | int n, m; 22 | cin >> n >> m; 23 | 24 | while (m--) { 25 | int a, b; 26 | cin >> a >> b; 27 | 28 | graph[a].push_back(b); 29 | graph[b].push_back(a); 30 | } 31 | 32 | int ans = 0; 33 | for (int i = 0; i < n; i++) { 34 | if (visited[i] == false) { 35 | if (graph[i].size() == 0) ans++; 36 | else { 37 | cnt = 0; 38 | dfs(i); 39 | if (cnt == 2) ans++; 40 | } 41 | } 42 | } 43 | cout << ans; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /a63_q1a_101.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 1e8 + 7; 5 | 6 | vector > multiply(vector > a, vector > b) { 7 | vector > c(4, vector (4)); 8 | for (int i = 0; i < 4; i++) { 9 | for (int j = 0; j < 4; j++) { 10 | for (int k = 0; k < 4; k++) { 11 | c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % MOD; 12 | } 13 | } 14 | } 15 | return c; 16 | } 17 | 18 | vector > power(vector > &a, long long n) { 19 | if (n == 1) return a; 20 | 21 | vector > c = power(a, n / 2); 22 | c = multiply(c, c); 23 | if (n % 2 == 1) c = multiply(c, a); 24 | return c; 25 | } 26 | 27 | int main() { 28 | cin.tie(nullptr)->sync_with_stdio(false); 29 | 30 | long long n; 31 | cin >> n; 32 | 33 | if (n == 1) cout << 1; 34 | else if (n == 2) cout << 4; 35 | else { 36 | vector > dp(4, vector (4)); 37 | dp[0][0] = 1; 38 | dp[2][0] = 1; 39 | dp[0][1] = 1; 40 | dp[1][2] = 1; 41 | dp[3][2] = 1; 42 | dp[1][3] = 1; 43 | dp[3][3] = 1; 44 | vector > res = power(dp, n - 2); 45 | 46 | int ans = 0; 47 | for (int i = 0; i < 4; i++) { 48 | for (int j = 0; j < 4; j++) { 49 | ans = (ans + res[i][j]) % MOD; 50 | } 51 | } 52 | cout << ans; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /a63_q1a_buffet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int n, K, m; 8 | cin >> n >> K >> m; 9 | 10 | vector d(n + 1), qs(n + 1); 11 | for (int i = 1; i <= n; i++) { 12 | cin >> d[i]; 13 | 14 | qs[i] = qs[i - 1] + d[i] - m; 15 | } 16 | 17 | while (K--) { 18 | int p, w; 19 | cin >> p >> w; 20 | 21 | int l = p, r = n, ans = -1; 22 | while (l <= r) { 23 | int mid = (l + r) / 2; 24 | 25 | if (qs[mid] - qs[p - 1] >= w) r = mid - 1, ans = mid; 26 | else l = mid + 1; 27 | } 28 | cout << ans << '\n'; 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /a63_q1b_2side.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, w, k; 10 | cin >> n >> w >> k; 11 | 12 | vector l(n + 1), r(n + 1); 13 | for (int i = 1; i <= n; i++) cin >> l[i]; 14 | for (int i = 1; i <= n; i++) cin >> r[i]; 15 | 16 | if (k == n) { 17 | vector > dp(n + 1, vector (2)); 18 | for (int i = 1; i <= n; i++) { 19 | dp[i][0] = max(dp[i - 1][0], dp[max(0, i - w - 1)][1] + l[i]); 20 | dp[i][1] = max(dp[i - 1][1], dp[max(0, i - w - 1)][0] + r[i]); 21 | } 22 | cout << max(dp[n][0], dp[n][1]); 23 | } 24 | else { 25 | vector >> dp(n + 1, vector > (2, vector (2))); 26 | for (int j = 1; j <= k; j++) { 27 | int now = j % 2, pre = now ^ 1; 28 | for (int i = 1; i <= n; i++) { 29 | dp[i][now][0] = max(dp[i - 1][now][0], dp[max(0, i - w - 1)][pre][1] + l[i]); 30 | dp[i][now][1] = max(dp[i - 1][now][1], dp[max(0, i - w - 1)][pre][0] + r[i]); 31 | } 32 | } 33 | cout << max(dp[n][k % 2][0], dp[n][k % 2][1]); 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /a63_q1b_virus2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector a; 5 | 6 | bool solve(int l, int r) { 7 | if (l + 1 == r) return true; 8 | 9 | int mid = (l + r) / 2; 10 | bool ok = solve(l, mid) & solve(mid + 1, r); 11 | 12 | int cnt1 = 0, cnt2 = 0; 13 | for (int i = l; i <= mid; i++) cnt1 += a[i]; 14 | for (int i = mid + 1; i <= r; i++) cnt2 += a[i]; 15 | if (abs(cnt1 - cnt2) > 1) ok = false; 16 | return ok; 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int n, k; 23 | cin >> n >> k; 24 | 25 | a.resize(1<> a[i]; 28 | 29 | if (solve(0, (1< 2 | using namespace std; 3 | 4 | const int MAX_N = 5e5 + 5; 5 | 6 | int a[MAX_N]; 7 | 8 | int main() { 9 | cin.tie(nullptr)->sync_with_stdio(false); 10 | 11 | int n, m, k; 12 | cin >> n >> m >> k; 13 | 14 | for (int i = 1; i <= n; i++) { 15 | cin >> a[i]; 16 | 17 | a[i] += a[i - 1]; 18 | } 19 | 20 | while (m--) { 21 | int L; 22 | long long b; 23 | cin >> L >> b; 24 | 25 | L++; 26 | int l = L, r = n, ans = 0; 27 | while (l <= r) { 28 | int mid = (l + r) / 2; 29 | 30 | if (a[mid] - a[L - 1] + (mid - L + 1) * k <= b) l = mid + 1, ans = a[mid] - a[L - 1]; 31 | else r = mid - 1; 32 | } 33 | cout << ans << '\n'; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /a64_q1_permu_before.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 25; 5 | 6 | int n; 7 | vector g[MAX_N]; 8 | bool visited[MAX_N]; 9 | int vec[MAX_N]; 10 | 11 | void solve(int i) { 12 | if (i == n) { 13 | for (int j = 0; j < n; j++) cout << vec[j] << ' '; 14 | cout << '\n'; 15 | return; 16 | } 17 | 18 | for (int j = 0; j < n; j++) { 19 | if (visited[j] == false) { 20 | bool ok = true; 21 | for (auto v : g[j]) ok &= visited[v]; 22 | 23 | visited[j] = true; 24 | vec[i] = j; 25 | if (ok == true) solve(i + 1); 26 | visited[j] = false; 27 | } 28 | } 29 | } 30 | 31 | int main() { 32 | cin.tie(nullptr)->sync_with_stdio(false); 33 | 34 | int m; 35 | cin >> n >> m; 36 | 37 | while (m--) { 38 | int a, b; 39 | cin >> a >> b; 40 | 41 | g[b].push_back(a); 42 | } 43 | 44 | for (int i = 0; i < n; i++) { 45 | bool ok = true; 46 | for (auto v : g[i]) ok &= visited[v]; 47 | 48 | visited[i] = true; 49 | vec[0] = i; 50 | if (ok == true) solve(1); 51 | visited[i] = false; 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /a64_q2_table_no_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 100000007; 5 | 6 | int dp[2][3]; 7 | 8 | int main() { 9 | cin.tie(nullptr)->sync_with_stdio(false); 10 | 11 | int n; 12 | cin >> n; 13 | 14 | dp[1][0] = dp[1][1] = dp[1][2] = 1; 15 | for (int i = 2; i <= n; i++) { 16 | int now = i % 2, pre = now ^ 1; 17 | 18 | dp[now][0] = (dp[pre][0] + dp[pre][1] + dp[pre][2]) % MOD; 19 | dp[now][1] = (dp[pre][0] + dp[pre][2]) % MOD; 20 | dp[now][2] = (dp[pre][0] + dp[pre][1]) % MOD; 21 | } 22 | cout << (dp[n % 2][0] + dp[n % 2][1] + dp[n % 2][2]) % MOD; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /a64_q2_two_mcs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5e5 + 5; 5 | const int INF = 1e9 + 7; 6 | 7 | int A[MAX_N]; 8 | 9 | struct Node { 10 | int ans, ansMax, ansMin, maxLeftSide, maxRightSide; 11 | }; 12 | 13 | Node solve(int l, int r) { 14 | if (l == r) return {max(A[l], 0), max(A[l], 0), max(A[l], 0), max(A[l], 0), max(A[l], 0)}; 15 | 16 | int mid = (l + r) / 2; 17 | Node L = solve(l, mid); 18 | Node R = solve(mid + 1, r); 19 | 20 | int sumLeft = 0, sumRight = 0; 21 | int maxLeft = 0, maxRight = 0; 22 | int minLeft = 0, minRight = 0; 23 | for (int i = mid; i >= l; i--) { 24 | sumLeft += A[i]; 25 | maxLeft = max(maxLeft, sumLeft); 26 | minLeft = min(minLeft, sumLeft); 27 | } 28 | for (int i = mid + 1; i <= r; i++) { 29 | sumRight += A[i]; 30 | maxRight = max(maxRight, sumRight); 31 | minRight = min(minRight, sumRight); 32 | } 33 | 34 | int ans_ = max({L.ans, R.ans, L.ansMax + R.ansMax, L.maxRightSide + maxRight, maxLeft + R.maxLeftSide}); 35 | int ansMax_ = max({L.ansMax, R.ansMax, maxLeft + maxRight}); 36 | int ansMin_ = min({L.ansMin, R.ansMin, minLeft + minRight}); 37 | int maxLeftSide_ = max({L.maxLeftSide, sumLeft - L.ansMin + maxRight, sumLeft - minLeft + R.ansMax, sumLeft + R.maxLeftSide}); 38 | int maxRightSide_ = max({R.maxRightSide, maxLeft + sumRight - R.ansMin, L.ansMax + sumRight - minRight, L.maxRightSide + sumRight}); 39 | return {ans_, ansMax_, ansMin_, maxLeftSide_, maxRightSide_}; 40 | } 41 | 42 | int main() { 43 | cin.tie(nullptr)->sync_with_stdio(false); 44 | 45 | int n; 46 | cin >> n; 47 | 48 | int maxElement = -INF; 49 | for (int i = 1; i <= n; i++) { 50 | cin >> A[i]; 51 | 52 | maxElement = max(maxElement, A[i]); 53 | } 54 | 55 | if (maxElement < 0) cout << maxElement; 56 | else cout << solve(1, n).ans; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /a64_q4_path_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, K[8]; 5 | vector > graph[20]; 6 | bool visited[20]; 7 | 8 | bool solve(const int &u, const int &tw, const int &K) { 9 | if (tw == K) return true; 10 | if (tw > K) return false; 11 | 12 | int maxw = tw; 13 | for (int i = 0; i < n; i++) { 14 | if (visited[i] == false and !graph[i].empty()) maxw += graph[i][0].first; 15 | } 16 | if (maxw < K) return false; 17 | 18 | for (auto &[w, v] : graph[u]) { 19 | if (visited[v] == true) continue; 20 | 21 | visited[v] = true; 22 | bool ok = solve(v, tw + w, K); 23 | visited[v] = false; 24 | 25 | if (ok == true) return true; 26 | } 27 | return false; 28 | } 29 | 30 | int main() { 31 | cin.tie(nullptr)->sync_with_stdio(false); 32 | 33 | int m; 34 | cin >> n >> m; 35 | 36 | for (int i = 0; i < 8; i++) cin >> K[i]; 37 | 38 | while (m--) { 39 | int a, b, w; 40 | cin >> a >> b >> w; 41 | 42 | graph[a].emplace_back(w, b); 43 | graph[b].emplace_back(w, a); 44 | } 45 | 46 | for (int i = 0; i < n; i++) sort(graph[i].rbegin(), graph[i].rend()); 47 | 48 | for (int i = 0; i < 8; i++) { 49 | bool ok = false; 50 | for (int j = 0; j < n; j++) { 51 | visited[j] = true; 52 | ok |= solve(j, 0, K[i]); 53 | visited[j] = false; 54 | 55 | if (ok == true) break; 56 | } 57 | if (ok == true) cout << "YES\n"; 58 | else cout << "NO\n"; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /a64_​q1_​lawn_​mowing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5e5 + 5; 5 | 6 | vector a; 7 | 8 | int solve(int l, int r, long long x) { 9 | if (l == r) return l; 10 | 11 | int mid = (l + r + 1) / 2; 12 | if (a[mid] <= x) return solve(mid, r, x); 13 | return solve(l, mid - 1, x); 14 | } 15 | 16 | int main() { 17 | cin.tie(nullptr)->sync_with_stdio(false); 18 | 19 | int n, m; 20 | long long k; 21 | cin >> n >> m >> k; 22 | 23 | a.resize(n + 1); 24 | for (int i = 1; i <= n; i++) { 25 | cin >> a[i]; 26 | 27 | a[i] += a[i - 1] + k; 28 | } 29 | 30 | while (m--) { 31 | int l; 32 | long long b; 33 | cin >> l >> b; 34 | 35 | l++; 36 | int r = solve(l - 1, n, b + a[l - 1]); 37 | cout << a[r] - a[l - 1] - (r - l + 1) * k << '\n'; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /a64_​q3_​shortest_​start.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, m, k, V; 10 | cin >> n >> m >> k >> V; 11 | 12 | vector st(k); 13 | for (auto &x : st) cin >> x; 14 | 15 | vector >> graph(n); 16 | while (m--) { 17 | int a, b, w; 18 | cin >> a >> b >> w; 19 | 20 | graph[b].emplace_back(a, w); 21 | } 22 | 23 | vector dist(n, INF); 24 | 25 | priority_queue > pq; 26 | pq.emplace(0, V); 27 | dist[V] = 0; 28 | 29 | while (!pq.empty()) { 30 | int u = pq.top().second; 31 | pq.pop(); 32 | 33 | for (auto [v, w] : graph[u]) { 34 | if (dist[u] + w < dist[v]) { 35 | dist[v] = dist[u] + w; 36 | pq.emplace(-dist[v], v); 37 | } 38 | } 39 | } 40 | 41 | int ans = INF; 42 | for (auto &x : st) ans = min(ans, dist[x]); 43 | cout << ans; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /a64_​q3_​wildfire.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5005; 5 | 6 | int ans; 7 | int b[MAX_N], q[MAX_N]; 8 | vector graph[MAX_N]; 9 | bool visited[MAX_N]; 10 | 11 | void dfs(int u, int x) { 12 | if (visited[u] == true) return; 13 | visited[u] = true; 14 | 15 | ans += b[u]; 16 | for (auto v : graph[u]) dfs(v, x); 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int n, m, k; 23 | cin >> n >> m >> k; 24 | 25 | int sum = 0; 26 | for (int i = 0; i < n; i++) { 27 | cin >> b[i]; 28 | sum += b[i]; 29 | } 30 | for (int i = 1; i <= k; i++) cin >> q[i]; 31 | while (m--) { 32 | int a, b; 33 | cin >> a >> b; 34 | 35 | graph[a].push_back(b); 36 | } 37 | 38 | for (int i = 1; i <= k; i++) { 39 | dfs(q[i], q[i]); 40 | cout << sum - ans << ' '; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /a64_​q4_​closest_​subset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int n, k, min_diff = INF; 7 | int a[101], qs[101]; 8 | 9 | void solve(int i, int m, int s) { 10 | if (m == 0) { 11 | min_diff = min(min_diff, abs(s - k)); 12 | return; 13 | } 14 | if (s + qs[n] - qs[n - m] - k >= min_diff) return; 15 | if (k - (s + qs[m]) >= min_diff) return; 16 | if (n - i < m) return; 17 | if (i == n) return; 18 | 19 | solve(i + 1, m - 1, s + a[i]); 20 | solve(i + 1, m, s); 21 | } 22 | 23 | int main() { 24 | cin.tie(nullptr)->sync_with_stdio(false); 25 | 26 | int m; 27 | cin >> n >> m >> k; 28 | 29 | for (int i = 0; i < n; i++) cin >> a[i]; 30 | 31 | sort(a, a + n, greater ()); 32 | for (int i = 0; i < n; i++) qs[i + 1] = qs[i] + a[i]; 33 | 34 | solve(0, m, 0); 35 | 36 | cout << min_diff; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /a65_q0_degrees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int n; 8 | cin >> n; 9 | 10 | vector degree(n + 1); 11 | for (int i = 1; i <= n; i++) { 12 | for (int j = 1; j <= n; j++) { 13 | int a; 14 | cin >> a; 15 | 16 | degree[i] += a; 17 | } 18 | } 19 | 20 | vector cnt(n + 1); 21 | int max_degree = 0; 22 | for (int i = 1; i <= n; i++) { 23 | max_degree = max(max_degree, degree[i]); 24 | cnt[degree[i]]++; 25 | } 26 | 27 | for (int i = 0; i <= max_degree; i++) cout << cnt[i] << ' '; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /a65_q1_mt_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int u, v, w, p; 5 | 6 | int solve(long long n, long long r, long long c) { 7 | if (n == 2) { 8 | if (r == 1 and c == 1) return u; 9 | if (r == 1 and c == 2) return v; 10 | if (r == 2 and c == 1) return w; 11 | return p; 12 | } 13 | 14 | long long m = n / 2; 15 | if (r <= m and c <= m) return solve(m, r, c); 16 | if (r <= m and c > m) return solve(m, c - m, r); 17 | if (r > m and c <= m) return -solve(m, r - m, c); 18 | return -solve(m, c - m, r - m); 19 | } 20 | 21 | int main() { 22 | cin.tie(nullptr)->sync_with_stdio(false); 23 | 24 | int n, m; 25 | cin >> n >> m >> u >> v >> w >> p; 26 | 27 | n = min(n + 1, 60); 28 | while (m--) { 29 | long long r, c; 30 | cin >> r >> c; 31 | 32 | cout << solve(1ll< 2 | using namespace std; 3 | 4 | int n, m, k, w, sumh, ans; 5 | int p[10], h[10], t[10]; 6 | 7 | void solve(int i, int cnt) { 8 | if (i == k) { 9 | ans = min(ans, sumh - cnt); 10 | return; 11 | } 12 | 13 | for (int j = 0; j < m; j++) { 14 | if (abs(t[i] - p[j]) <= w and h[j] > 0) { 15 | h[j]--; 16 | solve(i + 1, cnt + 1); 17 | h[j]++; 18 | } 19 | else solve(i + 1, cnt); 20 | } 21 | } 22 | 23 | int main() { 24 | cin.tie(nullptr)->sync_with_stdio(false); 25 | 26 | cin >> n >> m >> k >> w; 27 | 28 | for (int i = 0; i < m; i++) cin >> p[i]; 29 | for (int i = 0; i < m; i++) { 30 | cin >> h[i]; 31 | 32 | sumh += h[i]; 33 | } 34 | for (int i = 0; i < k; i++) cin >> t[i]; 35 | 36 | ans = sumh; 37 | solve(0, 0); 38 | cout << ans; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /a65_q1b_street_art.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | 6 | int N; 7 | long long Y[MAX_N]; 8 | 9 | int solve(long long l, long long r) { 10 | int cnt = upper_bound(Y + 1, Y + N + 1, r) - lower_bound(Y + 1, Y + N + 1, l); 11 | if (cnt == 0) return 0; 12 | if (r - l + 1 < 3) return cnt; 13 | 14 | long long u = (r - l + 1) / 3; 15 | long long v = (r - l + 2) / 3; 16 | int r1 = solve(l, l + u - 1); 17 | int r2 = solve(l + u, l + u + v - 1); 18 | int r3 = solve(l + u + v, r); 19 | return r1 + r2 + r3 - min({r1, r2, r3}) + cnt; 20 | } 21 | 22 | int main() { 23 | cin.tie(nullptr)->sync_with_stdio(false); 24 | 25 | long long W; 26 | cin >> W >> N; 27 | 28 | for (int i = 1; i <= N; i++) cin >> Y[i]; 29 | 30 | sort(Y + 1, Y + N + 1); 31 | 32 | cout << solve(1ll, W); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /a65_q2a_distance_one.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 1e8 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, k; 10 | cin >> n >> k; 11 | 12 | int ans = 1; 13 | vector dp(n + 1); 14 | dp[0] = 1; 15 | for (int i = 1; i <= n; i++) { 16 | dp[i] = 1; 17 | for (int j = i - k; j >= 1; j--) dp[i] = (dp[i] + dp[j]) % MOD; 18 | ans = (ans + dp[i]) % MOD; 19 | } 20 | cout << ans; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /a65_q2a_huge_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, Q; 8 | cin >> N >> Q; 9 | 10 | map mp; 11 | for (int i = 0; i < N; i++) { 12 | int x, c; 13 | cin >> x >> c; 14 | 15 | mp[x] += c; 16 | } 17 | 18 | vector > vec; 19 | vec.emplace_back(0, 0); 20 | for (auto [k, v] : mp) vec.emplace_back(vec.back().first + v, k); 21 | 22 | while (Q--) { 23 | long long p; 24 | cin >> p; 25 | 26 | auto it = lower_bound(vec.begin(), vec.end(), make_pair(p, -1)); 27 | cout << it->second << '\n'; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /a65_q2b_arch_match.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | 6 | int A[MAX_N], dp[MAX_N][MAX_N]; 7 | 8 | int solve(int l, int r) { 9 | if (dp[l][r] != -1) return dp[l][r]; 10 | if (l >= r) return dp[l][r] = 0; 11 | 12 | dp[l][r] = max(0, solve(l + 1, r - 1) + A[l] * A[r]); 13 | for (int mid = l; mid < r; mid++) { 14 | dp[l][r] = max(dp[l][r], solve(l, mid) + solve(mid + 1, r)); 15 | } 16 | return dp[l][r]; 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int n; 23 | cin >> n; 24 | 25 | for (int i = 1; i <= n; i++) cin >> A[i]; 26 | 27 | memset(dp, -1, sizeof(dp)); 28 | cout << solve(1, n); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /a65_q3a_expressway.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 705; 5 | const int INF = 1e9 + 7; 6 | 7 | vector graph[MAX_N]; 8 | int c[MAX_N][MAX_N]; 9 | int dist[MAX_N]; 10 | bool visited[MAX_N]; 11 | 12 | int main() { 13 | cin.tie(nullptr)->sync_with_stdio(false); 14 | 15 | int n; 16 | cin >> n >> c[1][2]; 17 | c[2][1] = c[1][2]; 18 | 19 | graph[1].push_back(2); 20 | graph[2].push_back(1); 21 | for (int i = 3; i <= n; i++) { 22 | int k; 23 | cin >> k; 24 | 25 | while (k--) { 26 | int a; 27 | cin >> a >> c[i][a]; 28 | c[a][i] = c[i][a]; 29 | 30 | graph[i].push_back(a); 31 | graph[a].push_back(i); 32 | } 33 | 34 | for (int j = 1; j <= i; j++) dist[j] = INF, visited[j] = false; 35 | 36 | priority_queue > pq; 37 | pq.emplace(0, 1); 38 | dist[1] = 0; 39 | while (!pq.empty()) { 40 | int u = pq.top().second; 41 | pq.pop(); 42 | 43 | if (visited[u] == true) continue; 44 | visited[u] = true; 45 | 46 | if (u == 2) break; 47 | 48 | for (auto &v : graph[u]) { 49 | if (visited[v] == false and dist[u] + c[u][v] < dist[v]) { 50 | dist[v] = dist[u] + c[u][v]; 51 | pq.emplace(-dist[v], v); 52 | } 53 | } 54 | } 55 | cout << dist[2] << ' '; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /a65_q4_collector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int n, k, ans = INF; 7 | bitset <1024> b[80]; 8 | vector idx; 9 | int P[80]; 10 | 11 | void solve(int i, int tp, bitset <1024> mp) { 12 | if (tp >= ans) return; 13 | if (mp.count() == n) return void(ans = tp); 14 | if (i == k) return; 15 | 16 | solve(i + 1, tp + P[idx[i]], mp | b[idx[i]]); 17 | solve(i + 1, tp, mp); 18 | } 19 | 20 | int main() { 21 | cin.tie(nullptr)->sync_with_stdio(false); 22 | 23 | cin >> n >> k; 24 | 25 | for (int i = 0; i < k; i++) { 26 | int m; 27 | cin >> P[i] >> m; 28 | 29 | idx.push_back(i); 30 | while (m--) { 31 | int s; 32 | cin >> s; 33 | 34 | b[i][s - 1] = 1; 35 | } 36 | } 37 | 38 | sort(idx.begin(), idx.end(), [&](const int &x, const int &y) { 39 | return make_pair(b[x].count(), -P[x]) > make_pair(b[y].count(), -P[y]); 40 | }); 41 | 42 | solve(0, 0, bitset <1024> ()); 43 | cout << ans; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /a65_​q3a_​in_​the_​middle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, t[3]; 10 | cin >> n >> t[0] >> t[1] >> t[2]; 11 | 12 | vector > graph(n + 1); 13 | for (int i = 1; i <= n; i++) { 14 | int k; 15 | cin >> k; 16 | 17 | while (k--) { 18 | int e; 19 | cin >> e; 20 | 21 | graph[i].push_back(e); 22 | } 23 | } 24 | 25 | vector > dist(3, vector (n + 1, INF)); 26 | for (int i = 0; i < 3; i++) { 27 | queue q; 28 | q.push(t[i]); 29 | dist[i][t[i]] = 0; 30 | while (!q.empty()) { 31 | int u = q.front(); 32 | q.pop(); 33 | 34 | for (auto &v : graph[u]) { 35 | if (dist[i][v] == INF) { 36 | dist[i][v] = dist[i][u] + 1; 37 | q.push(v); 38 | } 39 | } 40 | } 41 | } 42 | 43 | int ans = INF; 44 | for (int i = 1; i <= n; i++) ans = min(ans, max({dist[0][i], dist[1][i], dist[2][i]})); 45 | cout << ans; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /a65_​q4_​tower_​defense_​2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int n, m, k, w; 8 | cin >> n >> m >> k >> w; 9 | 10 | vector p(m), h(m), idx(m); 11 | for (int i = 0; i < m; i++) cin >> p[i]; 12 | for (int i = 0; i < m; i++) cin >> h[i]; 13 | for (int i = 0; i < m; i++) idx[i] = i; 14 | sort(idx.begin(), idx.end(), [&](const int &a, const int &b) { 15 | return p[a] < p[b]; 16 | }); 17 | 18 | int i = 0, j = 0; 19 | while (i <= n and j < m and k > 0) { 20 | int x = max(i + 1, p[idx[j]] - w); 21 | if (x <= n and x - w <= p[idx[j]] and h[idx[j]] > 0) { 22 | h[idx[j]]--; 23 | k--; 24 | i = x; 25 | } 26 | else j++; 27 | } 28 | 29 | int sum = 0; 30 | for (int i = 0; i < m; i++) sum += h[i]; 31 | cout << sum; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /a66_f1_largest_diff_path.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 2005; 5 | const int INF = 1e9 + 7; 6 | 7 | int ans; 8 | int c[MAX_N], dp[MAX_N]; 9 | vector graph[MAX_N]; 10 | 11 | int dfs(const int &u) { 12 | if (dp[u] != -INF) return dp[u]; 13 | 14 | int res = -INF; 15 | for (auto v : graph[u]) res = max(res, dfs(v)); 16 | ans = max(ans, -c[u] + res); 17 | res = max(res, c[u]); 18 | return dp[u] = res; 19 | } 20 | 21 | int main() { 22 | cin.tie(nullptr)->sync_with_stdio(false); 23 | 24 | int n, m; 25 | cin >> n >> m; 26 | 27 | for (int i = 0; i < n; i++) cin >> c[i]; 28 | while (m--) { 29 | int a, b; 30 | cin >> a >> b; 31 | 32 | graph[a].push_back(b); 33 | } 34 | 35 | for (int i = 0; i < n; i++) dp[i] = -INF; 36 | for (int i = 0; i < n; i++) dfs(i); 37 | 38 | cout << ans; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /a66_f1_make_zero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 16777216; 5 | 6 | int ans = MOD; 7 | 8 | void solve(const int &c, const int &x) { 9 | if (c >= ans) return; 10 | if (x == 0) return void(ans = c); 11 | 12 | solve(c + 1, 2 * x % MOD); 13 | solve(c + 1, (x + 1) % MOD); 14 | } 15 | 16 | int main() { 17 | cin.tie(nullptr)->sync_with_stdio(false); 18 | 19 | int x; 20 | cin >> x; 21 | 22 | solve(0, x); 23 | 24 | cout << ans; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /a66_​f1_​ev_​truck.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | const int INF = 1e9 + 7; 6 | 7 | int dist[MAX_N][MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int n, m; 13 | cin >> n >> m; 14 | 15 | for (int i = 0; i < n; i++) { 16 | for (int j = 0; j < n; j++) { 17 | if (i != j) dist[i][j] = INF; 18 | } 19 | } 20 | 21 | while (m--) { 22 | int a, b, c; 23 | cin >> a >> b >> c; 24 | 25 | dist[a][b] = dist[b][a] = c; 26 | } 27 | 28 | for (int k = 0; k < n; k++) { 29 | for (int i = 0; i < n; i++) { 30 | for (int j = 0; j < n; j++) { 31 | dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); 32 | } 33 | } 34 | } 35 | 36 | int ans = 0; 37 | for (int i = 0; i < n; i++) { 38 | for (int j = 0; j < n; j++) { 39 | ans = max(ans, dist[i][j]); 40 | } 41 | } 42 | cout << ans; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /a66_​q1a_​day_​cover.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1000; 5 | const int MAX_M = 20; 6 | 7 | int n, m, cnt, ans; 8 | vector a[MAX_M], idx; 9 | int mp[MAX_N]; 10 | 11 | void solve(int i, int c) { 12 | if (c >= ans) return; 13 | if (cnt == n) return void(ans = c); 14 | 15 | for (int j = i; j < m; j++) { 16 | for (auto v : a[idx[j]]) if (mp[v]++ == 0) cnt++; 17 | solve(j + 1, c + 1); 18 | for (auto v : a[idx[j]]) if (--mp[v] == 0) cnt--; 19 | } 20 | } 21 | 22 | int main() { 23 | cin.tie(nullptr)->sync_with_stdio(false); 24 | 25 | cin >> n >> m; 26 | for (int i = 0; i < m; i++) { 27 | int k; 28 | cin >> k; 29 | 30 | idx.push_back(i); 31 | a[i].resize(k); 32 | for (auto &x : a[i]) { 33 | cin >> x; 34 | x--; 35 | } 36 | } 37 | 38 | sort(idx.begin(), idx.end(), [&](const int &x, const int &y) { 39 | return a[x].size() > a[y].size(); 40 | }); 41 | 42 | ans = m; 43 | solve(0, 0); 44 | cout << ans; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /a66_​q1a_​day_​cover_bitset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bitset <1000> a[20]; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int n, m; 10 | cin >> n >> m; 11 | 12 | for (int i = 0; i < m; i++) { 13 | int k; 14 | cin >> k; 15 | 16 | while (k--) { 17 | int d; 18 | cin >> d; 19 | 20 | d--; 21 | a[i][d] = 1; 22 | } 23 | } 24 | 25 | int ans = m; 26 | for (int mask = 0; mask < (1< chk; 28 | for (int i = 0; i < m; i++) { 29 | if (mask & (1< 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int a[300005]; 7 | 8 | int solve(int l, int r) { 9 | if (l == r) return a[l]; 10 | 11 | int mid = (l + r) / 2; 12 | int res = max(solve(l, mid), solve(mid + 1, r)); 13 | 14 | vector mp(2000, -INF); 15 | int sum = 0; 16 | for (int i = mid; i >= l; i--) { 17 | sum += a[i]; 18 | mp[a[i] + 1000] = max(mp[a[i] + 1000], sum); 19 | } 20 | 21 | sum = 0; 22 | for (int i = mid + 1; i <= r; i++) { 23 | sum += a[i]; 24 | if (mp[a[i] + 1000] != INF) res = max(res, mp[a[i] + 1000] + sum); 25 | } 26 | return res; 27 | } 28 | 29 | int main() { 30 | cin.tie(nullptr)->sync_with_stdio(false); 31 | 32 | int n; 33 | cin >> n; 34 | 35 | for (int i = 1; i <= n; i++) cin >> a[i]; 36 | 37 | cout << solve(1, n); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /a66_​q1b_​triforce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | 6 | int table[MAX_N][MAX_N]; 7 | 8 | int check(int a, int b, int c) { 9 | if (a == 0 or b == 0 or c == 0) return 0; 10 | if (a == b or a == c) return a; 11 | if (b == c) return b; 12 | return 0; 13 | } 14 | 15 | int solve(int x1, int x2, int y1, int y2) { 16 | if (x1 + 1 == x2 and y1 + 1 == y2) { 17 | if (table[x2][y2] != 0) return 0; 18 | 19 | return check(table[x1][y1], table[x1][y2], table[x2][y1]); 20 | } 21 | 22 | int mx = (x1 + x2) / 2; 23 | int my = (y1 + y2) / 2; 24 | 25 | for (int i = mx + 1; i <= x2; i++) { 26 | for (int j = my + 1; j <= y2; j++) { 27 | if (table[i][j] != 0) return 0; 28 | } 29 | } 30 | 31 | int a = solve(x1, mx, y1, my); 32 | int b = solve(x1, mx, my + 1, y2); 33 | int c = solve(mx + 1, x2, y1, my); 34 | return check(a, b, c); 35 | } 36 | 37 | int main() { 38 | cin.tie(nullptr)->sync_with_stdio(false); 39 | 40 | for (int q = 0; q < 3; q++) { 41 | int n; 42 | cin >> n; 43 | 44 | for (int i = 1; i <= n; i++) { 45 | for (int j = 1; j <= n; j++) { 46 | cin >> table[i][j]; 47 | } 48 | } 49 | 50 | cout << solve(1, n, 1, n) << '\n'; 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /a66_​q2a_​eating_​futomaki.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5005; 5 | 6 | int a[MAX_N]; 7 | int dp[MAX_N][MAX_N]; 8 | 9 | int solve(int l, int r) { 10 | if (dp[l][r] != -1) return dp[l][r]; 11 | if (l > r) return 0; 12 | if (l == r) return dp[l][r] = a[l]; 13 | 14 | return dp[l][r] = max({solve(l + 2, r) + max(a[l], a[l + 1]), 15 | solve(l, r - 2) + max(a[r - 1], a[r]), 16 | solve(l + 1, r - 1) + max(a[l], a[r])}); 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int n; 23 | cin >> n; 24 | 25 | for (int i = 1; i <= n; i++) cin >> a[i]; 26 | 27 | memset(dp, sizeof(dp), -1); 28 | cout << solve(1, n); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /a66_​q2a_​insertion_​count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int n, m; 8 | cin >> n >> m; 9 | 10 | vector a(n + 1); 11 | for (int i = 1; i <= n; i++) cin >> a[i]; 12 | 13 | while (m--) { 14 | int k; 15 | cin >> k; 16 | 17 | int cnt = 0; 18 | for (int i = 1; i <= n; i++) { 19 | if (a[i] == k) break; 20 | if (a[i] > k) cnt++; 21 | } 22 | cout << cnt << '\n'; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /a66_​q2b_​multi_​recurrence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | const int MOD = 32717; 6 | 7 | int dp[MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int n, k; 13 | cin >> n >> k; 14 | 15 | for (int i = 1; i <= k; i++) cin >> dp[i]; 16 | for (int i = k + 1; i <= n; i++) { 17 | int p; 18 | cin >> p; 19 | 20 | while (p--) { 21 | int b; 22 | cin >> b; 23 | 24 | if (i - b >= 0) dp[i] = (dp[i] + dp[i - b]) % MOD; 25 | } 26 | } 27 | 28 | for (int i = 1; i <= n; i++) cout << dp[i] << ' '; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /a66_​q3a_​hex_​map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 305; 5 | const int INF = 1e9 + 7; 6 | const int d[2][6][2] = {{{-1, 0}, {1, 0}, {0, -1}, {1, -1}, {0, 1}, {1, 1}}, {{-1, 0}, {1, 0}, {-1, -1}, {0, -1}, {-1, 1}, {0, 1}}}; 7 | 8 | int table[MAX_N][MAX_N]; 9 | int dist[MAX_N][MAX_N]; 10 | 11 | int main() { 12 | cin.tie(nullptr)->sync_with_stdio(false); 13 | 14 | int R, C; 15 | cin >> R >> C; 16 | 17 | int a1, b1, a2, b2; 18 | cin >> a1 >> b1 >> a2 >> b2; 19 | 20 | swap(a1, b1); 21 | swap(a2, b2); 22 | 23 | for (int i = 1; i <= R; i++) { 24 | for (int j = 1; j <= C; j++) { 25 | cin >> table[i][j]; 26 | dist[i][j] = INF; 27 | } 28 | } 29 | 30 | priority_queue > pq; 31 | pq.emplace(-table[a1][b1], a1, b1); 32 | dist[a1][b1] = table[a1][b1]; 33 | while (!pq.empty()) { 34 | auto [di, ux, uy] = pq.top(); 35 | pq.pop(); 36 | 37 | for (int i = 0; i < 6; i++) { 38 | int vx = ux + d[uy % 2][i][0], vy = uy + d[uy % 2][i][1]; 39 | if (vx < 1 or vx > R or vy < 1 or vy > C) continue; 40 | 41 | if (dist[ux][uy] + table[vx][vy] < dist[vx][vy]) { 42 | dist[vx][vy] = dist[ux][uy] + table[vx][vy]; 43 | pq.emplace(-dist[vx][vy], vx, vy); 44 | } 45 | } 46 | } 47 | cout << dist[a2][b2]; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /a66_​q3a_​sandworm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 705; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | int table[2][MAX_N][MAX_N]; 8 | int cnt[2][MAX_N][MAX_N]; 9 | int visited[MAX_N][MAX_N]; 10 | bool warp[MAX_N][MAX_N]; 11 | 12 | int main() { 13 | cin.tie(nullptr)->sync_with_stdio(false); 14 | 15 | int R, C, K; 16 | cin >> R >> C >> K; 17 | 18 | for (int t = 0; t < 2 ; t++) { 19 | for (int i = 1; i <= R; i++) { 20 | for (int j = 1; j <= C; j++) { 21 | cin >> table[t][i][j]; 22 | } 23 | } 24 | } 25 | while (K--) { 26 | int a, b; 27 | cin >> a >> b; 28 | 29 | warp[a][b] = true; 30 | } 31 | 32 | for (int t = 0; t < 2; t++) { 33 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) visited[i][j] = 0; 34 | for (int i = 1; i <= R; i++) { 35 | for (int j = 1; j <= C; j++) { 36 | if (visited[i][j] != 0 or table[t][i][j] == 1) continue; 37 | 38 | queue > q; 39 | q.emplace(i, j); 40 | visited[i][j] = 1; 41 | int sum = 0; 42 | while (!q.empty()) { 43 | auto [ux, uy] = q.front(); 44 | q.pop(); 45 | 46 | sum++; 47 | for (int z = 0; z < 4; z++) { 48 | int vx = ux + d[z][0], vy = uy + d[z][1]; 49 | if (vx < 1 or vx > R or vy < 1 or vy > C) continue; 50 | 51 | if (table[t][vx][vy] == 0 and visited[vx][vy] == 0) { 52 | visited[vx][vy] = 1; 53 | q.emplace(vx, vy); 54 | } 55 | } 56 | } 57 | 58 | q.emplace(i, j); 59 | visited[i][j] = 2; 60 | while (!q.empty()) { 61 | auto [ux, uy] = q.front(); 62 | q.pop(); 63 | 64 | cnt[t][ux][uy] = sum; 65 | for (int z = 0; z < 4; z++) { 66 | int vx = ux + d[z][0], vy = uy + d[z][1]; 67 | if (vx < 1 or vx > R or vy < 1 or vy > C) continue; 68 | 69 | if (table[t][vx][vy] == 0 and visited[vx][vy] == 1) { 70 | visited[vx][vy] = 2; 71 | q.emplace(vx, vy); 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | 79 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) visited[i][j] = 0; 80 | 81 | int ans = cnt[0][1][1]; 82 | queue > q; 83 | q.emplace(1, 1); 84 | visited[1][1] = 1; 85 | while (!q.empty()) { 86 | auto [ux, uy] = q.front(); 87 | q.pop(); 88 | 89 | if (warp[ux][uy] == true) ans = max(ans, cnt[0][1][1] + cnt[1][ux][uy]); 90 | for (int z = 0; z < 4; z++) { 91 | int vx = ux + d[z][0], vy = uy + d[z][1]; 92 | if (vx < 1 or vx > R or vy < 1 or vy > C) continue; 93 | 94 | if (table[0][vx][vy] == 0 and visited[vx][vy] == 0) { 95 | visited[vx][vy] = 1; 96 | q.emplace(vx, vy); 97 | } 98 | } 99 | } 100 | cout << ans; 101 | return 0; 102 | } -------------------------------------------------------------------------------- /a66_​q3b_​hex_​map_​v2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 305; 5 | const int d[2][6][2] = {{{0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {1, -1}, {1, 0}}, 6 | {{0, -1}, {0, 1}, {-1, 0}, {-1, 1}, {1, 0}, {1, 1}}}; 7 | const int INF = 1e9 + 7; 8 | 9 | int a[MAX_N][MAX_N]; 10 | int dist[MAX_N][MAX_N]; 11 | bool visited[MAX_N][MAX_N]; 12 | 13 | int main() { 14 | cin.tie(nullptr)->sync_with_stdio(false); 15 | 16 | int R, C; 17 | cin >> R >> C; 18 | 19 | int a1, b1, a2, b2; 20 | cin >> a1 >> b1 >> a2 >> b2; 21 | 22 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) cin >> a[i][j]; 23 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) dist[i][j] = INF; 24 | 25 | priority_queue > pq; 26 | pq.emplace(-a[a1][b1], a1, b1); 27 | dist[a1][b1] = a[a1][b1]; 28 | while (!pq.empty()) { 29 | auto [di, x, y] = pq.top(); 30 | pq.pop(); 31 | 32 | if (visited[x][y] == true) continue; 33 | visited[x][y] = true; 34 | 35 | for (int i = 0; i < 6; i++) { 36 | int nx = x + d[x % 2][i][0], ny = y + d[x % 2][i][1]; 37 | if (nx < 1 or nx > R or ny < 1 or ny > C) continue; 38 | if (visited[nx][ny] == false and dist[x][y] + a[nx][ny] < dist[nx][ny]) { 39 | dist[nx][ny] = dist[x][y] + a[nx][ny]; 40 | pq.emplace(-dist[nx][ny], nx, ny); 41 | } 42 | } 43 | } 44 | cout << dist[a2][b2]; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /a66_​q4a_​billboard_​feng_​shui.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, k, ans; 5 | int c[55], qc[55]; 6 | bitset <55> p; 7 | 8 | void solve(int i, int sum, bitset <55> s) { 9 | if (i == n) return void(ans = max(ans, sum)); 10 | if (sum + qc[i] <= ans) return; 11 | 12 | if (i < k - 1) { 13 | s[i] = 1; 14 | if (i == 0 or (i > 0 and s[i - 1] == 0)) solve(i + 1, sum + c[i], s); 15 | s[i] = 0; 16 | solve(i + 1, sum, s); 17 | } 18 | else { 19 | if (i >= k) s >>= 1; 20 | s[k - 1] = 1; 21 | if (s[k - 2] == 0 and s != p) solve(i + 1, sum + c[i], s); 22 | s[k - 1] = 0; 23 | if (s != p) solve(i + 1, sum, s); 24 | } 25 | } 26 | 27 | int main() { 28 | cin.tie(nullptr)->sync_with_stdio(false); 29 | 30 | cin >> n >> k; 31 | 32 | for (int i = 0; i < n; i++) cin >> c[i]; 33 | for (int i = 0; i < k; i++) { 34 | int x; 35 | cin >> x; 36 | p[i] = x; 37 | } 38 | 39 | qc[n - 1] = c[n - 1]; 40 | for (int i = n - 2; i >= 0; i--) qc[i] = qc[i + 1] + c[i]; 41 | 42 | solve(0, 0, bitset <55> ()); 43 | 44 | cout << ans; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /a66_​q4a_​clique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, ans; 5 | int b[50], qb[50]; 6 | bitset <50> f[50]; 7 | 8 | void solve(int i, int sum, bitset <50> s, bitset <50> chosen) { 9 | if (i == n) return void(ans = max(ans, sum)); 10 | if (sum + qb[i] <= ans) return; 11 | 12 | chosen[i] = 1; 13 | if ((s & f[i] & chosen) == chosen) solve(i + 1, sum + b[i], s & f[i], chosen); 14 | chosen[i] = 0; 15 | 16 | solve(i + 1, sum, s, chosen); 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | cin >> n; 23 | 24 | for (int i = 0; i < n; i++) cin >> b[i]; 25 | for (int i = 0; i < n; i++) { 26 | for (int j = 0; j < n; j++) { 27 | int x; 28 | cin >> x; 29 | 30 | f[i][j] = x; 31 | } 32 | } 33 | 34 | qb[n - 1] = b[n - 1]; 35 | for (int i = n - 2; i >= 0; i--) qb[i] = qb[i + 1] + b[i]; 36 | 37 | bitset <50> tmp; 38 | for (int i = 0; i < n; i++) tmp[i] = 1; 39 | 40 | solve(0, 0, bitset <50> ().set(), bitset <50> ()); 41 | 42 | cout << ans; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /da66_f1_buffet_flight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 2005; 5 | const int INF = 1e9 + 7; 6 | 7 | int ci[MAX_N], co[MAX_N]; 8 | vector graph[MAX_N]; 9 | int dist[MAX_N]; 10 | bool visited[MAX_N]; 11 | 12 | int main() { 13 | cin.tie(nullptr)->sync_with_stdio(false); 14 | 15 | int n, m; 16 | cin >> n >> m; 17 | 18 | for (int i = 0; i < n; i++) cin >> ci[i]; 19 | for (int i = 0; i < n; i++) cin >> co[i]; 20 | while (m--) { 21 | int a, b; 22 | cin >> a >> b; 23 | 24 | graph[a].push_back(b); 25 | } 26 | 27 | for (int i = 0; i < n; i++) dist[i] = INF; 28 | 29 | priority_queue > pq; 30 | pq.emplace(0, 0); 31 | dist[0] = 0; 32 | while (!pq.empty()) { 33 | int u = pq.top().second; 34 | pq.pop(); 35 | 36 | if (visited[u] == true) continue; 37 | visited[u] = true; 38 | 39 | for (auto v : graph[u]) { 40 | if (visited[v] == false and dist[u] + co[u] + ci[v] < dist[v]) { 41 | dist[v] = dist[u] + co[u] + ci[v]; 42 | pq.emplace(-dist[v], v); 43 | } 44 | } 45 | } 46 | 47 | for (int i = 0; i < n; i++) { 48 | if (dist[i] == INF) dist[i] = -1; 49 | cout << dist[i] << ' '; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /da66_f1_linear_recurrence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | const int MOD = 32717; 6 | 7 | int c[MAX_N], a[MAX_N], R[MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int k, n; 13 | cin >> k >> n; 14 | 15 | for (int i = 1; i <= k; i++) cin >> c[i]; 16 | for (int i = 0; i < k; i++) cin >> a[i]; 17 | 18 | for (int i = 0; i < k; i++) R[i] = a[i]; 19 | for (int i = k; i <= n; i++) { 20 | for (int j = 1; j <= k; j++) { 21 | R[i] = (R[i] + c[j] * R[i - j] % MOD) % MOD; 22 | } 23 | } 24 | cout << R[n]; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /da66_f1_map_walk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 15; 5 | 6 | int R, C; 7 | int a[MAX_N][MAX_N]; 8 | string path; 9 | 10 | void solve(int i, int j) { 11 | if (i < 1 or i > R or j < 1 or j > C or a[i][j] != 0) return; 12 | if (i == R and j == C) { 13 | cout << path << '\n'; 14 | return; 15 | } 16 | 17 | a[i][j] = 2; 18 | path.push_back('A'); 19 | solve(i, j + 1); 20 | path[path.size() - 1] = 'B'; 21 | solve(i + 1, j); 22 | path[path.size() - 1] = 'C'; 23 | solve(i - 1, j); 24 | path.pop_back(); 25 | a[i][j] = 0; 26 | } 27 | 28 | int main() { 29 | cin.tie(nullptr)->sync_with_stdio(false); 30 | 31 | cin >> R >> C; 32 | 33 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) cin >> a[i][j]; 34 | 35 | solve(1, 1); 36 | cout << "DONE"; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /da66_f1_sea_bridge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5005; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | int a[MAX_N][MAX_N], dist[MAX_N][MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int R, C; 13 | cin >> R >> C; 14 | 15 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) cin >> a[i][j]; 16 | 17 | queue > q; 18 | for (int i = 1; i <= R; i++) { 19 | for (int j = 1; j <= C; j++) { 20 | if (a[i][j] == 1) { 21 | q.emplace(i, j); 22 | dist[i][j] = 1; 23 | } 24 | } 25 | } 26 | while (!q.empty()) { 27 | auto [ux, uy] = q.front(); 28 | q.pop(); 29 | 30 | if (a[ux][uy] == 2) { 31 | cout << dist[ux][uy]; 32 | return 0; 33 | } 34 | for (int i = 0; i < 4; i++) { 35 | int vx = ux + d[i][0], vy = uy + d[i][1]; 36 | if (vx < 1 or vx > R or vy < 1 or vy > C or a[vx][vy] == 3) continue; 37 | if (dist[vx][vy] == 0) { 38 | dist[vx][vy] = dist[ux][uy] + 1; 39 | q.emplace(vx, vy); 40 | } 41 | } 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /da66_f2_grid_loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1000; 5 | 6 | int R, C; 7 | int a[MAX_N][MAX_N]; 8 | vector graph[MAX_N * MAX_N]; 9 | bool visited[MAX_N * MAX_N]; 10 | 11 | int node(int i, int j) { 12 | if (i < 0 or i >= R or j < 0 or j >= C) return -1; 13 | return i * C + j; 14 | } 15 | 16 | bool is_horizontal_left(int i, int j) { 17 | return a[i][j] == 1 or a[i][j] == 3 or a[i][j] == 4; 18 | } 19 | 20 | bool is_horizontal_right(int i, int j) { 21 | return a[i][j] == 1 or a[i][j] == 5 or a[i][j] == 6; 22 | } 23 | 24 | bool is_vertical_top(int i, int j) { 25 | return a[i][j] == 2 or a[i][j] == 4 or a[i][j] == 5; 26 | } 27 | 28 | bool is_vertical_bottom(int i, int j) { 29 | return a[i][j] == 2 or a[i][j] == 3 or a[i][j] == 6; 30 | } 31 | 32 | void connect(int x1, int y1, int x2, int y2) { 33 | int u = node(x1, y1), v = node(x2, y2); 34 | graph[u].push_back(v); 35 | } 36 | 37 | int main() { 38 | cin.tie(nullptr)->sync_with_stdio(false); 39 | 40 | cin >> R >> C; 41 | 42 | for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) cin >> a[i][j]; 43 | for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) { 44 | if (j - 1 >= 0 and is_horizontal_left(i, j - 1) == true and is_horizontal_right(i, j) == true) connect(i, j, i, j - 1); 45 | if (j + 1 < C and is_horizontal_left(i, j) == true and is_horizontal_right(i, j + 1) == true) connect(i, j, i, j + 1); 46 | if (i - 1 >= 0 and is_vertical_top(i - 1, j) == true and is_vertical_bottom(i, j) == true) connect(i, j, i - 1, j); 47 | if (i + 1 < R and is_vertical_top(i, j) == true and is_vertical_bottom(i + 1, j) == true) connect(i, j, i + 1, j); 48 | } 49 | 50 | int cnt_cycle = 0, max_len = 0; 51 | for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) if (visited[node(i, j)] == false) { 52 | int s = node(i, j), cnt = 0; 53 | queue > q; 54 | q.emplace(s, -1); 55 | visited[s] = true; 56 | bool is_cycle = false; 57 | while (!q.empty()) { 58 | auto [u, p] = q.front(); 59 | q.pop(); 60 | 61 | cnt++; 62 | for (auto v : graph[u]) if (v != p) { 63 | if (visited[v] == false) { 64 | visited[v] = true; 65 | q.emplace(v, u); 66 | } 67 | else is_cycle = true; 68 | } 69 | } 70 | if (is_cycle == true) { 71 | cnt_cycle++; 72 | max_len = max(max_len, cnt); 73 | } 74 | } 75 | cout << cnt_cycle << ' ' << max_len; 76 | return 0; 77 | } -------------------------------------------------------------------------------- /da66_f2_new_game_plus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 505; 5 | const int MOD = 1e8 + 7; 6 | 7 | int table[MAX_N][MAX_N], dp[MAX_N][MAX_N][3]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int R, C; 13 | cin >> R >> C; 14 | 15 | for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) cin >> table[i][j]; 16 | 17 | for (int i = 1; i <= R; i++) if (table[i][1] == 0) { 18 | if (i - 1 >= 0 and table[i - 1][2] == 0) dp[i - 1][2][0]++; 19 | if (table[i][2] == 0) dp[i][2][1]++; 20 | if (i + 1 <= R and table[i + 1][2] == 0) dp[i + 1][2][2]++; 21 | } 22 | for (int j = 2; j < C; j++) for (int i = 1; i <= R; i++) if (table[i][j] == 0) { 23 | if (i - 1 >= 0 and table[i - 1][j + 1] == 0) dp[i - 1][j + 1][0] = (dp[i - 1][j + 1][0] + dp[i][j][1] + dp[i][j][2]) % MOD; 24 | if (table[i][j + 1] == 0) dp[i][j + 1][1] = (dp[i][j + 1][1] + dp[i][j][0] + dp[i][j][2]) % MOD; 25 | if (i + 1 <= R and table[i + 1][j + 1] == 0) dp[i + 1][j + 1][2] = (dp[i + 1][j + 1][2] + dp[i][j][0] + dp[i][j][1]) % MOD; 26 | } 27 | 28 | int ans = 0; 29 | for (int i = 1; i <= R; i++) ans = (ans + dp[i][C][0] + dp[i][C][1] + dp[i][C][2]) % MOD; 30 | cout << ans; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /da66_m1_line_paint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector > vec; 11 | for (int i = 1; i <= N; i++) { 12 | int a, b; 13 | cin >> a >> b; 14 | 15 | vec.emplace_back(a, b); 16 | } 17 | 18 | sort(vec.begin(), vec.end()); 19 | 20 | int farthest = -1; 21 | for (int i = 0; i < N; i++) { 22 | if (farthest + 1 < vec[i].first) { 23 | if (farthest != -1) cout << farthest << ' '; 24 | cout << vec[i].first << ' '; 25 | } 26 | farthest = max(farthest, vec[i].second); 27 | } 28 | cout << farthest; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /ex00e4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int a, b; 5 | string s; 6 | 7 | void solve(int c, int len) { 8 | if (c > a or len > b) return; 9 | if (c == a and len == b) { 10 | cout << s << '\n'; 11 | return; 12 | } 13 | 14 | s[len] = '1'; 15 | solve(c + 1, len + 1); 16 | s[len] = '0'; 17 | solve(c, len + 1); 18 | } 19 | 20 | int main() { 21 | cin.tie(nullptr)->sync_with_stdio(false); 22 | 23 | cin >> a >> b; 24 | 25 | s = string(b, '0'); 26 | 27 | solve(0, 0); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /ex00e5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, k; 5 | bool b[25]; 6 | 7 | void solve(int i, int c, bool valid) { 8 | if (i == n) { 9 | if (valid == true) { 10 | for (int i = 0; i < n; i++) cout << b[i]; 11 | cout << '\n'; 12 | } 13 | return; 14 | } 15 | b[i] = 0; 16 | solve(i + 1, 0, valid); 17 | b[i] = 1; 18 | solve(i + 1, c + 1, valid | (c + 1 == k)); 19 | } 20 | 21 | int main() { 22 | cin.tie(nullptr)->sync_with_stdio(false); 23 | 24 | cin >> n >> k; 25 | 26 | solve(0, 0, k == 0); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /ex01e2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1e5 + 5; 5 | const long long INF = 1e18 + 7; 6 | 7 | int A[MAX_N]; 8 | 9 | long long solve(int l, int r) { 10 | if (l == r) return A[l]; 11 | 12 | int mid = (l + r) / 2; 13 | long long res = max(solve(l, mid), solve(mid + 1, r)); 14 | 15 | long long maxLeft = -INF, maxRight = -INF; 16 | long long sumLeft = 0, sumRight = 0; 17 | for (int i = mid; i >= l; i--) { 18 | sumLeft += A[i]; 19 | maxLeft = max(maxLeft, sumLeft); 20 | } 21 | for (int i = mid + 1; i <= r; i++) { 22 | sumRight += A[i]; 23 | maxRight = max(maxRight, sumRight); 24 | } 25 | res = max(res, maxLeft + maxRight); 26 | return res; 27 | } 28 | 29 | int main() { 30 | cin.tie(nullptr)->sync_with_stdio(false); 31 | 32 | int N; 33 | cin >> N; 34 | 35 | for (int i = 1; i <= N; i++) cin >> A[i]; 36 | 37 | cout << solve(1, N); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /ex01e3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, M; 8 | cin >> N >> M; 9 | 10 | vector a(N); 11 | for (auto &x : a) cin >> x; 12 | 13 | while (M--) { 14 | int x; 15 | cin >> x; 16 | 17 | int l = 0, r = N - 1, ans = -1; 18 | while (l <= r) { 19 | int mid = (l + r) / 2; 20 | 21 | if (a[mid] <= x) l = mid + 1, ans = a[mid]; 22 | else r = mid - 1; 23 | } 24 | cout << ans << '\n'; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /ex01e4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | long long X, N, K; 8 | cin >> X >> N >> K; 9 | 10 | long long res = 1; 11 | while (N > 0) { 12 | if (N % 2 == 1) res = res * X % K; 13 | 14 | X = X * X % K; 15 | N /= 2; 16 | } 17 | cout << res; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /ex01h1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector position; 5 | 6 | int solve(int l, int r, int x) { 7 | if (l == r) return l; 8 | 9 | int mid = (l + r) / 2; 10 | if (position[mid] >= x) return solve(l, mid, x); 11 | return solve(mid + 1, r, x); 12 | } 13 | 14 | int main() { 15 | cin.tie(nullptr)->sync_with_stdio(false); 16 | 17 | position.push_back(0); 18 | position.push_back(1); 19 | position.push_back(3); 20 | for (int i = 3, j = 2;; i++) { 21 | if (i > position[j]) j++; 22 | 23 | position.push_back(position.back() + j); 24 | if (position.back() > 2e9) break; 25 | } 26 | 27 | int N; 28 | cin >> N; 29 | 30 | while (N--) { 31 | int X; 32 | cin >> X; 33 | 34 | cout << solve(1, position.size() - 1, X) << '\n'; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /ex01m1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector > ans; 5 | 6 | void solve(int x1, int x2, int y1, int y2, int x, int y) { 7 | if (x1 == x2 and y1 == y2) return; 8 | 9 | int xm = (x1 + x2) / 2; 10 | int ym = (y1 + y2) / 2; 11 | int type; 12 | 13 | if (x <= xm and y <= ym) { 14 | type = 0; 15 | solve(x1, xm, y1, ym, x, y); 16 | solve(x1, xm, ym + 1, y2, xm, ym + 1); 17 | solve(xm + 1, x2, y1, ym, xm + 1, ym); 18 | solve(xm + 1, x2, ym + 1, y2, xm + 1, ym + 1); 19 | } 20 | else if (x <= xm and y > ym) { 21 | type = 1; 22 | solve(x1, xm, y1, ym, xm, ym); 23 | solve(x1, xm, ym + 1, y2, x, y); 24 | solve(xm + 1, x2, y1, ym, xm + 1, ym); 25 | solve(xm + 1, x2, ym + 1, y2, xm + 1, ym + 1); 26 | } 27 | else if (x > xm and y <= ym) { 28 | type = 2; 29 | solve(x1, xm, y1, ym, xm, ym); 30 | solve(x1, xm, ym + 1, y2, xm, ym + 1); 31 | solve(xm + 1, x2, y1, ym, x, y); 32 | solve(xm + 1, x2, ym + 1, y2, xm + 1, ym + 1); 33 | } 34 | else if (x > xm and y > ym) { 35 | type = 3; 36 | solve(x1, xm, y1, ym, xm, ym); 37 | solve(x1, xm, ym + 1, y2, xm, ym + 1); 38 | solve(xm + 1, x2, y1, ym, xm + 1, ym); 39 | solve(xm + 1, x2, ym + 1, y2, x, y); 40 | } 41 | ans.emplace_back(type, ym, xm); 42 | } 43 | 44 | int main() { 45 | cin.tie(nullptr)->sync_with_stdio(false); 46 | 47 | int L, X, Y; 48 | cin >> L >> X >> Y; 49 | 50 | solve(0, L - 1, 0, L - 1, Y, X); 51 | 52 | cout << ans.size() << '\n'; 53 | for (auto [t, y, x] : ans) cout << t << ' ' << y << ' ' << x << '\n'; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /ex01m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector A; 5 | 6 | long long solve(int l, int r) { 7 | if (l == r) return 0; 8 | 9 | int mid = (l + r) / 2; 10 | long long res = solve(l, mid) + solve(mid + 1, r); 11 | 12 | vector L, R; 13 | for (int i = l; i <= mid; i++) L.push_back(A[i]); 14 | for (int i = mid + 1; i <= r; i++) R.push_back(A[i]); 15 | 16 | int i = 0, j = 0, k = l; 17 | while (i < L.size() and j < R.size()) { 18 | if (L[i] <= R[j]) A[k++] = L[i++]; 19 | else res += L.size() - i, A[k++] = R[j++]; 20 | } 21 | while (i < L.size()) A[k++] = L[i++]; 22 | while (j < R.size()) A[k++] = R[j++]; 23 | 24 | return res; 25 | } 26 | 27 | int main() { 28 | cin.tie(nullptr)->sync_with_stdio(false); 29 | 30 | int N; 31 | cin >> N; 32 | 33 | A.resize(N); 34 | for (auto &x : A) cin >> x; 35 | 36 | cout << solve(0, N - 1); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /ex01m3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | int N; 7 | int T[MAX_N]; 8 | 9 | long long solve(long long l, long long r, long long c) { 10 | if (l == r) return l; 11 | 12 | long long m = (l + r) / 2, sum = 0; 13 | for (int i = 1; i <= N; i++) sum += m / T[i]; 14 | 15 | if (sum >= c) return solve(l, m, c); 16 | return solve(m + 1, r, c); 17 | } 18 | 19 | int main() { 20 | cin.tie(nullptr)->sync_with_stdio(false); 21 | 22 | int A; 23 | cin >> N >> A; 24 | 25 | for (int i = 1; i <= N; i++) cin >> T[i]; 26 | 27 | while (A--) { 28 | long long c; 29 | cin >> c; 30 | 31 | cout << solve(0, 1e12, c - N) << '\n'; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /ex02e1_fibo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N; 10 | cin >> N; 11 | 12 | cout << fibo(N); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /ex02e2_​cnk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, K; 8 | cin >> N >> K; 9 | 10 | vector > dp(N + 1, vector (N + 1)); 11 | for (int n = 0; n <= N; n++) { 12 | for (int k = 0; k <= n; k++) { 13 | if (k == 0 or k == n) dp[n][k] = 1; 14 | else dp[n][k] = dp[n - 1][k - 1] + dp[n - 1][k]; 15 | } 16 | } 17 | cout << dp[N][K]; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /ex02e3_bs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, M; 8 | cin >> N >> M; 9 | 10 | vector a(N); 11 | for (auto &x : a) cin >> x; 12 | 13 | while (M--) { 14 | int x; 15 | cin >> x; 16 | 17 | int l = 0, r = N - 1, ans = -1; 18 | while (l <= r) { 19 | int mid = (l + r) / 2; 20 | 21 | if (a[mid] <= x) l = mid + 1, ans = mid; 22 | else r = mid - 1; 23 | } 24 | cout << ans << '\n'; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /ex02h1_closest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int getDistance(pair a, pair b) { 7 | int diffX = a.first - b.first; 8 | int diffY = a.second - b.second; 9 | return diffX * diffX + diffY * diffY; 10 | } 11 | 12 | int solve(vector > &sortedX, vector > &sortedY) { 13 | int N = sortedX.size(); 14 | if (N == 1) return INF; 15 | if (N == 2) return getDistance(sortedX[0], sortedX[1]); 16 | 17 | int mid = (N - 1) / 2; 18 | vector > XL, XR, YL, YR; 19 | for (int i = 0; i <= mid; i++) XL.push_back(sortedX[i]); 20 | for (int i = mid + 1; i < N; i++) XR.push_back(sortedX[i]); 21 | for (auto [x, y] : sortedY) { 22 | if (x <= XL.back().first) YL.emplace_back(x, y); 23 | else YR.emplace_back(x, y); 24 | } 25 | 26 | int minDist = min(solve(XL, YL), solve(XR, YR)); 27 | vector > strip; 28 | for (auto [x, y] : sortedY) { 29 | if ((x - XL.back().first) * (x - XL.back().first) < minDist) strip.emplace_back(x, y); 30 | } 31 | 32 | for (int i = 0; i < strip.size(); i++) { 33 | for (int j = i + 1; j < strip.size(); j++) { 34 | if ((strip[j].second - strip[i].second) * (strip[j].second - strip[i].second) >= minDist) break; 35 | 36 | minDist = min(minDist, getDistance(strip[i], strip[j])); 37 | } 38 | } 39 | return minDist; 40 | } 41 | 42 | int main() { 43 | cin.tie(nullptr)->sync_with_stdio(false); 44 | 45 | int N; 46 | cin >> N; 47 | 48 | vector > sortedX, sortedY; 49 | for (int i = 0; i < N; i++) { 50 | int x, y; 51 | cin >> x >> y; 52 | 53 | sortedX.emplace_back(x, y); 54 | sortedY.emplace_back(y, x); 55 | } 56 | 57 | sort(sortedX.begin(), sortedX.end()); 58 | sort(sortedY.begin(), sortedY.end()); 59 | for (auto &[y, x] : sortedY) swap(x, y); 60 | 61 | cout << solve(sortedX, sortedY); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /ex02m1_triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector > dp(N + 1, vector (N + 1)); 11 | for (int i = 1; i <= N; i++) { 12 | for (int j = 1; j <= i; j++) { 13 | int a; 14 | cin >> a; 15 | 16 | dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + a; 17 | } 18 | } 19 | 20 | int ans = 0; 21 | for (int i = 1; i <= N; i++) ans = max(ans, dp[N][i]); 22 | 23 | cout << ans << '\n'; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /ex02m2_barcode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, M, K; 8 | cin >> N >> M >> K; 9 | 10 | vector >> dp(N + 1, vector > (M + 1, vector (K + 1))); 11 | dp[1][1][0] = 1; 12 | for (int i = 2; i <= N; i++) { 13 | for (int j = 1; j <= M; j++) { 14 | for (int k = 0; k <= K; k++) { 15 | if (j - 1 >= 1) dp[i][j][k] += dp[i - 1][j - 1][k]; 16 | if (k - 1 >= 0) dp[i][1][k] += dp[i - 1][j][k - 1]; 17 | } 18 | } 19 | } 20 | 21 | int ans = 0; 22 | for (int i = 1; i <= M; i++) ans += dp[N][i][K]; 23 | 24 | cout << ans; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /ex03e1_​lcs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | string a, b; 8 | cin >> a >> b; 9 | 10 | int n = a.size(), m = b.size(); 11 | vector > dp(n + 1, vector (m + 1)); 12 | for (int i = 1; i <= n; i++) { 13 | for (int j = 1; j <= m; j++) { 14 | if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; 15 | else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 16 | } 17 | } 18 | cout << dp[n][m]; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /ex03e2_​mcm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 105; 5 | const long long INF = 1e18 + 7; 6 | 7 | long long a[MAX_N]; 8 | long long dp[MAX_N][MAX_N]; 9 | 10 | int main() { 11 | cin.tie(nullptr)->sync_with_stdio(false); 12 | 13 | int n; 14 | cin >> n; 15 | 16 | for (int i = 1; i <= n + 1; i++) cin >> a[i]; 17 | 18 | for (int len = 2; len <= n; len++) { 19 | for (int l = 1, r = len; r <= n; l++, r++) { 20 | dp[l][r] = INF; 21 | for (int m = l; m < r; m++) { 22 | dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + a[l] * a[m + 1] * a[r + 1]); 23 | } 24 | } 25 | } 26 | cout << dp[1][n]; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /ex03e3_​coin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N, M; 10 | cin >> N >> M; 11 | 12 | vector a(N); 13 | for (auto &x : a) cin >> x; 14 | 15 | vector dp(M + 1); 16 | for (int i = 1; i <= M; i++) { 17 | dp[i] = INF; 18 | for (int j = 0; j < N; j++) { 19 | if (i - a[j] >= 0) dp[i] = min(dp[i], dp[i - a[j]] + 1); 20 | } 21 | } 22 | cout << dp[M]; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /ex03h1_​square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int R, C; 8 | cin >> R >> C; 9 | 10 | int ans = 0; 11 | vector > dp(R + 1, vector (C + 1)); 12 | for (int i = 1; i <= R; i++) { 13 | for (int j = 1; j <= C; j++) { 14 | char a; 15 | cin >> a; 16 | 17 | if (a == '1') dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1; 18 | ans = max(ans, dp[i][j]); 19 | } 20 | } 21 | cout << ans; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /ex03m1_billboardex03m1_billboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector dp(N + 1); 11 | for (int i = 1; i <= N; i++) { 12 | int c; 13 | cin >> c; 14 | 15 | dp[i] = max(dp[i - 1], dp[max(0, i - 2)] + c); 16 | } 17 | cout << dp[N]; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /ex03m2_lis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector S(N + 1), dp(N + 1); 11 | for (int i = 1; i <= N; i++) cin >> S[i]; 12 | 13 | int ans = 0; 14 | for (int i = 1; i <= N; i++) { 15 | dp[i] = 1; 16 | for (int j = 1; j < i; j++) { 17 | if (S[j] < S[i]) dp[i] = max(dp[i], dp[j] + 1); 18 | } 19 | ans = max(ans, dp[i]); 20 | } 21 | cout << ans; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /ex03m3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N, K; 10 | cin >> N >> K; 11 | 12 | vector p(N + 1); 13 | for (int i = 1; i <= N; i++) cin >> p[i]; 14 | 15 | vector dp(N + 1); 16 | for (int i = 1; i <= min(N, K + 1); i++) dp[i] = p[i]; 17 | for (int i = K + 2; i <= N; i++) { 18 | dp[i] = INF; 19 | for (int j = i; j >= max(1, i - 2 * K - 1); j--) { 20 | dp[i] = min(dp[i], dp[j] + p[i]); 21 | } 22 | } 23 | 24 | int ans = INF; 25 | for (int i = max(1, N - K); i <= N; i++) ans = min(ans, dp[i]); 26 | cout << ans; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /ex04e1_interval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector > intervals(N); 11 | for (auto &x : intervals) cin >> x.second; 12 | for (auto &x : intervals) cin >> x.first; 13 | 14 | sort(intervals.begin(), intervals.end()); 15 | 16 | int end_time = 0, ans = 0; 17 | for (auto [e, s] : intervals) { 18 | if (end_time <= s) { 19 | end_time = e; 20 | ans++; 21 | } 22 | } 23 | cout << ans; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /ex04e1_​interval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector s(N), e(N); 11 | for (int i = 0; i < N; i++) cin >> s[i]; 12 | for (int i = 0; i < N; i++) cin >> e[i]; 13 | 14 | vector idx(N); 15 | iota(idx.begin(), idx.end(), 0); 16 | sort(idx.begin(), idx.end(), [&](const int &a, const int &b) { 17 | return e[a] < e[b]; 18 | }); 19 | 20 | int ans = 0, cur = 0; 21 | for (int i = 0; i < N; i++) { 22 | if (cur <= s[idx[i]]) { 23 | cur = e[idx[i]]; 24 | ans++; 25 | } 26 | } 27 | cout << ans; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /ex04e2_fknapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | double W; 8 | int N; 9 | cin >> W >> N; 10 | 11 | vector w(N), v(N); 12 | vector idx(N); 13 | for (auto &x : v) cin >> x; 14 | for (auto &x : w) cin >> x; 15 | 16 | iota(idx.begin(), idx.end(), 0); 17 | sort(idx.begin(), idx.end(), [&](const int &x, const int &y) { 18 | return v[x] * w[y] > w[x] * v[y]; 19 | }); 20 | 21 | double ans = 0; 22 | for (int i = 0; i < N; i++) { 23 | if (W >= w[idx[i]]) { 24 | W -= w[idx[i]]; 25 | ans += v[idx[i]]; 26 | } 27 | else { 28 | ans += v[idx[i]] * W / w[idx[i]]; 29 | break; 30 | } 31 | } 32 | cout << fixed << setprecision(4) << ans; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /ex04e2_​fknapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | double W; 8 | int N; 9 | cin >> W >> N; 10 | 11 | vector v(N), w(N); 12 | for (int i = 0; i < N; i++) cin >> v[i]; 13 | for (int i = 0; i < N; i++) cin >> w[i]; 14 | 15 | vector idx(N); 16 | iota(idx.begin(), idx.end(), 0); 17 | sort(idx.begin(), idx.end(), [&](const int &a, const int &b) { 18 | return v[a] * w[b] > v[b] * w[a]; 19 | }); 20 | 21 | double ans = 0; 22 | for (int i = 0; i < N; i++) { 23 | if (w[idx[i]] <= W) { 24 | ans += v[idx[i]]; 25 | W -= w[idx[i]]; 26 | } 27 | else { 28 | ans += W * v[idx[i]] / w[idx[i]]; 29 | break; 30 | } 31 | } 32 | cout << fixed << setprecision(4) << ans; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /ex04e3_teamwork.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N, M; 8 | cin >> N >> M; 9 | 10 | vector a(M); 11 | for (int i = 0; i < M; i++) cin >> a[i]; 12 | 13 | sort(a.begin(), a.end()); 14 | 15 | priority_queue , greater > pq; 16 | for (int i = 0; i < N; i++) pq.push(0); 17 | 18 | double ans = 0; 19 | for (int i = 0; i < M; i++) { 20 | int u = pq.top(); 21 | pq.pop(); 22 | 23 | ans += u + a[i]; 24 | pq.push(u + a[i]); 25 | } 26 | cout << fixed << setprecision(3) << ans / M; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /ex04h1_montblanc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N, D; 10 | cin >> N >> D; 11 | 12 | vector s(N + 1); 13 | for (int i = 1; i <= N; i++) cin >> s[i]; 14 | 15 | int l = 0, r = INF, ans = 0, d = 0; 16 | while (l <= r) { 17 | int mid = (l + r) / 2; 18 | 19 | int cnt = 1; 20 | int st = 0; 21 | for (int i = 1; i <= N; i++) { 22 | if (s[i] - s[i - 1] > mid) { 23 | cnt = INF; 24 | break; 25 | } 26 | if (s[i] - st > mid) { 27 | st = s[i - 1]; 28 | cnt++; 29 | } 30 | } 31 | 32 | if (cnt <= D) r = mid - 1, ans = mid, d = cnt; 33 | else l = mid + 1; 34 | } 35 | cout << ans << ' ' << d; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /ex04m1_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector v, v2; 11 | for (int i = 0; i < N; i++) { 12 | int x; 13 | cin >> x; 14 | 15 | v.push_back(x); 16 | v2.push_back(x); 17 | } 18 | 19 | sort(v2.begin(), v2.end()); 20 | 21 | int cnt[4][4] = {}; 22 | for (int i = 0; i < N; i++) cnt[v2[i]][v[i]]++; 23 | 24 | int ans = 0, sum = 0; 25 | for (int i = 1; i <= 3; i++) { 26 | for (int j = i + 1; j <= 3; j++) { 27 | int t = min(cnt[i][j], cnt[j][i]); 28 | ans += t; 29 | sum += cnt[i][j] + cnt[j][i] - 2 * t; 30 | } 31 | } 32 | cout << ans + sum * 2 / 3; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /ex04m1_​sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int N; 8 | cin >> N; 9 | 10 | vector original(N), sorted(N); 11 | for (int i = 0; i < N; i++) { 12 | cin >> original[i]; 13 | sorted[i] = --original[i]; 14 | } 15 | 16 | sort(sorted.begin(), sorted.end()); 17 | 18 | int cnt[3][3] = {}; 19 | for (int i = 0; i < N; i++) cnt[sorted[i]][original[i]]++; 20 | 21 | int ans = 0, sum = 0; 22 | for (int i = 0; i < 3; i++) for (int j = i + 1; j < 3; j++) { 23 | int c = min(cnt[i][j], cnt[j][i]); 24 | ans += c; 25 | sum += cnt[i][j] + cnt[j][i] - 2 * c; 26 | } 27 | cout << ans + sum * 2 / 3; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /ex04m2_​salesman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | int M; 7 | int order[MAX_N], dist[MAX_N]; 8 | 9 | int cost(int a, int b) { 10 | if (a > b) swap(a, b); 11 | return min(dist[b - 1] - dist[a - 1], dist[M] - dist[b - 1] + dist[a - 1]); 12 | } 13 | 14 | int main() { 15 | cin.tie(nullptr)->sync_with_stdio(false); 16 | 17 | int N; 18 | cin >> N >> M; 19 | 20 | for (int i = 0; i < M; i++) { 21 | cin >> order[i]; 22 | order[i]++; 23 | } 24 | order[M] = order[0]; 25 | for (int i = 1; i <= M; i++) { 26 | cin >> dist[i]; 27 | dist[i] += dist[i - 1]; 28 | } 29 | 30 | for (int i = 0; i < N; i++) { 31 | int A, B; 32 | cin >> A >> B; 33 | 34 | A++, B++; 35 | int ans = 0; 36 | for (int i = 1; i <= M; i++) { 37 | ans += min({cost(order[i - 1], order[i]), 38 | cost(order[i - 1], A) + cost(order[i], B), 39 | cost(order[i - 1], B) + cost(order[i], A)}); 40 | } 41 | cout << ans << '\n'; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /ex05e1_shortest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 105; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | char table[MAX_N][MAX_N]; 8 | int dist[MAX_N][MAX_N]; 9 | 10 | int main() { 11 | cin.tie(nullptr)->sync_with_stdio(false); 12 | 13 | int R, C; 14 | cin >> R >> C; 15 | 16 | for (int i = 0; i < R; i++) cin >> table[i]; 17 | 18 | memset(dist, -1, sizeof(dist)); 19 | 20 | queue > q; 21 | if (table[0][0] == '.') { 22 | q.emplace(0, 0); 23 | dist[0][0] = 0; 24 | } 25 | while (!q.empty()) { 26 | auto [ux, uy] = q.front(); 27 | q.pop(); 28 | 29 | for (int i = 0; i < 4; i++) { 30 | int vx = ux + d[i][0], vy = uy + d[i][1]; 31 | if (vx < 0 or vy < 0 or vx >= R or vy >= C) continue; 32 | 33 | if (dist[vx][vy] == -1 and table[vx][vy] != '#') { 34 | dist[vx][vy] = dist[ux][uy] + 1; 35 | q.emplace(vx, vy); 36 | } 37 | } 38 | } 39 | cout << dist[R - 1][C - 1]; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /ex05e2_​planning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | vector graph[MAX_N]; 7 | int indegree[MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int N; 13 | cin >> N; 14 | 15 | for (int i = 0; i < N; i++) { 16 | int m; 17 | cin >> m; 18 | 19 | indegree[i] = m; 20 | while (m--) { 21 | int x; 22 | cin >> x; 23 | 24 | graph[x].push_back(i); 25 | } 26 | } 27 | 28 | queue q; 29 | for (int i = 0; i < N; i++) { 30 | if (indegree[i] == 0) q.push(i); 31 | } 32 | 33 | while (!q.empty()) { 34 | int u = q.front(); 35 | q.pop(); 36 | 37 | cout << u << ' '; 38 | for (auto v : graph[u]) { 39 | if (--indegree[v] == 0) q.push(v); 40 | } 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /ex05e3_catapult.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | vector graph[MAX_N]; 7 | bool reach[MAX_N][MAX_N]; 8 | bool visited[MAX_N]; 9 | int parent[MAX_N], sz[MAX_N]; 10 | 11 | int find_parent(int u) { 12 | if (u == parent[u]) return u; 13 | return parent[u] = find_parent(parent[u]); 14 | } 15 | 16 | void dfs(int u, int x) { 17 | if (visited[u] == true) return; 18 | visited[u] = true; 19 | reach[x][u] = true; 20 | for (auto &v : graph[u]) dfs(v, x); 21 | } 22 | 23 | int main() { 24 | cin.tie(nullptr)->sync_with_stdio(false); 25 | 26 | int N; 27 | cin >> N; 28 | 29 | for (int i = 0; i < N; i++) { 30 | int m; 31 | cin >> m; 32 | 33 | while (m--) { 34 | int x; 35 | cin >> x; 36 | 37 | graph[i].push_back(x); 38 | } 39 | } 40 | 41 | for (int i = 0; i < N; i++) { 42 | fill(visited, visited + N, false); 43 | dfs(i, i); 44 | } 45 | 46 | iota(parent, parent + N, 0); 47 | fill(sz, sz + N, 1); 48 | for (int i = 0; i < N; i++) { 49 | for (int j = i + 1; j < N; j++) { 50 | if (reach[i][j] == true and reach[j][i] == true) { 51 | int u = find_parent(i), v = find_parent(j); 52 | if (u != v) { 53 | parent[v] = u; 54 | sz[u] += sz[v]; 55 | } 56 | } 57 | } 58 | } 59 | 60 | vector ans; 61 | for (int i = 0; i < N; i++) { 62 | if (parent[i] == i) ans.push_back(sz[i]); 63 | } 64 | 65 | sort(ans.begin(), ans.end()); 66 | 67 | for (auto &v : ans) cout << v << ' '; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /ex05h1_​rank.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5005; 5 | 6 | int cnt_disc, cnt_scc; 7 | vector graph[MAX_N], graph2[MAX_N]; 8 | int disc[MAX_N], low[MAX_N], scc[MAX_N], sz[MAX_N]; 9 | bool on_stack[MAX_N]; 10 | stack nodes; 11 | int indegree[MAX_N], Rank[MAX_N]; 12 | map chk[MAX_N]; 13 | 14 | // This is out of scope from Algorithm Design, you don't need to understand what I did :) 15 | void tarjan(int u) { 16 | disc[u] = low[u] = ++cnt_disc; 17 | on_stack[u] = true; 18 | nodes.push(u); 19 | for (auto &v : graph[u]) { 20 | if (disc[v] == 0) { 21 | tarjan(v); 22 | low[u] = min(low[u], low[v]); 23 | } 24 | else if (on_stack[v] == true) low[u] = min(low[u], disc[v]); 25 | } 26 | if (disc[u] == low[u]) { 27 | int now; 28 | do { 29 | now = nodes.top(); 30 | nodes.pop(); 31 | 32 | scc[now] = cnt_scc; 33 | sz[cnt_scc]++; 34 | on_stack[now] = false; 35 | } while (u != now); 36 | cnt_scc++; 37 | } 38 | } 39 | 40 | int main() { 41 | cin.tie(nullptr)->sync_with_stdio(false); 42 | 43 | int N, P; 44 | cin >> N >> P; 45 | 46 | while (P--) { 47 | int a, b; 48 | cin >> a >> b; 49 | 50 | graph[a].push_back(b); 51 | } 52 | 53 | for (int i = 0; i < N; i++) { 54 | if (disc[i] == 0) tarjan(i); 55 | } 56 | for (int u = 0; u < N; u++) { 57 | for (auto &v : graph[u]) { 58 | int a = scc[u], b = scc[v]; 59 | if (a == b or chk[a].count(b)) continue; 60 | 61 | chk[a][b] = true; 62 | graph2[a].push_back(b); 63 | indegree[b]++; 64 | } 65 | } 66 | 67 | queue q; 68 | for (int i = 0; i < cnt_scc; i++) { 69 | if (indegree[i] == 0) { 70 | Rank[i] = 0; 71 | q.push(i); 72 | } 73 | } 74 | while (!q.empty()) { 75 | int u = q.front(); 76 | q.pop(); 77 | 78 | for (auto &v : graph2[u]) { 79 | if (--indegree[v] == 0) { 80 | Rank[v] = Rank[u] + 1; 81 | q.push(v); 82 | } 83 | } 84 | } 85 | 86 | map cnt_rank; 87 | for (int i = 0; i < cnt_scc; i++) cnt_rank[Rank[i]] += sz[i]; 88 | 89 | for (auto [r, c] : cnt_rank) cout << c << ' '; 90 | return 0; 91 | } -------------------------------------------------------------------------------- /ex05m1_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | vector graph[MAX_N]; 7 | bool visited[MAX_N]; 8 | 9 | bool dfs(int u, int p) { 10 | if (visited[u] == true) return true; 11 | visited[u] = true; 12 | 13 | for (auto v : graph[u]) { 14 | if (v == p) continue; 15 | 16 | if (dfs(v, u) == true) return true; 17 | } 18 | return false; 19 | } 20 | 21 | int main() { 22 | cin.tie(nullptr)->sync_with_stdio(false); 23 | 24 | int t; 25 | cin >> t; 26 | 27 | while (t--) { 28 | int n, m; 29 | cin >> n >> m; 30 | 31 | for (int i = 0; i < n; i++) { 32 | visited[i] = false; 33 | graph[i].clear(); 34 | } 35 | 36 | while (m--) { 37 | int a, b; 38 | cin >> a >> b; 39 | 40 | graph[a].push_back(b); 41 | graph[b].push_back(a); 42 | } 43 | 44 | bool ok = false; 45 | for (int i = 0; i < n; i++) { 46 | if (visited[i] == false and dfs(i, -1) == true) { 47 | ok = true; 48 | break; 49 | } 50 | } 51 | 52 | if (ok == true) cout << "YES\n"; 53 | else cout << "NO\n"; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /ex05m2_connection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | vector graph[MAX_N]; 7 | int dist[MAX_N]; 8 | 9 | int main() { 10 | cin.tie(nullptr)->sync_with_stdio(false); 11 | 12 | int n, m, k; 13 | cin >> n >> m >> k; 14 | 15 | while (m--) { 16 | int a, b; 17 | cin >> a >> b; 18 | 19 | graph[a].push_back(b); 20 | graph[b].push_back(a); 21 | } 22 | 23 | int ans = 0; 24 | for (int i = 0; i < n; i++) { 25 | memset(dist, -1, sizeof(dist)); 26 | 27 | int cnt = 1; 28 | queue q; 29 | q.push(i); 30 | dist[i] = 0; 31 | while (!q.empty()) { 32 | int u = q.front(); 33 | q.pop(); 34 | 35 | if (dist[u] == k) break; 36 | for (auto v : graph[u]) { 37 | if (dist[v] == -1) { 38 | dist[v] = dist[u] + 1; 39 | cnt++; 40 | q.push(v); 41 | } 42 | } 43 | } 44 | ans = max(ans, cnt); 45 | } 46 | cout << ans; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /ex06e1_cable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | int parent[MAX_N]; 7 | 8 | int find_parent(int u) { 9 | if (u == parent[u]) return u; 10 | return parent[u] = find_parent(parent[u]); 11 | } 12 | 13 | int main() { 14 | cin.tie(nullptr)->sync_with_stdio(false); 15 | 16 | int N; 17 | cin >> N; 18 | 19 | vector > edges; 20 | for (int i = 0; i < N - 1; i++) { 21 | for (int j = i + 1; j < N; j++) { 22 | int w; 23 | cin >> w; 24 | 25 | edges.emplace_back(w, i, j); 26 | } 27 | } 28 | 29 | sort(edges.begin(), edges.end()); 30 | 31 | int ans = 0; 32 | iota(parent, parent + N, 0); 33 | for (auto [w, a, b] : edges) { 34 | int u = find_parent(a), v = find_parent(b); 35 | if (u != v) { 36 | ans += w; 37 | parent[v] = u; 38 | } 39 | } 40 | cout << ans << '\n'; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /ex06e1_​cable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 1005; 5 | 6 | int parent[MAX_N]; 7 | 8 | int find_parent(int u) { 9 | if (u == parent[u]) return u; 10 | return parent[u] = find_parent(parent[u]); 11 | } 12 | 13 | int main() { 14 | cin.tie(nullptr)->sync_with_stdio(false); 15 | 16 | int N; 17 | cin >> N; 18 | 19 | vector > edges; 20 | for (int i = 0; i < N - 1; i++) { 21 | for (int j = i + 1; j < N; j++) { 22 | int x; 23 | cin >> x; 24 | 25 | edges.emplace_back(x, i, j); 26 | } 27 | } 28 | 29 | iota(parent, parent + N, 0); 30 | sort(edges.begin(), edges.end()); 31 | 32 | int ans = 0; 33 | for (auto [w, a, b] : edges) { 34 | int u = find_parent(a), v = find_parent(b); 35 | if (u != v) { 36 | ans += w; 37 | parent[v] = u; 38 | } 39 | } 40 | cout << ans; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /ex06e2_shortest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | 6 | int main() { 7 | cin.tie(nullptr)->sync_with_stdio(false); 8 | 9 | int N, M, s; 10 | cin >> N >> M >> s; 11 | 12 | vector > dist(N, vector (N, INF)); 13 | while (M--) { 14 | int a, b, c; 15 | cin >> a >> b >> c; 16 | 17 | dist[a][b] = c; 18 | } 19 | 20 | dist[s][s] = 0; 21 | for (int k = 0; k < N; k++) { 22 | for (int i = 0; i < N; i++) { 23 | for (int j = 0; j < N; j++) { 24 | dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); 25 | } 26 | } 27 | } 28 | 29 | bool ok = true; 30 | for (int i = 0; i < N; i++) if (dist[i][i] < 0) ok = false; 31 | 32 | if (ok == true) { 33 | for (int i = 0; i < N; i++) cout << dist[s][i] << ' '; 34 | } 35 | else cout << "-1"; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /ex06e3_columbia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 2e9 + 7; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | int main() { 8 | cin.tie(nullptr)->sync_with_stdio(false); 9 | 10 | int R, C; 11 | cin >> R >> C; 12 | 13 | vector > a(R + 5, vector (C + 5)); 14 | for (int i = 1; i <= R; i++) { 15 | for (int j = 1; j <= C; j++) { 16 | cin >> a[i][j]; 17 | } 18 | } 19 | 20 | vector > dist(R + 5, vector (C + 5, INF)); 21 | vector > visited(R + 5, vector (C + 5, false)); 22 | 23 | priority_queue > pq; 24 | pq.emplace(0, 1, 1); 25 | dist[1][1] = 0; 26 | while (!pq.empty()) { 27 | auto [di, ux, uy] = pq.top(); 28 | pq.pop(); 29 | 30 | if (visited[ux][uy] == true) continue; 31 | visited[ux][uy] = true; 32 | 33 | for (int i = 0; i < 4; i++) { 34 | int vx = ux + d[i][0], vy = uy + d[i][1]; 35 | if (vx < 1 or vx > R or vy < 1 or vy > C) continue; 36 | if (visited[vx][vy] == false and dist[ux][uy] + a[vx][vy] < dist[vx][vy]) { 37 | dist[vx][vy] = dist[ux][uy] + a[vx][vy]; 38 | pq.emplace(-dist[vx][vy], vx, vy); 39 | } 40 | } 41 | } 42 | 43 | for (int i = 1; i <= R; i++) { 44 | for (int j = 1; j <= C; j++) cout << dist[i][j] << ' '; 45 | cout << '\n'; 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /ex06h1_karin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long INF = 1e18 + 7; 5 | int d[5][2] = {{-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}}; 6 | 7 | int K; 8 | 9 | vector > multiply(vector > a, vector > b) { 10 | vector > c(K, vector (K, INF)); 11 | for (int i = 0; i < K; i++) { 12 | for (int j = 0; j < K; j++) { 13 | for (int k = 0; k < K; k++) { 14 | c[i][j] = min(c[i][j], a[i][k] + b[k][j]); 15 | } 16 | } 17 | } 18 | return c; 19 | } 20 | 21 | vector > expo(vector > a, int b) { 22 | if (b == 1) return a; 23 | 24 | vector > c = expo(a, b / 2); 25 | c = multiply(c, c); 26 | if (b % 2 == 1) c = multiply(c, a); 27 | return c; 28 | } 29 | 30 | int main() { 31 | cin.tie(nullptr)->sync_with_stdio(false); 32 | 33 | int N, P; 34 | cin >> N >> K >> P; 35 | 36 | vector > cost(K, vector (5)); 37 | for (int i = 0; i < K; i++) for (int j = 0; j < 5; j++) cin >> cost[i][j]; 38 | 39 | vector >> dp(K, vector > (K, vector (2, INF))); 40 | for (int i = 0; i < K; i++) { 41 | priority_queue > pq; 42 | pq.emplace(0, i, 0); 43 | dp[i][i][0] = 0; 44 | while (!pq.empty()) { 45 | auto [dist, k, n] = pq.top(); 46 | pq.pop(); 47 | 48 | for (int j = 0; j < 5; j++) { 49 | int nk = (k + d[j][0] + K) % K, nn = n + d[j][1]; 50 | if (nn < 2 and dp[i][k][n] + cost[k][j] < dp[i][nk][nn]) { 51 | dp[i][nk][nn] = dp[i][k][n] + cost[k][j]; 52 | pq.emplace(-dp[i][nk][nn], nk, nn); 53 | } 54 | } 55 | } 56 | } 57 | 58 | vector > mat(K, vector (K)); 59 | for (int i = 0; i < K; i++) for (int j = 0; j < K; j++) mat[i][j] = dp[i][j][1]; 60 | 61 | cout << expo(mat, N - 1)[0][P]; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /ex06m1_​massrelay.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 5005; 5 | 6 | int parent[MAX_N]; 7 | 8 | int find_parent(int u) { 9 | if (u == parent[u]) return u; 10 | return parent[u] = find_parent(parent[u]); 11 | } 12 | 13 | int main() { 14 | cin.tie(nullptr)->sync_with_stdio(false); 15 | 16 | int N, M, Q; 17 | cin >> N >> M >> Q; 18 | 19 | vector > edges; 20 | while (M--) { 21 | int a, b, c; 22 | cin >> a >> b >> c; 23 | 24 | edges.emplace_back(c, a, b); 25 | } 26 | 27 | iota(parent, parent + N, 0); 28 | sort(edges.begin(), edges.end()); 29 | 30 | int cnt = N; 31 | vector ans(N + 1); 32 | for (auto [c, a, b] : edges) { 33 | int u = find_parent(a), v = find_parent(b); 34 | if (u != v) { 35 | ans[--cnt] = c; 36 | parent[v] = u; 37 | } 38 | } 39 | 40 | while (Q--) { 41 | int D; 42 | cin >> D; 43 | 44 | cout << ans[D] << '\n'; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /ex06m2_exchange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cin.tie(nullptr)->sync_with_stdio(false); 6 | 7 | int T; 8 | cin >> T; 9 | 10 | while (T--) { 11 | int N; 12 | cin >> N; 13 | 14 | vector > R(N, vector (N, 0)); 15 | for (int i = 0; i < N; i++) { 16 | for (int j = 0; j < N; j++) { 17 | cin >> R[i][j]; 18 | } 19 | } 20 | 21 | for (int k = 0; k < N; k++) { 22 | for (int i = 0; i < N; i++) { 23 | for (int j = 0; j < N; j++) { 24 | R[i][j] = max(R[i][j], R[i][k] * R[k][j]); 25 | } 26 | } 27 | } 28 | 29 | bool ok = false; 30 | for (int i = 0; i < N; i++) { 31 | if (R[i][i] > 1.0) ok = true; 32 | } 33 | 34 | if (ok == true) cout << "YES\n"; 35 | else cout << "NO\n"; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /ex07e1_nqueen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, ans; 5 | bool col[15], d1[30], d2[50]; 6 | 7 | void solve(int r) { 8 | if (r == N) { 9 | ans++; 10 | return; 11 | } 12 | for (int c = 0; c < N; c++) { 13 | if (col[c] == false and d1[r + c] == false and d2[15 + r - c] == false) { 14 | col[c] = d1[r + c] = d2[15 + r - c] = true; 15 | solve(r + 1); 16 | col[c] = d1[r + c] = d2[15 + r - c] = false; 17 | } 18 | } 19 | } 20 | 21 | int main() { 22 | cin.tie(nullptr)->sync_with_stdio(false); 23 | 24 | cin >> N; 25 | solve(0); 26 | cout << ans; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /ex07e2_iss.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, ans; 5 | 6 | void solve(int i, int s) { 7 | if (s > N) return; 8 | if (s == N) { 9 | ans++; 10 | return; 11 | } 12 | for (int j = i; j <= N; j++) solve(j, s + j); 13 | } 14 | 15 | int main() { 16 | cin.tie(nullptr)->sync_with_stdio(false); 17 | 18 | cin >> N; 19 | 20 | for (int i = 1; i <= N; i++) solve(i, i); 21 | cout << ans; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /ex07e3_coloring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_N = 55; 5 | 6 | vector graph[MAX_N]; 7 | int color[MAX_N]; 8 | bool visited[MAX_N]; 9 | 10 | bool dfs(int u, int k) { 11 | visited[u] = true; 12 | vector has_color(k); 13 | for (auto v : graph[u]) { 14 | if (visited[v] == true) has_color[color[v]] = true; 15 | } 16 | 17 | for (int c = 0; c < k; c++) { 18 | if (has_color[c] == true) continue; 19 | 20 | color[u] = c; 21 | bool ok = true; 22 | for (auto v : graph[u]) { 23 | if (visited[v] == false and dfs(v, k) == false) { 24 | ok = false; 25 | break; 26 | } 27 | } 28 | if (ok == true) return true; 29 | } 30 | visited[u] = false; 31 | return false; 32 | } 33 | 34 | int main() { 35 | cin.tie(nullptr)->sync_with_stdio(false); 36 | 37 | int n, m; 38 | cin >> n >> m; 39 | 40 | while (m--) { 41 | int a, b; 42 | cin >> a >> b; 43 | 44 | graph[a].push_back(b); 45 | graph[b].push_back(a); 46 | } 47 | 48 | int l = 1, r = n, ans = -1; 49 | while (l <= r) { 50 | int mid = (l + r) / 2; 51 | 52 | bool ok = true; 53 | for (int i = 0; i < n; i++) color[i] = -1, visited[i] = false; 54 | for (int i = 0; i < n; i++) { 55 | if (visited[i] == false and dfs(i, mid) == false) { 56 | ok = false; 57 | break; 58 | } 59 | } 60 | 61 | if (ok == true) r = mid - 1, ans = mid; 62 | else l = mid + 1; 63 | } 64 | cout << ans; 65 | return 0; 66 | } -------------------------------------------------------------------------------- /ex07m1_​knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | float W, ans; 6 | float qsv[100]; 7 | 8 | struct Item { 9 | float v, w; 10 | 11 | bool operator<(const Item &o) const { 12 | return v * o.w > o.v * w; 13 | } 14 | }; 15 | Item a[100]; 16 | vector a_greedy[100]; 17 | 18 | void solve(int i, float weight, float value) { 19 | if (i == N) return void(ans = max(ans, value)); 20 | if (value + qsv[i] <= ans) return; 21 | 22 | float v_greedy = value, w_greedy = W - weight; 23 | for (auto &item : a_greedy[i]) { 24 | if (w_greedy >= item.w) { 25 | v_greedy += item.v; 26 | w_greedy -= item.w; 27 | } 28 | else { 29 | v_greedy += w_greedy * item.v / item.w; 30 | break; 31 | } 32 | } 33 | if (v_greedy <= ans) return; 34 | 35 | if (weight + a[i].w <= W) solve(i + 1, weight + a[i].w, value + a[i].v); 36 | solve(i + 1, weight, value); 37 | } 38 | 39 | int main() { 40 | cin.tie(nullptr)->sync_with_stdio(false); 41 | 42 | cin >> W >> N; 43 | 44 | for (int i = 0; i < N; i++) cin >> a[i].v; 45 | for (int i = 0; i < N; i++) cin >> a[i].w; 46 | 47 | sort(a, a + N); 48 | 49 | qsv[N - 1] = a[N - 1].v; 50 | for (int i = N - 2; i >= 0; i--) qsv[i] = qsv[i + 1] + a[i].v; 51 | for (int i = 0; i < N; i++) { 52 | for (int j = i; j < N; j++) a_greedy[i].push_back(a[j]); 53 | sort(a_greedy[i].begin(), a_greedy[i].end()); 54 | } 55 | 56 | solve(0, 0.0, 0.0); 57 | cout << fixed << setprecision(5) << ans; 58 | return 0; 59 | } -------------------------------------------------------------------------------- /ex07m2_15puzzle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INF = 1e9 + 7; 5 | const int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 6 | 7 | int main() { 8 | cin.tie(nullptr)->sync_with_stdio(false); 9 | 10 | vector > a(4, vector (4)); 11 | for (int i = 0; i < 4; i++) { 12 | for (int j = 0; j < 4; j++) { 13 | cin >> a[i][j]; 14 | } 15 | } 16 | 17 | int cnt = 0; 18 | for (int i = 0; i < 4; i++) { 19 | for (int j = 0; j < 4; j++) { 20 | if (a[i][j] == 0) continue; 21 | cnt += abs(i - (a[i][j] - 1) / 4) + abs(j - (a[i][j] - 1) % 4); 22 | } 23 | } 24 | if (cnt == 0) { 25 | cout << 0; 26 | return 0; 27 | } 28 | 29 | map >, int> dp; 30 | priority_queue >>> pq; 31 | pq.emplace(-cnt, a); 32 | dp[a] = 0; 33 | while (!pq.empty()) { 34 | auto [c, u] = pq.top(); 35 | pq.pop(); 36 | 37 | int tmp = dp[u]; 38 | for (int i = 0; i < 4; i++) { 39 | for (int j = 0; j < 4; j++) { 40 | if (u[i][j] != 0) continue; 41 | 42 | for (int k = 0; k < 4; k++) { 43 | int ni = i + d[k][0], nj = j + d[k][1]; 44 | if (ni < 0 or ni >= 4 or nj < 0 or nj >= 4) continue; 45 | 46 | swap(u[i][j], u[ni][nj]); 47 | if (!dp.count(u)) { 48 | dp[u] = tmp + 1; 49 | int ncnt = 0; 50 | for (int x = 0; x < 4; x++) { 51 | for (int y = 0; y < 4; y++) { 52 | if (u[x][y] == 0) continue; 53 | ncnt += abs(x - (u[x][y] - 1) / 4) + abs(y - (u[x][y] - 1) % 4); 54 | } 55 | } 56 | if (ncnt == 0) { 57 | cout << dp[u]; 58 | return 0; 59 | } 60 | pq.emplace(-dp[u] - ncnt, u); 61 | } 62 | swap(u[i][j], u[ni][nj]); 63 | } 64 | } 65 | } 66 | } 67 | return 0; 68 | } -------------------------------------------------------------------------------- /midterm64.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JomnoiZ/AlgorithmDesign/f492f02cfa6869710c51744e7f0a8f17daef1ea2/midterm64.pdf -------------------------------------------------------------------------------- /midterm65.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JomnoiZ/AlgorithmDesign/f492f02cfa6869710c51744e7f0a8f17daef1ea2/midterm65.pdf --------------------------------------------------------------------------------