AtCoder Beginner Contest 285 A-F 题解

比赛链接

A - Edge Checker 2

判断 y == 2x || y == 2x + 1 即可。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int a, b;

int main() {
	
	scanf("%d %d", &a, &b);
	if(b == 2 * a || b == 2 * a + 1) puts("Yes");
	else puts("No");
	
	return 0;
}

B - Longest Uncommon Prefix

跟着题意来,枚举判断,然后输出就好。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 5005;
int n;
char a[N];

int main() {
	scanf("%d", &n);
	scanf("%s", a + 1);
	for(int i = 1; i < n; i ++) {
		int ans = 0;
		for(int j = 1; j + i <= n; j ++) {
			if(a[j] == a[i + j]) break;
			ans = j;
		}
		printf("%d\n", ans);
	}
	return 0;
}

C - abc285_brutmhyhiizp

26 进制转 10 进制(甚至不需要高精

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int N = 1e5;
int n;
char a[N];
ll ans;

int main() {
	scanf("%s", a + 1);
	n = strlen(a + 1);
	for(int i = 1; i <= n; i ++) {
		ans = ans * 26 + a[i] - 'A' + 1;
	}
	printf("%lld", ans);
	return 0;
}

D - Change Usernames

根据题意,符合条件等同于,在原名和改名之间建单向边构成一张图后,图中不存在环(实在理解不了画个图看)

因此,先“离散化”字符串,然后建边,跑一遍图找环就可。

具体看代码。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<map>
#include<vector>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
int n, in[N], cnt;
map< string, int > mp;
vector<int> G[N];
queue<int> q;
bool bfs() {
	int now, ver, num = 0;
	for(int i = 1; i <= cnt; i ++) {
		if(!in[i]) q.push(i), num ++;
	}
	while(!q.empty()) {
		now = q.front(), q.pop();
		for(int i = 0; i < G[now].size(); i ++) {
			ver = G[now][i], in[ver] --;
			if(!in[ver]) q.push(ver), num ++;
		}
	}
	return num == cnt;
}

string u, v;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++) {
		cin >> u >> v;
		if(!mp[u]) mp[u] = ++cnt;
		if(!mp[v]) mp[v] = ++cnt;
		G[mp[u]].push_back(mp[v]);
		in[mp[v]] ++;
	}
	if(bfs()) puts("Yes");
	else puts("No");
	return 0;
}

E - Work or Rest

定义 dp[i][j] 为确定了 i 天且已经连续工作 j 天的生产力最大值。

定义 g[i] 为假期间隔之间的生产力之和,

因此,可得转移方程:

  • dp[i][j] = max(dp[i][j], dp[i - 1][j - 1])
  • dp[i][0] = max(dp[i][0], dp[i - 1][j] + g[j])

优化后得:

dp[i] = max(dp[i], dp[j] +g[i - j - 1])

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<map>
#include<vector>
using namespace std;
#define ll long long
const int N = 5005;

int n;
ll dp[N], p[N], a[N];

int main() {
	
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
	for(int i = 1; i <= n; i ++) {
		p[i] = p[i - 1] + a[(i + 1) / 2];
	}
	for(int i = 1; i <= n; i ++) {
		for(int j = 0; j < n; j ++) {
			if(i - j - 1 < 0) break;
			dp[i] = max(dp[i], dp[j] + p[i - j - 1]);
		}
	}
	printf("%lld", dp[n]);
	return 0;
}

F - Substring of Sorted String

对每个字符建立树状数组维护

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<map>
#include<vector>
using namespace std;
#define ll long long
const int N = 1e5 + 5;

int n, q, tr[28][N];
char s[N];
int lowbit(int x) {
	return x & (-x);
}

void add(int c, int x, int v) {
	for(; x <= n; x += lowbit(x)) tr[c][x] += v;
}

int query(int c, int x) {
	int res = 0;
	for(; x; x -= lowbit(x)) res += tr[c][x];
	return res;
}

int main() {
	scanf("%d %s %d", &n, s + 1, &q);
	for(int i = 1; i <= n; i ++) add(s[i] - 'a', i, 1);
	for(int i = 1; i < n; i ++) {
		if(s[i] > s[i + 1]) add(27, i, 1);
	}
	int op, x, l, r, ans, tmp;
	char y;
	while(q --) {
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d %c", &x, &y);
			add(s[x] - 'a', x, -1), add(y - 'a', x, 1);
			if(x > 1 && s[x - 1] > s[x]) add(27, x - 1, -1);
			if(x < n && s[x] > s[x + 1]) add(27, x, -1);
			s[x] = y;
			if(x > 1 && s[x - 1] > s[x]) add(27, x - 1, 1);
			if(x < n && s[x] > s[x + 1]) add(27, x, 1);
		}
		else {
			scanf("%d %d", &l, &r);
			ans = 1;
			for(int i = s[l] - 'a' + 1; i <= s[r] - 'a' - 1; i ++) {
				tmp = query(i, r) - query(i, l - 1);
				ans &= (tmp == query(i, n));
				
			}
			if(ans && query(27, r - 1) == query(27, l - 1)) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}
posted @ 2023-02-24 23:02  Spring-Araki  阅读(24)  评论(0编辑  收藏  举报