目录
[A. Love Story](#A. Love Story)
[B. Blank Space](#B. Blank Space)
[C. Mr. Perfectly Fine](#C. Mr. Perfectly Fine)
[D. Gold Rush](#D. Gold Rush)
[E. The Lakes](#E. The Lakes)
[F. Forever Winter](#F. Forever Winter)
[G. Hits Different](#G. Hits Different)
[H. Don't Blame Me](#H. Don't Blame Me)
比赛链接
Dashboard - Codeforces Round 871 (Div. 4) - Codeforces
A. Love Story
找到与codeforces 有多少个不同的字符。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
string s = "codeforces";
void solve() {
string st;
cin >> st;
int ans = 0;
for (int i = 0; i < st.size(); i++)
{
if (st[i] != s[i])ans++;
}
cout << ans << "\n";
}
signed main() {
ios;
TEST
solve();
return 0;
}
B. Blank Space
找连续最长的0.
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
void solve() {
int n;
cin >> n;
int cnt = 0, ans = 0;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
if (x == 0) cnt++;
else
{
ans = max(ans, cnt);
cnt = 0;
}
}
ans = max(ans, cnt);
cout << ans << "\n";
}
signed main() {
ios;
TEST
solve();
return 0;
}
C. Mr. Perfectly Fine
分成01、10、11的三种情况,找全部的最小值,前面两个的最小值相加与第三个的最小值比较出最小值。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
void solve() {
int n;
cin >> n;
int c1 = 1e9, c2 = 1e9, c3 = 1e9;
for (int i = 1; i <= n; i++)
{
int t;
cin >> t;
string s;
cin >> s;
if (s == "01") c2 = min(c2, t);
if (s == "10") c1 = min(c1, t);
if (s == "11") c3 = min(c3, t);
}
int res = min(c1 + c2, c3);
if (res != 1e9)
cout << min(c1 + c2, c3) << "\n";
else cout << "-1\n";
}
signed main() {
ios;
TEST
solve();
return 0;
}
D. Gold Rush
按照题意可以将一堆分成三堆,看看目标是否出现即可,搜索。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
if (m > n)
{
cout << "NO\n";
return;
}
if (n == m)
{
cout << "YES\n";
return;
}
queue<int>q;
q.push(n);
while (q.size())
{
int k = q.front();
q.pop();
if (k == m)
{
cout << "YES\n";
return;
}
if (k < m|| k % 3 != 0) continue;
int t=k / 3;
q.push(t);
q.push(t * 2);
}
cout << "NO\n";
}
signed main() {
ios;
TEST
solve();
return 0;
}
E. The Lakes
dfs染色法。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
int vis[1001][1001];
int a[1001][1001];
int q[N];
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
int n, m;
int sum = 0;
int ans = 0;
void dfs(int x, int y, int tag)
{
vis[x][y] = tag;
sum += a[x][y];
ans = max(ans, sum);
for (int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (tx<1 || ty<1 || tx>n || ty>m) continue;
if (a[tx][ty] == 0) continue;
if (vis[tx][ty]) continue;
dfs(tx, ty, tag);
}
}
void solve() {
cin >> n >> m;
ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
int cnt = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i][j] == 0) continue;
if (vis[i][j]) continue;
sum = 0;
++cnt;
dfs(i, j, cnt);
}
}
cout << ans << "\n";
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
vis[i][j] = 0;
}
}
}
signed main() {
ios;
TEST
solve();
return 0;
}
F. Forever Winter
模拟一下,与出入度为1的点连接的就是与主节点连接的点,输出他们的出入度即可,与主节点连接的点,出入度需要减一。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>>f(n + 1);
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
f[u].push_back(v);
f[v].push_back(u);
}
set<int>q;
for (int i = 1; i <= n; i++)
{
if (f[i].size() == 1)
{
q.insert(f[i][0]);
}
}
int ans1;
for (auto x : q)
{
ans1 = f[x].size() - 1;
break;
}
int ans2;
for (int i = 1; i <= n; i++)
{
set<int>tt;
for (auto x : f[i]) tt.insert(x);
if (tt == q)
{
ans2 = f[i].size();
break;
}
}
cout << ans2 << ' ' << ans1 << "\n";
}
signed main() {
ios;
TEST
solve();
return 0;
}
G. Hits Different
一开始用搜索,优化半天还是超时,正解是二位数组前缀和dp。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
int a[1500][1500];
int cnt=1;
int ans[N];
void solve() {
int n;
cin >> n;
cout << ans[n] << "\n";
}
signed main() {
for (int i = 1; i < 1500; i++)
{
for (int j = i - 1; j >= 1; j--)
{
a[j][i - j] = a[j - 1][i - j] + a[j][i - j - 1] - a[j - 1][i - j - 1] + cnt * cnt;
ans[cnt] = a[j][i - j];
cnt++;
}
}
ios;
TEST
solve();
return 0;
}
H. Don't Blame Me
找子序列所有元素的与和的二进制里1的个数等于k,数位dp。
cpp
#include<bits/stdc++.h>
#define int long long
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
int dp[N][64];
int a[N];
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++)
{
dp[i][a[i]] = 1;
for (int j = 0; j <= 63; j++)
{
dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
dp[i][j & a[i]] = (dp[i][j & a[i]] + dp[i - 1][j]) % mod;
}
}
int ans = 0;
for (int i = 0; i <= 63; i++)
{
int cnt = 0;
for (int j = 0; j < 6; j++)
{
if ((i >> j) & 1)
{
cnt++;
}
}
if (cnt == k)
{
ans = (ans + dp[n][i]) % mod;
}
}
cout << ans << "\n";
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= 63; j++)
{
dp[i][j] = 0;
}
}
}
signed main() {
ios;
TEST
solve();
return 0;
}