AtCoder Beginner Contest 386



A - Full House 2#

题意#

4个整数,问能否添加一个整数使得恰有3个整数a2个整数b

思路#

模拟

代码#

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

const int mxn = 1e6 + 5;

void solve()
{
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    map<int, int> m;
    m[A]++;
    m[B]++;
    m[C]++;
    m[D]++;
    int a = 0; 
    int b = 0; 

    for (const auto& i : m)
    {
        if (i.second == 3) 
        {
            a++;
        }
        else if (i.second == 2) 
        {
            b++;
        }
    }
    if (a > 0 || b >= 2) 
    {
        cout << "Yes" << endl;
    }
    else 
    {
        cout << "No" << endl;
    }
}

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

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

	return 0;
}


B - Calculator#

题意#

基础字符"00,0,1,2,3,4,5,6,7,8,9"。给定字符串s,求其最少由多少基础字符组成

思路#

模拟

代码#

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

const int mxn = 1e6 + 5;

void solve()
{
	string s;
	cin >> s;
	if (s.find('0') == -1)
	{
		cout << s.size();
		return;
	}
	int cnt = 0, ans = s.size();
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == '0')
		{
			if (cnt)
			{
				cnt = 0;
				ans--;
			}
			else
			{
				cnt++;
			}
		}
		else
		{
			cnt = 0;
		}
	}
	cout << ans << endl;
}

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

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

	return 0;
}


C - Operate 1#

题意#

在字符串s中进行不超过1次操作:插入、删除、替换一个字符,判断是否有可能使得s=t

思路#

由于最多只能操作一次,所以st的长度最多相差1(增/删一次);长度相同,最多只能有一个字符不同(替换一次);长度不同,只能是长串中多出一个字符,其他有不一样就无解

代码#

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

const int mxn = 1e6 + 5;

void solve()
{
	int k;
	string s, t;
	cin >> k >> s >> t;
	if (s == t)
	{
		cout << "Yes" << endl;
		return;
	}
	int ss = s.size(), tt = t.size();
	if (abs((int)ss - (int)tt) > 1)
	{
		cout << "No" << endl;
		return;
	}
	if (ss == tt)
	{
		int cnt = 0;
		for (int i = 0; i < ss; i++)
		{
			if (s[i] != t[i])
			{
				cnt++;
				if (cnt > 1)
				{
					cout << "No" << endl;
					return;
				}
			}
		}
		cout << "Yes" << endl;
		return;
	}
	int cnt = 0;
	for (int i = 0, j = 0; i < s.size() && j < t.size(); i++, j++)
	{
		if (s[i] == t[j])
		{
			continue;
		}
		cnt++;
		if (cnt > 1)
		{
			cout << "No" << endl;
			return;
		}
		if (s.size() > t.size())
		{
			i++;
		}
		else
		{
			j++;
		}
	}
	cout << "Yes" << endl;
}

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

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

	return 0;
}


D - Diagonal Separation#

题意#

给定n×n的网格,起初有m格被染色(给出x,y和颜色)。现在要讲所有格子都染成黑色或白色,使得:对于每一行,存在一个i使得第i格及其左边全是黑色,其余是白色;对于每一列,存在一个j使得j及其上边全是黑色,其余是白色。问是否能满足要求。

思路#

显然对于最开始被染成黑色的格子,在它的左上区域(xxiyyi)都不能有白色。那只要记录最左以及最上的白色格子位置,看有没有黑格子在其右、下即可。由于先以x升序排序,故只需记录最靠上的白格即可

代码#

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

const int mxn = 2e5 + 5;

struct node
{
	int x, y;
	char ch;
	bool operator < (const node& a)
	{
		if (x == a.x)
		{
			return y < a.y;
		}
		return x < a.x;
	}
};

void solve()
{
	int n, m;
	cin >> n >> m;
	vector<node> v(m);
	for (int i = 0; i < m; i++)
	{
		int x, y;
		char ch;
		cin >> x >> y >> ch;
		x--, y--;
		v[i].x = x, v[i].y = y, v[i].ch = ch;
	}
	sort(v.begin(), v.end());
	int min_y = 1e18;
	for (int i = 0; i < m; i++)
	{
		int x = v[i].x;
		int y = v[i].y;
		char ch = v[i].ch;
		if (ch == 'W')
		{
			min_y = min(min_y, y);
		}
		else if (min_y <= y)
		{
			cout << "No" << endl;
			return;
		}
	}
	cout << "Yes" << endl;
}

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

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

	return 0;
}


E - Maximize XOR#

题意#

给定大小为n的非负数序列A,从中选k个元素进行异或,求异或的最大值,即求:

思路#

题目保证Cnk106,对于较小的k可以直接枚举(dfs);对于较大的k可以反过来,先求出所有元素的异或和,枚举不需要的元素

代码#

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

const int mxn = 2e5 + 5;
int n, k, ans = -1;
vector<int> a;

void dfs(int p, int res, int m)
{
	if (!m)
	{
		ans = max(ans, res);
		return;
	}
	if (p == n) // 没选够
	{
		return;
	}
	dfs(p + 1, res ^ a[p], m - 1);
	dfs(p + 1, res, m);
}

void solve()
{
	int tot = 0;
	cin >> n >> k;
	a.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		tot ^= a[i];
	}
	if (k <= n - k)
	{
		dfs(0, 0, k);
	}
	else
	{
		dfs(0, tot, n - k);
	}
	cout << ans << endl;
}

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

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

	return 0;
}



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

posted @   _SeiI  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示