A - Past ABCs
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
if (s[0] != 'A' || s[1] != 'B' || s[2] != 'C') {
cout << "No" << endl;
return 0;
}
int n = (s[3] - '0') * 100 + (s[4] - '0') * 10 + (s[5] - '0');
if (n != 316 && n > 0 && n <= 349)cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
B - The bottom of the ninth
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < 9; i++) {
int x;
cin >> x;
sum1 += x;
}
for (int i = 0; i < 8; i++) {
int x;
cin >> x;
sum2 += x;
}
int sum = sum1+1;
cout << sum-sum2 << endl;
return 0;
}
C - Dentist Aoki
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n, q;
cin >> n >> q;
int sum = n;
bool vec[1005];
for (int i = 1; i < n + 2; i++)vec[i] = true;
for (int i = 0; i < q; i++) {
int x;
cin >> x;
if (vec[x] == true) {
vec[x] = false;
sum--;
}
else {
vec[x] = true;
sum++;
}
}
cout << sum << endl;
return 0;
}
D - Spot the Difference
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n;
cin >> n;
char a[105][105];
char b[105][105];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> b[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] != b[i][j]) {
cout << i << " " << j << endl;
return 0;
}
}
}
return 0;
}
E - Sort
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int>a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<pair<int, int>>operation;
for (int i = 1; i <= n; i++) {
while (a[i] != i) {
int correct_pos = a[i];
swap(a[i], a[correct_pos]);
operation.push_back({ i,correct_pos });
}
}
cout << operation.size() << endl;
for (const auto& op : operation) {
cout << op.first << " " << op.second << endl;
}
return 0;
}
注意:
我一开始一直在想怎么排序,然后使用了选择排序,于是超时了。后面发现,原来直接每个下标就是对应排序后的元素,是我观察不够仔细。
F - Merge the balls
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main()
{
int n;
cin >> n;
int count = n;
stack<int>stk;
vector<int>vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
for (int i = 0; i < n; i++) {
while (!stk.empty() && vec[i]==stk.top() ){
stk.pop();
vec[i]++;
}
stk.push(vec[i]);
}
cout << stk.size() << endl;
return 0;
}
G - New Friends
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 2e5 + 5;
vector<int>e[N];
int vis[N];
int main()
{
int n, m;
cin >> n >> m;
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
if (vis[i])continue;
queue<int>que;
que.push(i);
vis[i] = 1;
long long sumn = 1, summ = e[i].size();
while (!que.empty()) {
int t = que.front();
que.pop();
for (int w : e[t]) {
if (!vis[w]) {
vis[w] = 1;
que.push(w);
sumn++;
summ += e[w].size();
}
}
}
ans += sumn * (sumn - 1) - summ;
}
cout << ans / 2 << endl;
return 0;
}