A 题
链接
https://atcoder.jp/contests/abc216/tasks/abc216_a。
题解
还是签到水题。
本题的难度在于,如何将一个类似
X.Y
\text{X.Y}
X.Y 格式数据区分开。该类格式还有:
X:Y
\text{X:Y}
X:Y 等。最简单的方法就是,将
X
\text{X}
X 和
Y
\text{Y}
Y 看成整数,中间的部分看成一个单字符。
AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
LL x,y;
char ch;
cin>>x>>ch>>y;
if (y<=2) {
cout<<x<<"-\n";
} else if (3<=y&&y<=6) {
cout<<x<<"\n";
} else if (7<=y&&y<=9) {
cout<<x<<"+\n";
}
return 0;
}
B 题
链接
https://atcoder.jp/contests/abc216/tasks/abc216_b。
题解
给
n
n
n 个人的姓和名,问这些人中是否有重名。
用数学的方法描述就是
1
≤
i
<
j
≤
n
,
S
i
=
S
j
,
and
T
i
=
T
j
1 \leq i < j \leq n,\ S_i = S_j, \text{and}\ T_i=T_j
1≤i<j≤n, Si=Sj,and Ti=Tj。
算法题目解决方法有很多,核心是要看
n
n
n 的大小。本题的
n
n
n 只有
1
,
000
1,000
1,000。最简单的办法就是暴力枚举,这样算法复杂度为
O
(
n
2
)
O(n^2)
O(n2)。最合适的办法是使用哈希表,这样时间复杂度为
O
(
n
)
O(n)
O(n)。
AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
LL n;
cin>>n;
string s;
getline(cin, s);
map<string, LL> M;
for (LL i=1; i<=n; i++) {
getline(cin, s);
if (M.count(s)>0) {
cout<<"Yes\n";
return 0;
}
M[s]++;
}
cout<<"No\n";
return 0;
}
C 题
链接
https://atcoder.jp/contests/abc216/tasks/abc216_c。
题解
本题只有两种操作:
- 操作 A。增加一个球。
- 操作 B。将现有球数量加倍。
由于本题的 n n n 非常大, 1 0 18 10^{18} 1018次方,本题没法使用 BFS 或者 DFS。
使用逆向倍增的思路。也就是说 n n n 为偶数,我们使用操作 B;如果 n n n 是奇数,我们使用操作 A。
AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
LL n;
cin>>n;
string ans = "";
while(n) {
if(n % 2 ==0){
n /= 2;
ans = 'B' + ans;
} else {
n --;
ans = 'A' + ans;
}
}
cout<<ans<<"\n";
return 0;
}