AtCoder Regular Contest 170 A-C
A - Yet Another AB Problem
贪心。
定义下标
对所有
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
int main()
{
cctie;
int n; cin >> n;
string S, T; cin >> S >> T;
set<int> atob, btoa;
for (int i = 0; i < n; i++)
{
if (S[i] == T[i]) continue;
if (S[i] == 'B' && T[i] == 'A') btoa.insert(i);
else atob.insert(i);
}
int ans = 0;
while (!btoa.empty() && !atob.empty() && *btoa.begin() < *(--atob.end()))
{
int k = *btoa.begin();
auto ita = atob.upper_bound(k);
int t = *ita;
S[k] = 'A', S[t] = 'B';
btoa.erase(k);
atob.erase(t);
ans++;
}
if (!atob.empty())
{
int k = n;
for (int i = 0; i < n; i++)
{
if (S[i] == T[i] && S[i] == 'A')
{
k = i;
break;
}
}
if (k < *atob.begin()) ans += atob.size();
else ans = -1;
}
if (ans != -1 && !btoa.empty())
{
int k = -1;
for (int i = n - 1; i >= 0; i--)
{
if (S[i] == T[i] && S[i] == 'B')
{
k = i;
break;
}
}
if (k > *(--btoa.end())) ans += btoa.size();
else ans = -1;
}
cout << ans << '\n';
return 0;
}
B - Arithmetic Progression Subsequence
枚举、二分。
记符合题目要求的区间为符合区间。
当
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define i64 long long
#define all(x) x.begin(), x.end()
int main()
{
cctie;
int n; cin >> n;
vector<int> b(n + 1);
vector a(11, vector<int>(0));
for (int i = 1; i <= n; i++)
{
cin >> b[i];
a[b[i]].push_back(i);
}
vector<int> r(n + 2, n + 1);
for (int k = 1; k <= 4; k++)
{
for (int i = 1; i + 2 * k <= 10; i++)
{
int j = i + k;
int p = j + k;
for (auto ii : a[i])
{
auto jj = upper_bound(all(a[j]), ii);
if (jj == a[j].end()) break;
auto pp = upper_bound(all(a[p]), *jj);
if (pp == a[p].end()) break;
r[ii] = min(r[ii], *pp);
}
for (auto pp : a[p])
{
auto jj = upper_bound(all(a[j]), pp);
if (jj == a[j].end()) break;
auto ii = upper_bound(all(a[i]), *jj);
if (ii == a[i].end()) break;
r[pp] = min(r[pp], *ii);
}
}
}
for (int i = 1; i <= 10; i++)
for (int j = 2; j < a[i].size(); j++)
r[a[i][j - 2]] = min(r[a[i][j - 2]], a[i][j]);
i64 ans = 1LL * (1 + n) * n / 2;
for (int i = n; i >= 1; i--)
{
r[i] = min(r[i], r[i + 1]);
ans -= r[i] - i;
}
cout << ans << '\n';
return 0;
}
C - Prefix Mex Sequence
动态规划。
记符合题目要求的序列为符合序列。
当
当
状态转移方程如下:
若
若
可以合成一个方程:
答案即为
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define i64 long long
const int N = 5010, mod = 998244353;
int n, m;
int S[N];
i64 dp[N][N];
int main()
{
cctie;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> S[i];
dp[0][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= min(m + 1, i); j++)
dp[i][j] = ((1 - S[i]) * dp[i - 1][j] * j + dp[i - 1][j - 1] * (1 + (1 - S[i]) * (m - j))) % mod;
i64 ans = 0;
for (int j = 1; j <= min(m + 1, n); j++) ans += dp[n][j];
cout << ans % mod << '\n';
return 0;
}
分类:
题解 / AtCoder
标签:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探