Loading

AtCoder Beginner Contest 399



A - Hamming Distance

题意

思路

模拟

代码

点击查看代码
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;

const int mxn = 1e7 + 10;

void solve()
{
	int n, ans = 0;
	cin >> n;
	string s, t;
	cin >> s >> t;
	for (int i = 0; i < n; i++)
	{
		if (s[i] != t[i])
		{
			ans++;
		}
	}
	cout << ans << endl;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int __ = 1;
	//cin >> __;
	while (__--)
	{
		solve();
	}

	return 0;
}


B - Ranking with Ties

题意

思路

模拟

代码

点击查看代码
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;

const int mxn = 1e7 + 10;

void solve()
{
	int n;
	cin >> n;
	vector<pii> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].first;
		a[i].second = i;
	}
	sort(a.begin(), a.end(), greater<pii>());
	int k = 1;
	vector<int> ans(n);
	ans[a[0].second] = k;
	int x = 1;
	for (int i = 1; i < n; i++)
	{
		if (a[i].first == a[i - 1].first)
		{
			x++;
			ans[a[i].second] = k;
			continue;
		}
		k += x;
		ans[a[i].second] = k;
		x = 1;
	}
	for (int i = 0; i < n; i++)
	{
		cout << ans[i] << endl;
	}
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int __ = 1;
	//cin >> __;
	while (__--)
	{
		solve();
	}

	return 0;
}

C - Make it Forest

题意

思路

如果两个顶点已经在同一集合中,说明这条边会形成环,需要删除;否则合并它们。

代码

点击查看代码
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;

const int mxn = 1e7 + 10;

struct DSU {
	std::vector<int> f, siz;
	DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }
	int leader(int x) {
		while (x != f[x]) x = f[x] = f[f[x]];
		return x;
	}
	bool same(int x, int y) { return leader(x) == leader(y); }
	bool merge(int x, int y) {
		x = leader(x);
		y = leader(y);
		if (x == y) return false;
		siz[x] += siz[y];
		f[y] = x;
		return true;
	}
	int size(int x) { return siz[leader(x)]; }
};

void solve()
{
	int n, m, ans = 0;
	cin >> n >> m;
	DSU d(n + 1);
	for (int i = 0; i < m; i++)
	{
		int u, v;
		cin >> u >> v;
		if (!d.merge(u, v))
		{
			ans++;
		}
	}
	cout << ans << endl;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int __ = 1;
	//cin >> __;
	while (__--)
	{
		solve();
	}

	return 0;
}


D - Switch Seats

题意

思路

当且仅当\(a\)\(b\)相邻时且两个\(a\)不相邻、两个\(b\)不相邻有\(1\)的贡献。求出一个数字两次出现的位置,剔掉不合法的即可

代码

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;

const int mxn = 1e6 + 10;

void solve()
{
	int n, ans = 0;
	cin >> n;
	n *= 2;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	vector<pii> p;
	vector<int> first(n + 1, -1);
	for (int i = 0; i < n; i++)
	{
		if (first[a[i]] == -1)
		{
			first[a[i]] = i;
		}
		else
		{
			p.push_back(make_pair(first[a[i]], i));
		}
	}
	sort(p.begin(), p.end());
	for (int i = 0; i < (int)p.size() - 1; i++)
	{
		auto [af, as] = p[i];
		auto [bf, bs] = p[i + 1];
		if (as - af == 1 || bs - bf == 1 || abs(af - bf) != 1 || abs(as - bs) != 1)
		{
			continue;
		}
		ans++;
	}
	cout << ans << endl;

}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int T;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}

E - Replace

题意

思路

代码

点击查看代码



比赛链接 https://atcoder.jp/contests/abc399

posted @ 2025-03-31 09:39  _SeiI  阅读(44)  评论(0)    收藏  举报