这题的数据太水了吧,如果代码错了请下掉我的题解。
Fine_Dust_Z:
我们虽然不会证明,那就留给其他题解证明吧,但是我们会观察法:发现这个答案就是可以达到的符合条件的格子比上所有可以到达的格子。
Fine_Dust_Z 这个一看就没学过数学。
我们知道时间接近无限时,机器人到达每个点的概率都相等。
why???题目告诉我们:
那么我们就有:
因此:
所以:
。
那么我们直接考虑计算符合条件的节点的入读不就行了,又有平面图转对偶图,几个 trick 拼一块做起来真是让人回味无穷啊。
每两个点之间建双向边,因为一个点可能会到达它本身所以有自环,那么入读之和呢?符合条件的点的入读之和呢?太难了😭我们跑路吧。
四个角的入读为 ,除去角的边上的点入度为 ,其他的点入读为 ,定义总入读为 ,则有:
太好了解决一个,那么我们定义合法节点的入读之和为 ,那么入读为 的节点有 个,入度为 的节点有 个,剩余的就是入读为 的节点,那么就有:
你这也写不到代码里啊,我们再推:
完美😋。
放障碍其实就是删点。
我们考虑处理删点:首先我们定义 为当前节点的入读,所以一定有 ,如果当前节点是合法节点就有 。
接下来考虑间接影响:如果相邻节点也被删了就不用管(因为在那个节点被删的时候直接把影响的入读减了),没有被删就有 ,如果那个节点是一个合法节点那么就有 。
如何判断相邻节点被占用
- 直接离散化或上
std::unordered_map,时间复杂度 。 - 用
std::set,因为我的写法套了个std::pair,时间复杂度 。
最后我们的答案就是:
时间复杂度 。
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
inline void solve(int cnt) {
int n, k;
cin>>n>>k;
set<pair<int, int>> st;
for(int i = 1; i <= k; i++){
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;
int now = 1;
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) {
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 写反了。