跳到正文

题解

题解:P11454 [USACO24DEC] 2D Conveyer Belt S

XGTD约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年12月31日
收录
2026年9月8日
更新
2026年11月11日
篇幅
约 6 分钟
获赞
3
题目
P11454

版本

  1. v1当前

P11454 [USACO24DEC] 2D Conveyer Belt S 题解

Preface

好题呀!赛时非常难得的正解完全想出来了,但是犯了一个实现错误,最后总分差七十分痛失金组。

思路主要就是逆向思维加深搜。

Problem Statement

p11454

Solution

首先发现一个点能用当且仅当他能到达一个能用点。所有边界以外的点都能用。

然后发现如果像题目说的一样加限制非常不好搞,每次会影响非常多的点,必须重新深搜一遍,复杂度 O(Qn2)O(Qn^2),不行。

于是考虑倒序,先搜出所有限制都加上后哪些点能用。此时因为每次是减去限制发现每个点影响的位置是固定的而且较少,且每去掉一条边只有两种情况:

  1. 这个位置去掉这条限制之前就能用。发现根本不用管,因为首先对于他自己答案肯定不变,而它除了自己外只能影响能到达它的点,这些点的答案显然也不变。
  2. 这个点本来用不了,此时又有两种可能:
    • 去掉限制使得这个点能用了。这种情况发生的前提是这个点四周的点至少有一个能用,因为现在它可以去到相邻的任意点了,任何一个能用它就能用。如果是这样,因为此点的答案变了,我们需要遍历所有能到它的点 xx,如果 xx 本来不行,改成行,本来就行,显然就可以退出不用再改能到 xx 的点了。
    • 去掉限制它还是用不了,显然不用管,把限制去掉就行了不用改答案。

这样下来由于一个点只会被改为能用一次,均摊复杂度 O(n2)O(n^2) 可以通过。

Code

C++
/*
was just one step away from gold!
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
int dx[] = {0, 0, 0, 1, -1}, dy[] = {0, 1, -1, 0, 0};
//L = 2, R = 1, U = 4, D = 3
int n, Q, a[2005][2005], use[2005][2005], ans[500006], now;
pair<int, int> qu[500005];
//the direction of each belt, and wheter one is useable
//ans is answer for qth day
int v[2005][2005];
void dfs(int x, int y){
	use[x][y] = 1;
	//only when use was 0 will we visit this x, y
	ans[now]++;
	//another grid freed
	for(int i = 1; i <= 4; i++){
		int oppo;
		if(i == 1)oppo = 2;
		if(i == 2)oppo = 1;
		if(i == 4)oppo = 3;
		if(i == 3)oppo = 4;
		int nx = x + dx[i], ny = y + dy[i];
		//new x and new y		
		if(a[nx][ny] != 0 && a[nx][ny] != oppo){
			continue;
			//this one can't get to x, y
		}
		if(use[nx][ny])continue;
		if(nx > n || ny > n || nx < 1 || ny < 1)continue;
		//out of grid
		dfs(nx, ny);
	}
}
signed main(){
	std::ios::sync_with_stdio(0);
	cin>>n>>Q;
	for(int i = 1; i <= Q; i++){
		int x, y;
		char z;
		cin>>x>>y>>z;
		qu[i].first = x;
		qu[i].second = y;
		if(z == 'L'){
			a[x][y] = 2;
			//row stay same, colume minus 1
		}
		if(z == 'R'){
			a[x][y] = 1;
			//row stay, colume plus 1
		}
		if(z == 'U'){
			a[x][y] = 4;
			//colume stay same, row minus 1
		}
		if(z == 'D'){
			a[x][y] = 3;
			//row down 1, colume stay
		}
		//0 means all 4 are legal
	}
	now = Q + 1;
	for(int i = 1; i <= n; i++){
		use[0][i] = 1;
		dfs(0, i);
		ans[now]--;//can't count itself, it's out of grid
		use[n + 1][i] = 1;
		dfs(n + 1, i);
		ans[now]--;
		use[i][n + 1] = 1;
		dfs(i, n + 1);
		ans[now]--;
		use[i][0] = 1;
		dfs(i, 0);
		ans[now]--;
		//all out side grids are usable ofcourse
	}
	for(int i = Q; i >= 1; i--){
		//un-build the ith one
		ans[i] = ans[i + 1];
		//one useable next round are all useable now
		a[qu[i].first][qu[i].second] = 0;
		if(use[qu[i].first][qu[i].second]){			
			continue;
		}
		now = i;
		int x = qu[i].first, y = qu[i].second;
		for(int j = 1; j <= 4; j++){
			if(use[x + dx[j]][y + dy[j]]){
				use[x][y] = 1;
				//any direction works, this one works
			}
		}
		if(use[x][y])dfs(x, y);
		//renew every one that can go to this one, they are
		//all useable now
	}
	for(int i = 2; i <= Q + 1; i++){
		cout<<n * n - ans[i]<<endl;
	}
	return 0;
}

Result

Result

After Thoughts

我犯的实现错误是第一遍带着所有限制深搜时一开始写的是从每一个点开始看能不能搜到边界,能就记为能用。但是这会导致如果有环的话一些本来能用的点被判定为不能用。赛后改为了从边界开始搜,被搜到的都记为能用,立马对了。唉,人品不够。

原文
存档评论1 条抓取于