Loading

abc302 题解

打的还行,加的分不多。

A

直接除完上取整即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	LL a, b;
	cin >> a >> b;
	cout << a / b + (a % b == 0 ? 0 : 1) << '\n';
    return 0;
}

看了看jls的

	cout << (a + b - 1) / b << '\n';

妙蛙。

B

建议看jls的,我写的太复杂了。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e2 + 5, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n, m;
string s[N];
string temp = "snuke";
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		cin >> s[i]; 
		s[i] = ' ' + s[i];	
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m - 4; j++)
		{
			string str = s[i].substr(j, 5);
			if(str == temp)
			{
				cout << i << ' ' << j << '\n';
				cout << i << ' ' << j + 1 << '\n';
				cout << i << ' ' << j + 2 << '\n';
				cout << i << ' ' << j + 3 << '\n';
				cout << i << ' ' << j + 4 << '\n';
				return 0;
			}
			reverse(str.begin(), str.end());
			if(str == temp)
			{
				cout << i << ' ' << j + 4 << '\n';
				cout << i << ' ' << j + 3 << '\n';
				cout << i << ' ' << j + 2 << '\n';
				cout << i << ' ' << j + 1 << '\n';
				cout << i << ' ' << j << '\n';
				return 0;
			}
		}
	for(int i = 1; i <= m; i++)
		for(int j = 1; j <= n - 4; j++)
		{
			string st = "";
			for(int k = j; k <= j + 4; k++) st += s[k][i];
			if(st == temp)
			{
				cout << j << ' ' << i << '\n';
				cout << j + 1 << ' ' << i << '\n';
				cout << j + 2 << ' ' << i << '\n';
				cout << j + 3 << ' ' << i << '\n';
				cout << j + 4 << ' ' << i << '\n';
				return 0;
			}
			reverse(st.begin(), st.end());
			if(st == temp)
			{
				cout << j + 4 << ' ' << i << '\n';
				cout << j + 3 << ' ' << i << '\n';
				cout << j + 2 << ' ' << i << '\n';
				cout << j + 1 << ' ' << i << '\n';
				cout << j << ' ' << i << '\n';
				return 0;
			}
		}
	for(int i = 1; i <= n - 4; i++)
		for(int j = 1; j <= m - 4; j++)
		{
			string st = "";
			for(int k = 0; k <= 4; k++) st += s[i+k][j+k];
			if(st == temp){
				cout << i << ' ' << j << '\n';
				cout << i + 1 << ' ' << j + 1 << '\n';
				cout << i + 2 << ' ' << j + 2 << '\n';
				cout << i + 3 << ' ' << j + 3 << '\n';
				cout << i + 4 << ' ' << j + 4 << '\n';
				return 0;
			}
			reverse(st.begin(), st.end());
			if(st == temp){
				cout << i + 4 << ' ' << j + 4 << '\n';
				cout << i + 3 << ' ' << j + 3 << '\n';
				cout << i + 2 << ' ' << j + 2 << '\n';
				cout << i + 1 << ' ' << j + 1 << '\n';
				cout << i << ' ' << j << '\n';
				return 0;
			}
		}
	for(int i = 1; i <= n; i++)
		for(int j = 5; j <= m; j++)
		{
			string st = "";
			for(int k = 0; k <= 4; k++) st += s[i+k][j-k];
			if(st == temp){
				cout << i << ' ' << j << '\n';
				cout << i + 1 << ' ' << j - 1 << '\n';
				cout << i + 2 << ' ' << j - 2 << '\n';
				cout << i + 3 << ' ' << j - 3 << '\n';
				cout << i + 4 << ' ' << j - 4 << '\n';
				return 0;
			}
			reverse(st.begin(), st.end());
			if(st == temp)
			{
				cout << i + 4 << ' ' << j - 4 << '\n';
				cout << i + 3 << ' ' << j - 3 << '\n';
				cout << i + 2 << ' ' << j - 2 << '\n';
				cout << i + 1 << ' ' << j - 1 << '\n';
				cout << i << ' ' << j << '\n';
				return 0;
			}
		}
	return 0;
}

