Educational Codeforces Round 174 (Rated for Div. 2)
A - Was there an Array?#
题意#
思路#
有“
”就无解
代码#
点击查看代码
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
void solve()
{
int n;
cin >> n;
vector<int> b(n - 2);
for (int i = 0; i < n - 2; i++)
{
cin >> b[i];
}
if (n <= 4)
{
cout << "YES" << endl;
return;
}
for (int i = 0; i < n - 4; i++)
{
if (b[i] == 1 && b[i + 1] == 0 && b[i + 2] == 1)
{
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;
}
B - Set of Strangers#
题意#
给定
的网格,每个网格有对应的颜色。每次操作可以选择颜色相同且不相邻的任意网格将其染成一种颜色。问将所有网格染成相同的颜色的最少操作数
思路#
对于某一种颜色,若每个网格都不相邻,则染成其他颜色只需要
次操作;有相邻的要 次。统计每种颜色是否相邻,无贡献为 ,否则为 ,最后选定要染的颜色——为了减少操作数,尽量选择有相邻的颜色
代码#
点击查看代码
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 2e5 + 10;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void solve()
{
int n, m;
cin >> n >> m;
unordered_set<int> s;
vector<bool> connect(n * m + 1, false);
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
s.insert(a[i][j]);
}
}
if (s.size() == 1)
{
cout << 0 << endl;
return;
}
bool f = false;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (connect[a[i][j]])
{
continue;
}
for (int k = 0; k < 4; k++)
{
int tx = i + dx[k];
int ty = j + dy[k];
if (tx < 0 || tx >= n || ty < 0 || ty >= m || a[tx][ty] != a[i][j])
{
continue;
}
connect[a[i][j]] = f = true;
break;
}
}
}
int ans = 0;
for (auto& i : s)
{
ans += (connect[i] ? 2 : 1);
}
cout << ans - 1 - f << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int __ = 1;
cin >> __;
while (__--)
{
solve();
}
return 0;
}
C - Beautiful Sequence#
题意#
思路#
由于只含有
三种元素,所以合法序列只能是 的形式。所以答案即对于每一对 ,求出其中间 的个数 ,这对 的贡献就是 。再预处理一下,避免超时
代码#
点击查看代码
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
const int mod = 998244353;
const int mxn = 2e5 + 10;
int pow2[mxn], inv_pow2[mxn];
int qpow(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1)
{
res = res * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return res;
}
void init()
{
pow2[0] = inv_pow2[0] = 1;
int inv_2 = qpow(2, mod - 2);
for (int i = 1; i < mxn; i++)
{
pow2[i] = pow2[i - 1] * 2 % mod;
inv_pow2[i] = inv_pow2[i - 1] * inv_2 % mod;
}
}
void solve()
{
int n;
cin >> n;
vector<int> a(n), cnt2(n);
vector<int> p1, p3;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] == 2)
{
cnt2[i] = (i ? cnt2[i - 1] + 1 : 1);
}
else
{
cnt2[i] = (i ? cnt2[i - 1] : 0);
if (a[i] == 1)
{
p1.push_back(i);
}
else if (a[i] == 3)
{
p3.push_back(i);
}
}
}
if (p1.empty() || p3.empty())
{
cout << 0 << endl;
return;
}
vector<int> pre(1, 0);
for (int i : p1)
{
int ai = cnt2[i];
pre.push_back((pre.back() + inv_pow2[ai]) % mod);
}
int ans = 0;
for (int i : p3)
{
int cnt = cnt2[i];
int num = upper_bound(p1.begin(), p1.end(), i) - p1.begin();
if (num == 0)
{
continue;
}
int sum = pre[num];
int t = (pow2[cnt] * sum % mod - num + mod) % mod;
ans = (ans + t) % mod;
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int __ = 1;
cin >> __;
init();
while (__--)
{
solve();
}
return 0;
}
D - Palindrome Shuffle#
题意#
思路#
代码#
点击查看代码
比赛链接 https://mirror.codeforces.com/contest/2069
分类:
CodeForces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具