Atcoder Regular Contest 059 题解
ARC059C. Be Together *712
签到题。枚举要改成哪个,因为值域只有
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN = 105;
LL n, A[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for(LL i = 1; i <= n; i ++) cin >> A[i];
LL ans = 1e18;
for(LL i = -100; i <= 100; i ++)
{
LL anss = 0;
for(LL j = 1; j <= n; j ++)
anss += (A[j] - i) * (A[j] - i);
ans = min(ans, anss);
}
cout << ans << '\n';
return 0;
}
ARC059D. Unbalanced *1374
小清新结论题,考虑怎样构造这个串。其重要条件就是两个相同的字母的位置的绝对值相差为
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
string s;
cin >> s;
for(int i = 1; i < (int)s.length(); i ++)
{
if(i >= 2)
{
if(s[i] == s[i - 2])
{
cout << i - 1 << " " << i + 1 << '\n';
return 0;
}
}
if(s[i] == s[i - 1])
{
cout << i << " " << i + 1 << '\n';
return 0;
}
}
cout << -1 << " " << -1 << '\n';
return 0;
}
ARC059E. Children and Candies *2189
算贡献的题,又看起来是一个背包。所以我们考虑在背包的基础上做一做手脚。
定义
答案就是
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN = 405;
const LL MOD = 1e9 + 7;
LL n, c, A[MAXN], B[MAXN], Pw[MAXN][MAXN], SumPw[MAXN][MAXN], DP[MAXN][MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> c;
for(LL i = 1; i <= n; i ++) cin >> A[i];
for(LL i = 1; i <= n; i ++) cin >> B[i];
for(LL i = 1; i < MAXN; i ++)
{
Pw[i][0] = 1ll;
for(LL j = 1; j < MAXN; j ++)
Pw[i][j] = (Pw[i][j - 1] * i) % MOD;
}
for(LL i = 1; i < MAXN; i ++)
for(LL j = 0; j < MAXN; j ++)
SumPw[i][j] = (SumPw[i - 1][j] + Pw[i][j]) % MOD;
DP[0][0] = 1;
for(LL i = 1; i <= n; i ++)
for(LL j = 0; j <= c; j ++)
for(LL k = 0; k <= j; k ++)
DP[i][j] = (DP[i][j] + (DP[i - 1][j - k] * (SumPw[B[i]][k] - SumPw[A[i] - 1][k] + MOD)) % MOD) % MOD;
cout << DP[n][c] << '\n';
return 0;
}
*ARC059F. Unhappy Hacking *2427
我们发现只要字符串长度一样,不管字符串里面的东西是什么,答案都是一样的。这一点非常好理解。
考虑动态规划。设
- 这一位匹配:
- 这一位退格:
,因为我们并不关心上一位敲了什么东西,因为字符串是任意的。
答案就是
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const LL MAXN = 5005;
LL n, m;
string s;
LL DP[MAXN][MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> s; m = s.length(); s = ' ' + s;
DP[0][0] = 1;
for(LL i = 1; i <= n; i ++)
for(LL j = 0; j <= i; j ++)
DP[i][j] = (DP[i][j] + DP[i - 1][max(0ll, j - 1)] + DP[i - 1][j + 1] * 2) % MOD;
cout << DP[n][m] << '\n';
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】