Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)A ~ E题题解

A - Tiny Arithmetic Sequence

水题,判断3个数是否能构成等差数列

void solve() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a + b == 2 * c || a + c == 2 * b
	        || b + c == 2 * a)cout << "Yes
";
	else cout << "No
";
}

B - Do you know the second highest mountain?

排序,先按山脉高度排序,高度一样则按名字排序

struct node {
	string s; int t;
};
vector<node>v;
bool cmp(node a, node b) {
	if (a.t == b.t)return a.s > b.s;
	return a.t > b.t;
}
void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		string s; int t;
		cin >> s >> t;
		v.push_back({s, t});
	}
	sort(v.begin(), v.end(), cmp);
	cout << v[1].s << '
';
}

赛后发现自己写复杂化了

pair<int, string>a[1010];
void solve() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)cin >> a[i].second >> a[i].first;
	sort(a + 1, a + 1 + n);
	cout << a[n - 1].second << endl;
}

C - Secret Number

这道题,是检测 0000 ~ 9999 的每一个值,但我们可以通过高桥的字符串进行简化

void solve() {
	string s; cin >> s;
	int ans = 0;
	for (int i = 0; i <= 9999; ++i) {
		vector<bool> f(10);
		int x = i;
		for (int j = 0; j < 4; ++j) {
			f[x % 10] = true;
			x /= 10;
		}
		bool f2 = true;
		for (int j = 0; j < 10; ++j) {
			if (s[j] == 'o' and !f[j]) f2 = false;
			if (s[j] == 'x' and f[j]) f2 = false;
		}
		ans += f2;
	}
	cout << ans << '
';
}
S = input()
ans = 0
for i in range(10000):
    flag = [False]*10
    now = i
    for j in range(4):
        flag[now%10] = True
        now //= 10
    flag2 = True
    for j in range(10):
        if S[j] == 'o' and not flag[j]:
            flag2 = False
        if S[j] == 'x' and flag[j]:
            flag2 = False
    ans += flag2
print(ans)

D - Game in Momotetsu World

虽然正向搜索会很麻烦,但反过来从终点搜索起点使用DP记录即可

using ll = long long;
ll n, m, dp[2010][2010];
char s[2011][2011];
void solve() {
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf("%s", s[i] + 1);
	memset(dp, 63, sizeof(dp));
	dp[n][m] = 0;
	for (int i = n; i > 0; i--)
		for (int j = m; j > 0; j--) {
			if (i == n && j == m)
				continue;
			dp[i][j] = max((s[i + 1][j] == '+' ? 1 : -1) - dp[i + 1][j],
			               (s[i][j + 1] == '+' ? 1 : -1) - dp[i][j + 1]);
		}
	if (dp[1][1] > 0)cout << "Takahashi
";
	else if (dp[1][1] == 0)cout << "Draw
";
	else cout << "Aoki
";
}

E - Xor Distances

#include<bits/stdc++.h>
#define int long long
#define N 200005
#define MOD 1000000007
using namespace std;
int n, d[N], ans;
vector<int> to[N], w[N];
void add(int u, int v, int wt) {to[u].push_back(v), w[u].push_back(wt);}
void dfs(int u, int fa) {
	for (int i = 0, v; i < to[u].size(); i++)
		if ((v = to[u][i]) != fa)d[v] = d[u] ^ w[u][i], dfs(v, u);
}
signed main() {
	cin >> n;
	for (int i = 1, u, v, wt; i < n;
	        i++)scanf("%lld%lld%lld", &u, &v, &wt), add(u, v, wt), add(v, u, wt);
	dfs(1, 0);
	for (int k = 0; k < 60; k++) {
		int a = 0;
		for (int i = 1; i <= n; i++)a += ((d[i] >> k) & 1);
		ans = (ans + a * (n - a) % MOD * ((1ll << k) % MOD) % MOD) % MOD;
	}
	cout << ans << endl;
	return 0;
}

F - Insertion Sort

待补

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/14773656.html