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\n";
	else cout << "No\n";
}

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 << '\n';
}

赛后发现自己写复杂化了

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 << '\n';
}
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\n";
	else if (dp[1][1] == 0)cout << "Draw\n";
	else cout << "Aoki\n";
}

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

待补

posted @   RioTian  阅读(135)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战
历史上的今天:
2020-05-16 路径规划之 A* 算法
点击右上角即可分享
微信分享提示

📖目录