第二次ak,纪念一下。

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

A - ReLU

题解

模拟。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x;
cin >> x;
cout << (x >= 0 ? x : 0) << "\n";
return 0;
}

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\) 位。

代码

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

C - Travel

题解

枚举所有情况即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<vector<int>> a(n, vector<int>(n));
for (auto &v : a)
for (auto &x : v) cin >> x;
int ans = 0;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
do {
if (p[0] != 0) continue;
int sum = a[p[n - 1]][p[0]];
for (int i = 1; i < n; i++) sum += a[p[i - 1]][p[i]];
if (sum == k) ++ans;
} while (next_permutation(p.begin(), p.end()));
cout << ans << "\n";
return 0;
}

D - Water Heater

题解

差分。

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2e5 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, w;
cin >> n >> w;
vector<long long> cnt(N);
for (int i = 0; i < n; i++) {
int s, t, p;
cin >> s >> t >> p;
cnt[s] += p;
cnt[t] -= p;
}
bool ok = cnt[0] <= w;
for (int i = 1; i < N; i++) {
cnt[i] += cnt[i - 1];
if (cnt[i] > w) ok = false;
}
cout << (ok ? "Yes" : "No") << "\n";
return 0;
}

E - Queen on Grid

题解

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

for (int x = i + 1; x <= h and MP[x][j] == '.'; x++) {
dp[x][j] += dp[i][j];
}
for (int y = j + 1; y <= w and MP[i][y] == '.'; y++) {
dp[i][y] += dp[i][j];
}
for (int x = i + 1, y = j + 1; x <= h and y <= w and MP[x][y] == '.'; x++, y++) {
dp[x][y] += dp[i][j];
}

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

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2010;
constexpr int MOD = 1e9 + 7; char MP[N][N]; int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w;
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> MP[i][j];
}
}
vector<vector<long long>> dp(N, vector<long long>(N));
vector<vector<long long>> row(N, vector<long long>(N));
vector<vector<long long>> col(N, vector<long long>(N));
vector<vector<long long>> diag(N, vector<long long>(N));
dp[1][1] = 1;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (MP[i][j] == '#') continue;
(row[i][j] += row[i - 1][j]) %= MOD;
(col[i][j] += col[i][j - 1]) %= MOD;
(diag[i][j] += diag[i - 1][j - 1]) %= MOD;
(dp[i][j] += row[i][j] + col[i][j] + diag[i][j]) %= MOD;
if (MP[i + 1][j] == '.') row[i + 1][j] += dp[i][j];
if (MP[i][j + 1] == '.') col[i][j + 1] += dp[i][j];
if (MP[i + 1][j + 1] == '.') diag[i + 1][j + 1] += dp[i][j];
}
}
cout << dp[h][w] << "\n";
return 0;
}

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

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2e5 + 100; int n, q;
int fa[N], clas[N];
map<int, int> son_num[N]; int Find(int x) {
return fa[x] == x ? fa[x] : fa[x] = Find(fa[x]);
} void Union(int x, int y) {
x = Find(x);
y = Find(y);
if (x != y) {
if (son_num[x].size() < son_num[y].size()) swap(x, y);
fa[y] = x;
for (const auto &[_class, num] : son_num[y]) {
son_num[x][_class] += num;
}
}
} void Init() {
for (int i = 0; i < N; i++) {
fa[i] = i;
}
} int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Init();
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> clas[i];
son_num[i][clas[i]] = 1;
}
for (int i = 0; i < q; i++) {
int op, x, y;
cin >> op >> x >> y;
if (op == 1) {
Union(x, y);
} else {
cout << son_num[Find(x)][y] << "\n";
}
}
return 0;
}

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源码解析-管中窥豹系列之排序(五)

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

  2. Docusaurus2 快速建站,发布 GitHub Pages

    Docusaurus2 可快速搭建文档.博客.官网等网站,并发布到 GitHub Pages, Serverless 等. 我们只需 Markdown 写写内容就行,也可直接编写 React 组件嵌入 ...

  3. LeetCode141-环形链表检测

    题目 给定一个链表,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置 ...

  4. 【Java】面向对象 - 封装

    继承 封装 多态 重新搞一波 复习巩固 简单记录 慕课网 imooc Java 零基础入门-Java面向对象-Java封装 封装 封装是什么? 将类的某些信息隐藏在类内部,不允许外部程序直接访问 通过 ...

  5. Oracle 10g 如何调整 sga_max_size 与 sga_target

    sga_max_size是相对于操作系统来讲的,当启动oracle时,一次性分配给oracle实例的sga不会超过sga_max_size值:而sga_target是相对于oracle这个正在运行的应 ...

  6. windows_myql 安装与卸载详细讲解,

    windows_myql 安装 注意: 安装前把 所有杀毒软件,安全卫士等关闭. 打开下载的mysql安装文件双击解压缩,运行"mysql-5.5.40-win64.msi". 注 ...

  7. uni-app开发经验分享十一: uniapp iOS云打包修改权限提示语

    打包提交appstore如果用到了如下权限需要修改提示语,详细描述使用这个权限的原因,如不修改提示语appstore审核可能会被拒绝.Apple的原则是,如果一个app想要申请用户同意某个隐私信息访问 ...

  8. Python編碼格式錯誤解決方案及案例

    Python格式錯誤解決方案及案例 這幾天在玩爬蟲,在解析和提取内容時經常出現由於内容格式問題導致出錯,為防止以後出錯,整下一下,以下是這幾天的總結: 1. 特殊符號或表情符號等 背景:爬取一個烹飪教 ...

  9. Python赋值、浅复制和深复制

    Python赋值.浅复制和深复制 ​ 首先我们需要知道赋值和浅复制的区别: 赋值和浅复制的区别 赋值,当一个对象赋值给另一个新的变量时,赋的其实是该对象在栈中的地址,该地址指向堆中的数据.即赋值后,两 ...

  10. mysql的安装使用及其用户管理

    mysql的安装使用及其用户管理   一.mariadb安装 搭建yum源 [mariadb] name = MariaDB baseurl = http://mirrors.ustc.edu.cn/ ...