Atcoder Beginner Contest 355 A - D
目录
A. Who Ate the Cake?
思路:
签到题直接模拟即可,只要a和b不相同就一定能确认犯人是c
//
// Created by Zhuang on 2024/5/25.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int a, b, c;
cin >> a >> b;
if (a == b)
cout << "-1\n";
else
cout << 6 - a - b << "\n";
return 0;
}
B. Piano 2
思路:
- 开一个bool数组记录原数组中出现的数
- 对原数组排序后,枚举判断相邻的数是否被bool数组记录过即可
//
// Created by Zhuang on 2024/5/25.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 110;
int b[N];
bool ba[300];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> c;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
ba[a[i]] = true;
c.emplace_back(a[i]);
}
for (int i = 0; i < m; i++)
cin >> b[i], c.emplace_back(b[i]);
sort(c.begin(), c.end());
int len = c.size();
// for (int i = 0; i < len; i++)
// {
// d2[c[i]] = i + 1;
// }
// int idx1 = 0, idx2 = 0;
/*for (int i = 0; i < len - 1; i++)
{
if (find(a.begin(), a.end(), c[i]) == a.end()) continue;
if(abs(d1[c[i]] - d1[c[i + 1]]) == 1) {
cout << "Yes\n";
return 0;
}
}*/
for (int i = 0; i < len; i++)
{
if (i == 0)
{
if (ba[c[i]] && ba[c[i + 1]])
{
cout << "Yes";
return 0;
}
}
else if (ba[c[i]] && ba[c[i - 1]])
{
cout << "Yes";
return 0;
}
}
cout << "No\n";
return 0;
}
C. Bingo 2
思路:
类似n皇后问题的处理方式,当需要记录一个数时,如果这个数没有被记录,则在一个 n * n 上的图中行、列、正对角线、副对角线都记录一边。最后判断有其中一个满足条件即可。
//
// Created by Zhuang on 2024/5/25.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 200001;
const int N = 1e4;
int han[N], ug[N], dg[N], lie[N];
set<int> vis;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, t, v;
cin >> n >> t;
for (int i = 1; i <= t; i++)
{
cin >> v;
int x = (v - 1) / n;
int y = (v - 1) % n;
if (!vis.count(v))
{
han[x]++;
lie[y]++;
ug[x + y]++;
dg[x - y + n]++;
if (han[x] == n || lie[y] == n || ug[x + y] == n || dg[x - y + n] == n)
{
cout << i << "\n";
return 0;
}
vis.insert(v);
}
}
cout << "-1\n";
return 0;
}
D. Intersecting Intervals
思路:
只需要分开维护左右两边端点,然后算出全部相交后区间,减去不相交的区间即可。
//
// Created by Zhuang on 2024/5/25.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++)
cin >> l[i] >> r[i];
sort(l.begin(), l.end());
sort(r.begin(), r.end());
int t = 0;
ll res = n * (n - 1ll) / 2;
for (auto &xx : l)
{
while (t < n && r[t] < xx) t++;
res -= t;
}
cout << res << "\n";
return 0;
}