ABC472总结(B, C)

C

有步数限制的搜索题,首先考虑广搜。

由于内存不够,本题用 \(string\) 存图最好。

#include<bits/stdc++.h>
#define F(i,l,r) for(int i(l); i <= (r); ++ i)
#define G(i,r,l) for(int i(r); i >= (l); -- i)

#define int ll 

using namespace std;
using ll = long long;
const int N = 3e5;
int n, k, m, sum = 0;
int a[N], q[N], st = 1, ed = 0;
void Main(){
	cin >> n >> m >> k;
	F(i, 1, n){
		cin >> a[i];
	} 
	F(i, 1, n){
		if(i <= m){
			if(sum + a[i] <= k){
				++ ed;
				q[ed] = i;
				sum += a[i];
				cout << "Yes\n"; 
			} 
			else{
				cout << "No\n";
			} 
		}
		else{
			if(st <= ed){
				while(st <= ed && q[st] + m - 1 < i){
					sum -= a[q[st]];
					++ st;
				}	
			}
			if(sum + a[i] <= k){
				++ ed;
				q[ed] = i;
				sum += a[i];
				cout << "Yes\n"; 
			} 
			else{
				cout << "No\n";
			} 			
		}
	} 
	return ;
} 
signed main(){
	ios::sync_with_stdio(0); cin.tie(); cout.tie(0);
	int T = 1;
	while(T --) Main();
	return fflush(0), 0;
}

D

简单无向图上找环。板子题,思考了一下不需要用 \(tarjan\),可以用深搜 + 模拟栈 + 染色实现(\(0\) 表示没走过,\(1\) 表示走过且入栈,\(2\) 表示走过且退栈)。(小声bb,终于会非 \(tarjan\) 的找环写法了)

#include<bits/stdc++.h>
#define F(i,l,r) for(int i(l); i <= (r); ++ i)
#define G(i,r,l) for(int i(r); i >= (l); -- i)
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
using ll = long long;
const int N = 5e5 + 1000;
struct node{
	int x, y, num;
};
string s[N];
int n, m, k, ans = 0;
bool h[N], w[N];
queue<node> q;
int dx[10] = {1, -1, 0, 0};
int dy[10] = {0, 0, 1, -1};
void Main(){
	cin >> n >> m >> k;
	-- n;
	-- m;
	F(i, 0, n){
		cin >> s[i];	
		F(j, 0, m){
			if(s[i][j] == '#'){
				h[i] = 1;
				w[j] = 1;
			}
		}
	}
	F(i, 0, n){
		F(j, 0, m){
			if(h[i] == 0 && w[j] == 0){
				q.push(node{i, j, 0});
//				printf("(%d %d)\n", i + 1, j + 1); 
				++ ans;
				s[i][j] = '1'; 
			}
		}
	}
//	printf("\n"); 
	while(q.size()){
		node u = q.front();
		q.pop();
		int x = u.x, y = u.y, num = u.num;
		if(num >= k){
			continue;
		} 
		F(i, 0, 3){
			int px = x + dx[i];
			int py = y + dy[i];
			if(px < 0 || px > n) continue;
			if(py < 0 || py > m) continue;// overcut
			if(s[px][py] == '.'){
				++ ans;
				q.push(node{px, py, num + 1}); 
//				printf("(%d %d)\n", px + 1, py + 1); 
				s[px][py] = '1'; 
			}
		}
	}
	cout << ans << '\n'; 
	return ;
} 
signed main(){
	ios::sync_with_stdio(0); cin.tie(); cout.tie(0);
	int T = 1;
	while(T --) Main();
	return fflush(0), 0;
}
posted @ 2026-08-27 22:21  superl61  阅读(12)  评论(0)    收藏  举报