AtCoder Beginner Contest 386
A - Full House 2#
题意#
给
个整数,问能否添加一个整数使得恰有 个整数 和 个整数
思路#
模拟
代码#
点击查看代码
#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#
题意#
基础字符"
"。给定字符串 ,求其最少由多少基础字符组成
思路#
模拟
代码#
点击查看代码
#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#
题意#
在字符串
中进行不超过 次操作:插入、删除、替换一个字符,判断是否有可能使得
思路#
由于最多只能操作一次,所以
与 的长度最多相差 (增/删一次);长度相同,最多只能有一个字符不同(替换一次);长度不同,只能是长串中多出一个字符,其他有不一样就无解
代码#
点击查看代码
#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#
题意#
给定
的网格,起初有 格被染色(给出 和颜色)。现在要讲所有格子都染成黑色或白色,使得:对于每一行,存在一个 使得第 格及其左边全是黑色,其余是白色;对于每一列,存在一个 使得 及其上边全是黑色,其余是白色。问是否能满足要求。
思路#
显然对于最开始被染成黑色的格子,在它的左上区域(
, )都不能有白色。那只要记录最左以及最上的白色格子位置,看有没有黑格子在其右、下即可。由于先以 升序排序,故只需记录最靠上的白格即可
代码#
点击查看代码
#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#
题意#
思路#
题目保证
,对于较小的 可以直接枚举( );对于较大的 可以反过来,先求出所有元素的异或和,枚举不需要的元素
代码#
点击查看代码
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效