C

枚举全排列判一下就行。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n, m;
int a[N];
char s[N][N];
int match(char *s1, char *s2)
{
	int cnt = 0;
	for(int i = 0; i < m; i ++) if(s1[i] != s2[i]) cnt ++;
	return cnt;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) cin >> s[i];
	for(int i = 1; i <= n; i ++) a[i] = i;
	bool f = false;
    do 
	{	
		bool ok = true;
		for(int i = 1; i < n; i ++) if(match(s[a[i]], s[a[i + 1]]) != 1) ok = false;
		if(ok)
		{
			f = true;
			break;
		} 
    } while (next_permutation(a + 1, a + 1 + n));
   	if(f) cout << "Yes\n";
   	else cout << "No\n";
    return 0;
}

D

b 数组中找到最后一个小于等于 ai+d 的数,判一下差记录答案就行。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
LL a[N], b[N];
LL n, m, d;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin >> n >> m >> d;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	for(int i = 1; i <= m; i ++) cin >> b[i];
	sort(b + 1, b + m + 1);
	LL res = -1;
	for(int i = 1; i <= n; i ++)
	{
		int tmp = upper_bound(b + 1, b + m + 1, a[i] + d) - b - 1;
		if(abs(a[i] - b[tmp]) <= d) res = max(res, a[i] + b[tmp]);
	}
	cout << res << '\n';
    return 0;
}

E

按照题意用set模拟。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5 + 5, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n, q, cnt;
set<int> adj[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin >> n >> q;
	cnt = n;
	while(q --)
	{
		int op, u, v;
		cin >> op >> u;
		if(op == 1)
		{
			cin >> v;
			if(adj[u].size() == 0) cnt --;
			if(adj[v].size() == 0) cnt --;
			adj[u].insert(v);
			adj[v].insert(u);
		}
		else
		{
			if(adj[u].size() != 0)  cnt ++;
			for(auto t : adj[u]) 
			{
				adj[t].erase(u);
				if(adj[t].size() == 0) cnt ++;
			}
			adj[u].clear();
		}
		cout << cnt << '\n';
	}
    return 0;
}

F

集合编号像集合内的每个元素连边权为 1 的边,元素向集合编号连边权为 0 的边,跑 01bfs即可,dijkstra 也不是不行。
赛后发现边权一定是交错走的,不用跑,但是 01bfs 好像挺快?79ms,目前没发现更短的。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 5e5 + 5, M = N, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n, m;
int h[N], nxt[M << 1], to[M << 1], val[M << 1], cnt;
int dist[N];
bool st[N];
void add(int u, int v, int w)
{
	to[++ cnt] = v, val[cnt] = w, nxt[cnt] = h[u], h[u] = cnt;
}
void bfs(int s)
{
	for(int i = 1;i <= n + m;i ++) dist[i] = INF, st[i] = false;
	deque<int> q;
	q.push_back(s);
	dist[s] = 0;
	while(q.size())
	{
		int u = q.front();
		q.pop_front();
		if(st[u]) continue;
		st[u] = true;
		if(u == n + m) break;
		for(int i = h[u]; i; i = nxt[i])
		{
			int v = to[i], w = val[i];
			if(dist[u] + w < dist[v])
			{
				dist[v] = dist[u] + w;
				if(w == 1) q.push_back(v);
				else q.push_front(v);
			}
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++)
	{
		int len;
		cin >> len;
		while(len --)
		{
			int x;
			cin >> x;
			add(i, n + x, 0);
			add(n + x, i, 1);
		}
	}
	bfs(n + 1);
	if(dist[n + m] == INF) dist[n + m] = 0;
	cout << dist[n + m] - 1 << '\n';
    return 0;
}

G

不会,后面补。

Ex

不会,后面补。

posted @   Svemit  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示