AtCoder Beginner Contest 386
A - Full House 2
题意
给\(4\)个整数,问能否添加一个整数使得恰有\(3\)个整数\(a\)和\(2\)个整数\(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\)
思路
由于最多只能操作一次,所以\(s\)与\(t\)的长度最多相差\(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\)及其上边全是黑色,其余是白色。问是否能满足要求。
思路
显然对于最开始被染成黑色的格子,在它的左上区域(\(x\le x_i\),\(y\le y_i\))都不能有白色。那只要记录最左以及最上的白色格子位置,看有没有黑格子在其右、下即可。由于先以\(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\)个元素进行异或,求异或的最大值,即求:
思路
题目保证\(C_n^k\le 10^6\),对于较小的\(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;
}


                
            
        
浙公网安备 33010602011771号