文章目录
一、A - Love Story
-
思路:
直接比较 -
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5,mod = 998244353;
int a[N];
void solve()
{
string s; cin >> s;
string res = "codeforces";
int d = 0;
for(int i = 0;i < 10;i ++ )
if(res[i] != s[i])
d++;
cout << d << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
二、B - Blank Space
-
思路: 因为数据很小直接暴力,数据大的时候,用一下双指针
-
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5,mod = 998244353;
int a[N];
void solve()
{
int n; cin >> n;
for(int i = 1;i <= n;i ++) cin >> a[i];
int ans = 0;
for(int i = 1;i <= n;i ++ )
{
int j = i;
if(a[i] == 0)
{
int d = 0;
while(j <= n && a[j] == 0)
{
j++;
d++;
}
ans = max(ans,d);
i = j;
}
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
三、C - Mr. Perfectly Fine
- 思路: 因为只有两种情况可以合成11
1 01 + 10
2 11
那就分别统计字符为"11",“01”,"10"的最小值,然后比较就行 - 代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5,mod = 998244353;
void solve()
{
int n; cin >> n;
int q1 = 1e9,q2 = 1e9,q3 = 1e9;
for(int i = 1;i <= n;i ++ )
{
int x;cin >> x;
string s; cin >> s;
if(s == "11")
{
q3 = min(q3,x);
}
else if(s == "01")
{
q1 = min(q1,x);
}
else if(s == "10")
{
q2 = min(q2,x);
}
}
int q = min(q1 + q2,q3);
if(q >= 1e9) cout << "-1" << endl;
else cout << q << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
四、D - Gold Rush
- 思路: 暴力bfs队列需要注意的是越界问题,一个数字 x 只能由 x * 3 或者 x + x/2转移而来,直接写就行
- 代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5,mod = 998244353;
bool st[N];
vector<int> w;
void solve()
{
int n,m; cin >> n >> m;
if(n == m) cout << "YES" << endl;
else if(n < m) cout << "NO" << endl;
else if(n % 3 != 0) cout << "NO" << endl;
else
{
// 每个只遍历一遍就行了
queue<int> q; q.push(m);
w.clear();
int f = 0;
while(q.size())
{
int t = q.front(); q.pop();
w.push_back(t);
if(st[t] || t > n) continue;
if(t == n)
{
f = 1;
break;
}
st[t] = true;
if(t * 3 <= n && st[t * 3] == false) q.push(t * 3);
if(t + t / 2 <= n && t % 2 == 0 && st[t + t / 2] == false) q.push(t + t / 2);
}
for(auto x:w) st[x] = false;
if(f) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
五、E - The Lakes
-
思路:
正常dfs一遍,把所有的不是0的边标记一下统计和,然后比较最大值 -
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5,mod = 998244353;
int a[N][N];
int dx[4] = {1,-1,0,0},dy[4] = {0,0,1,-1};
bool st[N][N];
int n,m;
int dfs(int x,int y)
{
st[x][y] = true;
int w = a[x][y];
for(int i = 0;i < 4;i ++ )
{
int x1 = x + dx[i],y1 = y + dy[i];
if(x1 < 1 || y1 < 1 || x1 > n || y1 > m || st[x1][y1] || a[x1][y1] == 0) continue;
w += dfs(x1,y1);
}
return w;
}
void solve()
{
cin >> n >> m;
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ )
cin >> a[i][j];
int ans = 0;
for(int i = 1;i <= n;i ++ )
{
for(int j = 1;j <= m;j ++ )
{
if(a[i][j] != 0 && st[i][j] == false)
{
int k = dfs(i,j);
ans = max(ans,k);
}
}
}
cout << ans << endl;
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ )
st[i][j] = false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
六、F - Forever Winter
-
思路:
因为是n个点,n - 1条边,题目是一个完整的图,我们可以先统计每个点连接有几条边,枚举中心点,如果该点的所有子节点的边数之和是m并且所有子节点连接的边数是相同的,该点就是中心点,然后中心点连接的边数就是 x,某个子节点连接的边数 - 1就是y -
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5,M = 1e3 +5,mod = 998244353;
int a[N];
vector<int> res[N];
void solve()
{
int n,m;
cin >> n >> m;
for(int i = 1;i <= m;i ++ )
{
int u,v; cin >> u >> v;
a[u]++;
a[v]++;
res[u].push_back(v);
res[v].push_back(u);
}
int x = 0,y = 0;
for(int i = 1;i <= n;i ++ )
{
vector<int> w;
for(auto k:res[i])
{
w.push_back(a[k]);
}
int qq = w[0];
int f = 0;
for(int j = 1;j < w.size();j ++ )
{
if(w[j] != qq)
{
f = 1;
break;
}
}
if(w.size() * qq != m) f = 1;
if(f == 1) continue;
x = w.size();
y = w[0] - 1;
break;
}
cout << x << ' ' << y << endl;
for(int i = 1;i <= n;i ++ ) res[i].clear(),a[i] = 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
七、G - Hits Different
-
思路:
预处理 + dp,观察这个图,你把这个图按二维数组看,我们定义dp[i][j]是打碎第i行第j列导致破碎的所有点的和,
你会发现f[i][j] 只由他们上一层来决定 f [ i ][ j ] = f [ i - 1 ] [ j ] + f[ i - 1 ][ j - 1 ],但是会有重复的,然后再看发现重复的是 f [ i - 2][ j - 1 ],然后再减去这个就行了 所以转移方程为 f [ i ][ j ] = f [ i - 1 ] [ j ] + f[ i - 1 ][ j - 1 ] - f [ i - 2][ j - 1 ];
然后就是把这个点变成是第几行第几列的,直接暴力算就行 -
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2025,M = 1e3 +5,mod = 998244353;
int a[N][N];
int f[N][N];
void init()
{
for(int i = 1;i <= 2023;i ++ ) // 第i行的和是多少
{
int w = i * (i - 1) / 2 + 1;
for(int j = 1;j <= i;j ++)
{
a[i][j] = w;
w++;
}
}
for(int i = 1;i <= 2023;i ++ )
{
for(int j = 1;j <= i;j ++ )
{
if(j == 1)
f[i][j] = f[i - 1][j] + a[i][j] * a[i][j];
else if(j == i)
f[i][j] = f[i - 1][j - 1] + a[i][j] * a[i][j];
else if(i - 2 >= 0)
f[i][j] = f[i - 1][j] + f[i - 1][j - 1] + a[i][j] *a[i][j] - f[i - 2][j - 1];
else
f[i][j] = f[i - 1][j] + f[i - 1][j - 1] + a[i][j] *a[i][j];
}
}
}
void solve()
{
int n; cin >> n;
int x,y;
if(n == 1)
{
cout << "1" << endl;
return;
}
for(int i = 1;i <= 2023;i ++ ) // 第i行
{
if(n - i * (i + 1) / 2 <= i + 1)
{
x = i + 1;
y = n - i * (i + 1) / 2;
break;
}
}
cout << f[x][y] << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);init();
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}
八、H - Don’t Blame Me
- 思路:
dp,
我们定义 f[ i ][ j ]是从前i个里面选,子序列**&**之后值为 j 的个数,因为j最大就是63,时间复杂度为 n * 63
状态转移: 不选第 i 个: f [ i ][ j ] += f [ i - 1 ][ j ]
选第i个:f [ i ][ j & a[i] ] += f [ i - 1 ][ j ]
因为要考虑前面什么都不选的话,答案是1,所以初始化的时候 f [ i ][ a [ i ] ] = 1
最后就是遍历一遍0 ~ 63,看看哪一个数字的二进制恰好是k个,相加便可 - 代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5,M = 1e3 +5,mod = 1e9+7;
// 每一个数字最多只有6位
// k最大也只是6
// f[i][j] 从前i个里面选,子序列操作之后的结果为j的个数
// f[i][j] = f[i - 1][j] // 不选第i个
// f[i][j & a[i]] = f[i - 1][j] // 选第i个,从前i - 1里面选,与的值为j,选完第i个以后值为 j & a[i]
// f[i][j] 一个也不选的值为1
int f[N][65];
int a[N];
int check(int x)
{
int d = 0;
while(x != 0)
{
if(x % 2 == 1) d++;
x /= 2;
}
return d;
}
void solve()
{
int n,k; cin >> n >> k;
for(int i = 1;i <= n;i ++ ) cin >> a[i];
for(int i = 1;i <= n;i ++ )
for(int j = 0;j <= 63;j ++ )
f[i][j] = 0;
for(int i = 1;i <= n;i ++ )
{
f[i][a[i]] = 1;
for(int j = 0;j <= 63;j ++ )
{
f[i][j] = (f[i][j] + f[i - 1][j]) % mod;
f[i][j & a[i]] = (f[i][j & a[i]] + f[i - 1][j]) % mod;
}
}
int ans = 0;
for(int i = 0;i <= 63;i ++ )
{
int s = check(i);
if(s == k)
ans = (ans + f[n][i]) % mod;
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T -- ) solve();
return 0;
}