版本比较
题解:P17077 [ICPC 2017 Shenyang R] Wandering Robots
正文
增加 39 行删除 45 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565714 行未改动7273747576777879808182834 行未改动888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132~~这题的数据太水了吧,如果代码错了请下掉我的题解。~~:::error[Fine_Dust_Z:]{open}我们虽然不会证明,~~那就留给其他题解证明吧~~,但是我们会观察法:发现这个答案就是可以达到的符合条件的格子比上所有可以到达的格子。:::`Fine_Dust_Z` 这个一看就没学过数学。我们知道时间接近无限时,机器人到达每个点的概率都相等。why???题目告诉我们:$$P_{x \to y} = \frac{1}{d_x+1}$$那么我们就有:$$\pi_x \times P_{x \to y} = (d_x + 1) \times \frac{1}{d_x + 1} = 1$$$$\pi_y \times P_{y \to x} = (d_y + 1) \times \frac{1}{d_y + 1} = 1$$因此:$$\pi_x = d_x + 1$$所以:$$\pi_x \times P_{x \to y} = \pi_y \times P_{y \to x}$$。那么我们直接考虑计算符合条件的节点的入读不就行了,~~又有平面图转对偶图,几个 trick 拼一块做起来真是让人回味无穷啊~~。每两个点之间建双向边,因为一个点可能会到达它本身所以有自环,那么入读之和呢?符合条件的点的入读之和呢?~~太难了😭我们跑路吧。~~四个角的入读为 $3$,除去角的边上的点入度为 $4$,其他的点入读为 $5$,定义总入读为 $ans$,则有:$$ans = 5 \times (n - 2)^2 + 4 \times 4(n - 2) + 3 \times 4$$$$ans = 5n^2 - 20n + 20 + 16n - 32 + 12$$$$ans = 5n^2 - 4n$$太好了解决一个,那么我们定义合法节点的入读之和为 $ans2$,那么入读为 $3$ 的节点有 $3$ 个,入度为 $4$ 的节点有 $2 \times (n - 2)$ 个,剩余的就是入读为 $5$ 的节点,那么就有:$$ans2 = 5 \times (\frac{n \times (n + 1)}{2} - 2 \times (n - 2) - 3) + 4 \times (2 \times (n - 2)) + 3 \times 3$$$$ans2 = \frac{5}{2}n^2 + \frac{5}{2}n - 10n + 20 - 15+ 8n - 16 + 9$$$$ans2 = \frac{5}{2}n^2 + \frac{1}{2}n - 2$$你这也写不到代码里啊,我们再推:$$ans2 = \frac{n(n+1)}{2} + 2n^2 - 2$$完美😋。放障碍其实就是删点。我们考虑处理删点:首先我们定义 $x$ 为当前节点的入读,所以一定有 $ans - x \to ans$,如果当前节点是合法节点就有 $ans2 - x \to ans2$。接下来考虑间接影响:如果相邻节点也被删了就不用管(因为在那个节点被删的时候直接把影响的入读减了),没有被删就有 $ans - 1 \to ans$,如果那个节点是一个合法节点那么就有 $ans2 - 1 \to ans2$。:::info[如何判断相邻节点被占用]1. 直接离散化或上 `std::unordered_map`,时间复杂度 $O(1)$。2. 用 `std::set`,因为我的写法套了个 `std::pair`,时间复杂度 $O(\log K)$。:::最后我们的答案就是:$$ans2 \div \gcd(ans,ans2) / ans \div \gcd(ans,ans2)$$时间复杂度 $O(t \cdot K \log K) 或 O(t \cdot K)$。:::success[code]```cpp#include <bits/stdc++.h>#define int long long14 行未改动int x, y;cin>>x>>y;st.insert({x, y});}int ans2 = 5 * n * n - 4 * n;int ans = 0;ans = (n * (n + 1)) / 2 + 2 * n * n - 2;for (auto& p : st) {int x = p.first;int y = p.second;4 行未改动if (nx >= 0 && nx < n && ny >= 0 && ny < n) {now++;}}ans2 -= now;if (x + y >= n - 1) {ans -= now;}for (int i = 0; i < 4; i++) {int nx = x + dx[i], ny = y + dy[i];if (nx >= 0 && nx < n && ny >= 0 && ny < n) {if (st.find({nx, ny}) == st.end()) {ans2 -= 1;if (nx + ny >= n - 1) {ans -= 1;}}}}}if (ans == 0) {cout << "Case #" << cnt << ": 0/1" << "\n";} else {int g = gcd(ans, ans2);cout << "Case #" << cnt << ": " << ans / g << "/" << ans2 / g << "\n";}}signed main() {cin.tie(nullptr) -> ios::sync_with_stdio(false);int t;cin>>t;for(int i = 1; i <= t; i++) {solve(i);}return 0;}```:::后记:注意到这个人代码和题解的 `ans` 和 `ans2` 写反了。12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565714 行未改动727374757677787980814 行未改动8687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`202/7/8`:变量条件定义英文变量、逻辑条件使用集合符号。~~这题的数据太水了吧,如果代码错了请下掉我的题解。~~:::error[Fine_Dust_Z:]{open}我们虽然不会证明,~~那就留给其他题解证明吧~~,但是我们会观察法:发现这个答案就是可以达到的符合条件的格子比上所有可以到达的格子。:::`Fine_Dust_Z` 这个一看就没学过数学。我们知道时间接近无限时且没有障碍物,机器人到达每个点的概率都相等。why???我们定义未被删除的所有节点集合为 $V$,定义符合条件的合法节点集合为:$$V_{true} = \{ (i, j) \in V \mid i + j \ge n - 1 \}$$题目告诉我们当且仅当 $x = y$ 或 $x$ 与 $y$ 相邻时:$$P_{x \to y} = \frac{1}{d_x+1}$$那么我们猜测平稳分布满足:$$\pi_x = d_x + 1$$因此:$$\pi_x \times P_{x \to y} = (d_x + 1) \times \frac{1}{d_x + 1} = 1$$$$\pi_y \times P_{y \to x} = (d_y + 1) \times \frac{1}{d_y + 1} = 1$$所以:$$\pi_x \times P_{x \to y} = \pi_y \times P_{y \to x}$$所以有答案:$$\frac{\sum_{(i,j) \in V_{true}} \pi_{i,j}}{\sum_{(i,j) \in V} \pi_{i,j}}$$。但是要注意啊我 $\pi_x$ 的推到过程中 $x$ 是坐标,只是写起来太繁琐而且不容易理解所以直接写的 $x$。那么我们直接考虑计算符合条件的节点的入度不就行了,~~又有平面图转对偶图,几个 trick 拼一块做起来真是让人回味无穷啊~~。每两个点之间建双向边,因为一个点可能会到达它本身所以有自环,那么度数之和呢?符合条件的点的度数之和呢?~~太难了😭我们跑路吧。~~四个角的入度为 $3$,除去角的边上的点入度为 $4$,其他的点入度为 $5$,定义总度数为 $S_{sum}$,则有:$$S_{sum} = 5 \times (n - 2)^2 + 4 \times 4(n - 2) + 3 \times 4$$$$S_{sum} = 5n^2 - 20n + 20 + 16n - 32 + 12$$$$S_{sum} = 5n^2 - 4n$$太好了解决一个,那么我们定义合法节点的度数之和为 $S_{true}$,那么入度为 $3$ 的节点有 $3$ 个,入度为 $4$ 的节点有 $2 \times (n - 2)$ 个,剩余的就是入度为 $5$ 的节点,那么就有:$$S_{true} = 5 \times (\frac{n \times (n + 1)}{2} - 2 \times (n - 2) - 3) + 4 \times (2 \times (n - 2)) + 3 \times 3$$$$S_{true} = \frac{5}{2}n^2 + \frac{5}{2}n - 10n + 20 - 15+ 8n - 16 + 9$$$$S_{true} = \frac{5}{2}n^2 + \frac{1}{2}n - 2$$你这也写不到代码里啊,我们再推:$$S_{true} = \frac{n(n+1)}{2} + 2n^2 - 2$$完美😋。放障碍其实就是删点。我们考虑处理删点:首先我们定义 $deg$ 为当前节点的入度,所以一定有 $S_{sum} - deg \to S_{sum}$,如果当前节点满足 $(x, y) \in V_{true}$ 就有 $S_{true} - deg \to S_{true}$。接下来考虑间接影响:如果相邻节点也被删了就不用管(因为在那个节点被删的时候直接把影响的入度减了),没有被删就有 $S_{sum} - 1 \to S_{sum}$,如果那个邻居节点满足 $(nx, ny) \in V_{true}$ 那么就有 $S_{true} - 1 \to S_{true}$。:::info[如何判断相邻节点被占用]直接离散化或上 `std::unordered_map`,时间复杂度 $O(1)$。用 `std::set`,因为我的写法套了个 `std::pair`,时间复杂度 $O(\log K)$。:::最后我们的答案就是:$$\frac{S_{true}}{\gcd(S_{sum},S_{true})} / \frac{S_{sum}}{\gcd(S_{sum},S_{true})}$$,时间复杂度 $O(t \cdot K \log K)$ 或 $O(t \cdot K)$。:::success[code]```cpp#include <bits/stdc++.h>#define int long long14 行未改动int x, y;cin>>x>>y;st.insert({x, y});}int S_sum = 5 * n * n - 4 * n;int S_true = (n * (n + 1)) / 2 + 2 * n * n - 2;for (auto& p : st) {int x = p.first;int y = p.second;4 行未改动if (nx >= 0 && nx < n && ny >= 0 && ny < n) {now++;}}S_sum -= now;if (x + y >= n - 1) {S_true -= now;}for (int i = 0; i < 4; i++) {int nx = x + dx[i], ny = y + dy[i];if (nx >= 0 && nx < n && ny >= 0 && ny < n) {if (st.find({nx, ny}) == st.end()) {S_sum -= 1;if (nx + ny >= n - 1) {S_true -= 1;}}}}}if (S_sum == 0) {cout << "Case #" << cnt << ": 0/1" << "\n";} else {int g = gcd(S_true, S_sum);cout << "Case #" << cnt << ": " << S_true / g << "/" << S_sum / g << "\n";}}signed main() {cin.tie(nullptr) -> ios::sync_with_stdio(false);int t;cin>>t;for(int i = 1; i <= t; i++) {solve(i);}return 0;}```:::