第二次ak,纪念一下。

比赛链接:https://atcoder.jp/contests/abc183/tasks

A - ReLU

题解

模拟。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int x;
  7. cin >> x;
  8. cout << (x >= 0 ? x : 0) << "\n";
  9. return 0;
  10. }

B - Billiards

题解

过两点向 \(x\) 轴作垂线,由两个直角三角形相似得:

\[\frac{x - sx}{gx - x} = \frac{sy}{gy}
\]

移项展开得:

\[(gy + sy) \times x = sy \times gx + sx \times gy
\]

即:

\[x = \frac{sy \times gx + sx \times gy}{gy + sy}
\]

Tips

要求误差小于 \(10^{-6}\) ,所以至少要输出小数点后 \(6\) 位。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. cout << fixed << setprecision(6);
  7. double sx, sy, gx, gy;
  8. cin >> sx >> sy >> gx >> gy;
  9. cout << (sy * gx + sx * gy) / (sy + gy) << "\n";
  10. return 0;
  11. }

C - Travel

题解

枚举所有情况即可。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int n, k;
  7. cin >> n >> k;
  8. vector<vector<int>> a(n, vector<int>(n));
  9. for (auto &v : a)
  10. for (auto &x : v) cin >> x;
  11. int ans = 0;
  12. vector<int> p(n);
  13. iota(p.begin(), p.end(), 0);
  14. do {
  15. if (p[0] != 0) continue;
  16. int sum = a[p[n - 1]][p[0]];
  17. for (int i = 1; i < n; i++) sum += a[p[i - 1]][p[i]];
  18. if (sum == k) ++ans;
  19. } while (next_permutation(p.begin(), p.end()));
  20. cout << ans << "\n";
  21. return 0;
  22. }

D - Water Heater

题解

差分。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. constexpr int N = 2e5 + 10;
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7. int n, w;
  8. cin >> n >> w;
  9. vector<long long> cnt(N);
  10. for (int i = 0; i < n; i++) {
  11. int s, t, p;
  12. cin >> s >> t >> p;
  13. cnt[s] += p;
  14. cnt[t] -= p;
  15. }
  16. bool ok = cnt[0] <= w;
  17. for (int i = 1; i < N; i++) {
  18. cnt[i] += cnt[i - 1];
  19. if (cnt[i] > w) ok = false;
  20. }
  21. cout << (ok ? "Yes" : "No") << "\n";
  22. return 0;
  23. }

E - Queen on Grid

题解

模拟做法:对于每个不为 '#' 的点,将水平、垂直、对角线上可达的点都加上走到当前点的方案数

  1. for (int x = i + 1; x <= h and MP[x][j] == '.'; x++) {
  2. dp[x][j] += dp[i][j];
  3. }
  4. for (int y = j + 1; y <= w and MP[i][y] == '.'; y++) {
  5. dp[i][y] += dp[i][j];
  6. }
  7. for (int x = i + 1, y = j + 1; x <= h and y <= w and MP[x][y] == '.'; x++, y++) {
  8. dp[x][y] += dp[i][j];
  9. }

为了避免超时可以分别将三个方向用差分维护。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. constexpr int N = 2010;
  4. constexpr int MOD = 1e9 + 7;
  5. char MP[N][N];
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9. int h, w;
  10. cin >> h >> w;
  11. for (int i = 1; i <= h; i++) {
  12. for (int j = 1; j <= w; j++) {
  13. cin >> MP[i][j];
  14. }
  15. }
  16. vector<vector<long long>> dp(N, vector<long long>(N));
  17. vector<vector<long long>> row(N, vector<long long>(N));
  18. vector<vector<long long>> col(N, vector<long long>(N));
  19. vector<vector<long long>> diag(N, vector<long long>(N));
  20. dp[1][1] = 1;
  21. for (int i = 1; i <= h; i++) {
  22. for (int j = 1; j <= w; j++) {
  23. if (MP[i][j] == '#') continue;
  24. (row[i][j] += row[i - 1][j]) %= MOD;
  25. (col[i][j] += col[i][j - 1]) %= MOD;
  26. (diag[i][j] += diag[i - 1][j - 1]) %= MOD;
  27. (dp[i][j] += row[i][j] + col[i][j] + diag[i][j]) %= MOD;
  28. if (MP[i + 1][j] == '.') row[i + 1][j] += dp[i][j];
  29. if (MP[i][j + 1] == '.') col[i][j + 1] += dp[i][j];
  30. if (MP[i + 1][j + 1] == '.') diag[i + 1][j + 1] += dp[i][j];
  31. }
  32. }
  33. cout << dp[h][w] << "\n";
  34. return 0;
  35. }

