首页 > 其他分享 >AtCoder Beginner Contest 285 A-F 题解

AtCoder Beginner Contest 285 A-F 题解

时间:2023-02-24 23:12:00浏览次数:45  
标签:AtCoder int 题解 scanf long add include 285 dp

比赛链接

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;
}

标签:AtCoder,int,题解,scanf,long,add,include,285,dp
From: https://www.cnblogs.com/Spring-Araki/p/17153495.html

相关文章

  • AtCoder Beginner Contest 283 A-F 题解
    A-Power快速幂板子点击查看代码#include<cstdio>#include<algorithm>usingnamespacestd;#defineintlonglongintn,m;intqpow(inta,intb){ intr......
  • CF837D题解
    CF837D题解没有用的话今天晚上在CF题库里随便选题选的,感觉还不错的题。昨天就开始停自习了,但是一直在摆烂(悲,自从上个学期就这样了,非常怀念一年前的机房,风清气正,大家都......
  • THUPC2019 令人难以忘记的题目名称 题解
    首先感性感觉这个东西是比较有循环节的,但这是后话。最后几步一定是差分相同->每个数相同->全为0。不平凡地猜想差分\(k\)次全\(0\)等价于可以\(k\)步胜利。假设......
  • Universial Cup 题解
    开始瞎做题了全部都写了简要题意,题解默认折叠,可以来做做(?UniversialCup官网The1stUniversalCup比赛名称&题解链接题解包含题目QOJlinkCodeforcesGymLin......
  • 题解 北大集训2018 点分治
    题意给定\(n\)个点的树,求点分治方案数,对\(10^9+7\)取模。两种方案不同当且仅当点分树不同。\(1\leqn\leq5000\)题解考虑怎样合并两个点分治方案。如果我们有......
  • history 题解(并查集)
    考虑使用边带权并查集维护点之间的连通性,边权为这条边建立的时间,查询时如果查询时间小于边权则不能通行(巧妙地处理了时间的性质)。由于时间这种东西性质特殊无法路径压缩,所......
  • 新版 Mac M2 安装老 saas 项目 报 Gem sass is not installed 问题解决
     换了新电脑,需要把老项目git拉下来再跑起来的时候发现生成样式文件的时候会报这个错误,(N年前老项目,用的是node-sass,[email protected]版本比较老旧,但项目还是要......
  • vue——更改路由模式为history后,刷新页面显示Cannot Get/空白/404,本地icon图标不显示
    参考:https://blog.csdn.net/william_jzy/article/details/106526339   https://www.bbsmax.com/A/A7zgKEVkz4/      https://router.vuejs.org/zh/gu......
  • CF1131B 题解
    题目传送门好水的绿题,当放松了。题目分析为了方便表述,定义\(lsta,lstb,nowa,nowb\),分别表示上次双方的得分以及当前的得分。考虑分讨,若\(lsta=lstb\),贡献即\(\min(n......
  • P6666 [清华集训2016] 数据交互 题解
    ##P6666[清华集训2016]数据交互题解###简要题意:n个点的树,m次操作,分别为添加一条路径$(u_i,v_i,w_i)$,和撤消一条路径,每一次操作后求出一条路径使得与这条路径有交的......