AtCoder Beginner Contest 377
A - Rearranging ABC
题意
给长度为\(3\)的字符串问是不是\(ABC\)
思路
模拟。
代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int cnt[30] = { 0 };
for (int i = 0; i < 3; i++)
{
char c;
cin >> c;
cnt[c - 'A']++;
}
if (cnt[0] == 1 && cnt[1] == 1 && cnt[2] == 1)
{
cout << "Yes" << endl;
return;
}
cout << "No" << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
B - Avoid Rook Attack
题意
\(8×8\)的网格,"."表示空,"#"表示有棋子,每个棋子能攻击它所在行和列的所有格子。找出不会被攻击的格子数。
思路
怎么做都行。
代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
bool r[10], c[10];
void solve()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
char ch;
ch = getchar();
if (ch == '#')
{
r[i] = c[j] = true;
}
}
getchar();
}
int ans = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (!r[i] && !c[j])
{
ans++;
}
}
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
C - Avoid Knight Attack
题意
\(n×n\)的网格,有\(m\)个棋子,每个棋子移动如图。找出不会被攻击的格子数。
思路
用\(set\)存能攻击的坐标,答案就是\(n×n-set.size()\)。
代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 2e5 + 5;
int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int n, m, x, y;
set<pii> vis;
void solve()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> x >> y;
vis.insert({ x,y });
for (int j = 0; j < 8; j++)
{
int tx = x + dx[j];
int ty = y + dy[j];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= n)
{
vis.insert({ tx, ty });
}
}
}
cout << n * n - vis.size() << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
D - Many Segments 2
题意
给定两个长度为\(n\)的序列,以及整数\(m\),求满足以下两个条件的整数对\((l,r)\)的个数:
\(1≤l≤r≤M\)
对于每个\(1≤i≤n\)来说,区间\([l,r]\)并不完全包含区间\([L_ i, R_i]\)
思路
代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
void solve()
{
int n, m;
cin >> n >> m;
vector<int> d(m + 1, 1);
for (int i = 0; i < n; i++)
{
int l, r;
cin >> l >> r;
d[r] = max(d[r], l + 1);
}
for (int r = 1; r <= m; r++)
{
d[r] = max(d[r], d[r - 1]);
}
int ans = 0;
for (int r = 1; r <= m; r++)
{
ans += r - d[r] + 1;
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
E - Permute K times 2
题意
给定\((1,2,···,n)\)的排列\(P=(P_1,P_2,···,P_n)\),进行\(k\)次操作:使\(P_i = P_{P_i}\ (\ i = 1,2,···,n\ )\)。求最终序列。
思路
代码
点击查看代码