F - Confluence

题解

并查集+启发式合并。

Tips

  • 为了避免超时需要始终用大堆合并小堆,最坏时间复杂度为 \(O_{((\frac{n}{2} + \frac{n}{4} + \frac{n}{8} + \dots )log_n)}\) ,用小堆合并大堆复杂度可能达到 \(O_{(n^2log_n)}\) 。
  • map<int, int> mp[N] 快于 map<int, map<int, int>> mp

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. constexpr int N = 2e5 + 100;
  4. int n, q;
  5. int fa[N], clas[N];
  6. map<int, int> son_num[N];
  7. int Find(int x) {
  8. return fa[x] == x ? fa[x] : fa[x] = Find(fa[x]);
  9. }
  10. void Union(int x, int y) {
  11. x = Find(x);
  12. y = Find(y);
  13. if (x != y) {
  14. if (son_num[x].size() < son_num[y].size()) swap(x, y);
  15. fa[y] = x;
  16. for (const auto &[_class, num] : son_num[y]) {
  17. son_num[x][_class] += num;
  18. }
  19. }
  20. }
  21. void Init() {
  22. for (int i = 0; i < N; i++) {
  23. fa[i] = i;
  24. }
  25. }
  26. int main() {
  27. ios::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29. Init();
  30. cin >> n >> q;
  31. for (int i = 1; i <= n; i++) {
  32. cin >> clas[i];
  33. son_num[i][clas[i]] = 1;
  34. }
  35. for (int i = 0; i < q; i++) {
  36. int op, x, y;
  37. cin >> op >> x >> y;
  38. if (op == 1) {
  39. Union(x, y);
  40. } else {
  41. cout << son_num[Find(x)][y] << "\n";
  42. }
  43. }
  44. return 0;
  45. }

AtCoder Beginner Contest 183的更多相关文章

  1. AtCoder Beginner Contest 183 E - Queen on Grid (DP)

    题意:有一个\(n\)x\(m\)的棋盘,你需要从\((1,1)\)走到\((n,m)\),每次可以向右,右下,下走任意个单位,\(.\)表示可以走,#表示一堵墙,不能通过,问从\((1,1)\)走\ ...

  2. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  3. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  4. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  7. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  8. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  9. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

随机推荐

  1. springboot源码解析-管中窥豹系列之Initializer(四)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  2. Token验证的流程及如何准确的判断一个数据的类型

    Token验证的流程: 1,客户端使用用户名跟密码请求登录:2,服务端收到请求,去验证用户名与密码:3,验证成功后,服务端会签发一个 Token,再把这个 Token 发送给客户端:4,客户端收到 T ...

  3. 【MySQL 高级】知识拓展

    MySQL高级 知识拓展 MySQL高级 知识拓展 数据量 和 B+树 的关系 事务隔离级别集底层原理MVCC 唯一索引和普通索引的关键不同点 MRR:multi range read 练习和总结

  4. 9. 细节见真章,Formatter注册中心的设计很讨巧

    目录 本文提纲 版本约定 你好,我是A哥(YourBatman). Spring设计了org.springframework.format.Formatter格式化器接口抽象,对格式化器进行了大一统, ...

  5. Linux 入门教程:00 Background

    Linux 为何物? 就是一个操作系统. Linux 历史: 操作系统始于二十世纪五十年代,当时的操作系统能运行批处理程序.批处理程序不需要用户的交互,它从文件或者穿孔卡片读取数据,然后输出到另外一个 ...

  6. 创建mysql帐户

    CREATE USER 'username'@'host' IDENTIFIED BY 'password';

  7. 以我的亲身经历,聊聊学python的流程,同时推荐学python的书

    因为干活要用到,所以我大概于19年5月开始学python,大概学了1个月后,我就能干公司的活了,而且这python项目还包含了机器学习等要素,大概3个月后,我还承担了项目里开发机器学习数据分析的任务. ...

  8. LuoguP5488 差分与前缀和

    题意 给定一个长为\(n\)的序列\(a\),求出其\(k\)阶差分或前缀和.结果的每一项都需要对\(1004535809\)取模. 打表找规律 先看前缀和,设\(n=5\),\(k=4\),按照阶从 ...

  9. springAOP的概述及使用

    Spring AOP SpringAOP是Spring中非常重要的功能模块之一,该模块提供了面向切面编程,在事务处理,日志记录,安全控制等操作中广泛使用. SpringAOP的基本概念 AOP的概念 ...

  10. 面试常问的ArrayQueue底层实现

    public class ArrayQueue<T> extends AbstractList<T>{ //定义必要的属性,容量.数组.头指针.尾指针 private int ...