Toyota Programming Contest 2023 Spring Qual A(AtCoder Beginner Contest 288)[A - F]

原比赛链接

A - Many A+B Problems

A - AC代码

#include <bits/stdc++.h>
using namespace std;
int t, a, b;
int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * w;
}
void write(int res) {
	if (res < 0) {
		putchar('-');
		res = -res;
	}
	if (res >= 10)
		write(res / 10);
	putchar(res % 10 + '0');
}
void solve() {
	a = read(), b = read();
	write(a + b);
	printf("\n");
}
int main() {
	t = read();
	while (t--)
		solve();
	return 0;
}

B - Qualification Contest

B - AC代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
string str[N];
int n, k;
void solve() {
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		cin >> str[i];
	sort(str, str + k);
	for (int i = 0; i < k; i++)
		cout << str[i] << endl;
	return;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	solve();
	return 0;
}

C - Don’t be cycle

C - AC代码

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int fa[N];
int find(int x) {
	if (fa[x] == x)
		return x;
	return fa[x] = find(fa[x]);
}
void merge(int x, int y) {
	int fx = find(x);
	int fy = find(y);
	if (fx != fy)
		fa[fx] = fy;
}
int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * w;
}
void write(int res) {
	if (res < 0) {
		putchar('-');
		res = -res;
	}
	if (res >= 10)
		write(res / 10);
	putchar(res % 10 + '0');
}
int n, m, a, b, res;
void solve() {
	n = read(), m = read();
	for (int i = 1; i <= n; i++)
		fa[i] = i;
	for (int i = 1; i <= m; i++) {
		a = read(), b = read();
		if (find(a) == find(b))
			res++;
		else
			merge(a, b);
	}
	write(res);
}
int main() {
	solve();
	return 0;
}

D - Range Add Query

D - AC代码

#include <bits/stdc++.h>
using namespace std;
int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * w;
}
int n, k, l, r, q;
void solve() {
	n = read(), k = read();
	vector <int> a(n);
	for (int i = 0; i < n; i++)
		a[i] = read();
	vector <vector <int> > d(k, vector <int> (n + 1));
	for (int i = 0; i < n; i++)
		d[i % k][i + 1] = a[i];
	for (int i = 0; i < k; i++)
		for (int j = 0; j < n; j++)
			d[i][j + 1] += d[i][j];
	cin >> q;
	while (q--) {
		cin >> l >> r;
		--l;
		vector <int> newd(k);
		for (int i = 0; i < k; i++)
			newd[i] = d[i][r] - d[i][l];
		sort(newd.begin(), newd.end());
		if (newd[0] == newd.back()) 
			puts("Yes");
		else 
			puts("No");
	}
}
int main() {
	solve();
	return 0;
}

E - Wish List

E - AC代码

#include <bits/stdc++.h>
#define int long long
#define inf 1e18
using namespace std;
const int N = 2e5 + 10, M = 5e3 + 10;
int dp[M][M];
int a[N], c[N], buy[N];
int n, k, res = inf;
int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * w;
}
void write(int res) {
	if (res < 0) {
		putchar('-');
		res = -res;
	}
	if (res >= 10)
		write(res / 10);
	putchar(res % 10 + '0');
}
void solve() {
	n = read(), k = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i <= n; i++)
		c[i] = read();
	for (int i = 1; i <= k; i++) {
		int temp = read();
		buy[temp] = 1;
	}
	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= n; j++)
			dp[i][j] = inf;
	dp[0][0] = 0;
	for (int i = 1; i <= n; i++) {
		int cst = inf;
		if (buy[i] == 0)
			dp[i][0] = dp[i - 1][0];
		for (int j = 1; j <= i; j++) {
			cst = min(cst, c[i - j + 1]);
			dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + cst + a[i]);
			if (buy[i] == 0)
				dp[i][j] = min(dp[i][j], dp[i - 1][j]);
		}
	}
	for (int i = k; i <= n; i++)
		res = min(res, dp[n][i]);
	write(res);
	return;
}
signed main() {
	solve();
	return 0;
}

F - Integer Division

F - AC代码

#include <bits/stdc++.h>
#define int long long
#define mod 998244353
using namespace std;
const int N = 2e5 + 10;
char c[N];
int len, ans, presum;
int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * w;
}
void write(int res) {
	if (res < 0) {
		putchar('-');
		res = -res;
	}
	if (res >= 10)
		write(res / 10);
	putchar(res % 10 + '0');
}
void solve() {
	len = read(), scanf("%s", c + 1);
	presum = 1;
	for (int i = 1; i <= len; i++) {
		int val = c[i] - '0';
		ans = (ans * 10 + presum * val) % mod;
		presum = (presum + ans) % mod;
	}
	write(ans);
	printf("\n");
	return;
}
signed main() {
	solve();
	return 0;
}

给个赞吧!

posted @ 2023-02-18 15:29  煎饼Li  阅读(69)  评论(0编辑  收藏  举报