ABC 307 A-G题解

不会写题解,只有AC代码

链接:東京海上日動プログラミングコンテスト2023(AtCoder Beginner Contest 307) - AtCoder

A - Weekly Records

题意:

题目第一行给出N周,第二行给出每天的步数,分别输出每周的总步数

题解:

水题

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const LL N = 2e5 + 10;
int a[N];
void solve()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		int s = 0;
		for (int j = 1, x; j <= 7; ++j)
		{
			scanf("%d", &x);
			s += x;
		}
		if (i != 1)printf(" ");
		printf("%d", s);
	}
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

B - racecar

题意:

给出N个字符串,问是否存在两个不同的i,j字符串首尾相连之后成为一个回文串

题解:

题目数据范围很小,可以直接枚举所有i,j拼接后暴力判断回文,时间复杂度O(N^2*|S|)

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const LL N = 2e5 + 10;
char s[110][60];
int len[110];
bool cheak(int x, int y)
{
	char ch[110];
	for (int i = 1; i <= len[x]; ++i)
		ch[i] = s[x][i];
	for (int i = 1; i <= len[y]; ++i)
		ch[i + len[x]] = s[y][i];
	for (int i = 1, j = len[x] + len[y]; i < j; ++i, --j)
		if (ch[i] != ch[j])return 0;
	return 1;
}
void solve()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", s[i] + 1);
		len[i] = strlen(s[i] + 1);
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)if (i != j)
		{
			if (cheak(i, j))
			{
				printf("Yes\n");
				return;
			}
		}
	}
	printf("No\n");
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

C不会捏

D - Mismatched Parentheses

题意:

给出一个字符串,删除其中所有合法括号的内容(看样例挺容易看出来的)

题解:

用栈来处理。遍历字符串,遇到左括存入栈中;遇到右括号时,若栈为空,则该右括号无法形成合法的括号,跳过,若栈非空,则栈顶元素与该右括号形成一对合法括号,去处栈顶的左括号并在左括号的位置标记其对应的右括号的位置以便在输出时跳过括号

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const LL N = 2e5 + 10;
int n, p[N];
char ch[N];
stack<int>st;
void solve()
{
	scanf("%d%s", &n, ch + 1);
	for (int i = 1; i <= n; ++i)
	{
		if (ch[i] == '(')st.push(i);
		else if (ch[i] == ')'&&st.size())
		{
			p[st.top()] = i; st.pop();
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		if (p[i])i = p[i];
		else printf("%c", ch[i]);
	}
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

E - Distinct Adjacent

题意:

求长度为N,由大小在1到M之间的数字组成的,相邻元素两两不相等的 环

题解:

DP。如果不是环,那从左往右遍历时每个元素的取值只需要考虑与前一个元素值不同。对于是环的情况,那要多考虑最后一个元素必须与第一个元素不同。可以用dp分别求对于每个位置上与第一位相同和与第一位不同两种方案的总方案数,最终答案就是最后一位与第一位不同的总方案数。

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const LL N = 1e6 + 10, MOD = 998244353;
LL dp[N][2];
//dp[i][1/0]第i位的选择与第一位相同/不同的方案数
void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	dp[1][1] = m;
	for (int i = 2; i <= n; ++i)
	{
		//设第一位位a
		//若当前位选择a,那方案数就是前一位不选择a的方案数
		dp[i][1] = dp[i - 1][0];
		//若当前位不选a,那方案数有两种
		//前一位不选a,那当前位即不能是a也不能是前一位,这一位有m-2种选法
		//前一位选了a,那当前位只需要不是a,这一位有m-1种选法
		dp[i][0] = (dp[i - 1][0] * (m - 2) + dp[i - 1][1] * (m - 1)) % MOD;
	}
	//最终答案为最后一位与第一位不同的方案数
	printf("%lld", dp[n][0]);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

F - Virus 2

题意:

有一个N个元素(人),M条边的无向带权图,第0天有K个感染者,在1到D天每天与感染者距离在Xi内的人将会被感染,求每个人被感染的时间(第几天),D天后未被感染则输出-1(具体看题)

题解:

用优先队列贪心。有点抽象...代码有点缝缝补补的更抽象了...(懒得写题解了)

应该是本蒟蒻的做法太抽象了...

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const LL N = 3e5 + 10, MOD = 998244353;
int ans[N];
queue<PII>inque;
priority_queue<PII, vector<PII>, greater<PII> >q, e[N];
priority_queue<PII>qu;
void solve()
{
	int n, m, k, d;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i)
		ans[i] = -1;
	for (int i = 1; i <= m; ++i)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		e[u].push({ w,v });
		e[v].push({ w,u });
	}
	scanf("%d", &k);
	for (int i = 1, x; i <= k; ++i)
	{
		scanf("%d", &x);
		ans[x] = 0;
		while (e[x].size())
		{
			q.push(e[x].top());
			e[x].pop();
		}
	}
	scanf("%d", &d);
	for (int i = 1, x; i <= d; ++i)
	{
		scanf("%d", &x);
		while (q.size() && q.top().first <= x)
		{
			qu.push({ x - q.top().first, q.top().second });
			q.pop();
		}
		while (qu.size())
		{
			int u = qu.top().second, w = qu.top().first;
			qu.pop();
			if (ans[u] != -1)continue;
			ans[u] = i;
			while (e[u].size() && e[u].top().first <= w)
			{
				qu.push({ w - e[u].top().first,e[u].top().second });
				e[u].pop();
			}
			while (e[u].size())
			{
				inque.push(e[u].top());
				e[u].pop();
			}
		}
		while (inque.size())
		{
			q.push(inque.front());
			inque.pop();
		}
	}
	for (int i = 1; i <= n; ++i)
		printf("%d\n", ans[i]);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

G - Approximate Equalization

题意:

给出一个长N的数组,给出两种操作:1、使Ai加一,使Ai+1减一 2、使Ai减一,使Ai+1加一。求使对于任意i,j |Ai-Aj|<=1的最小操作数

题解:

DP。设avg为平均值向下取整,最终数组会有m个avg+1,n-m个avg(因为要求|Ai-Aj|<=1,所以数组最终的操作结果一定是这种情况)

dp状态表示dp[i][j]表示前i位有j个avg+1,转移见代码(若更改前i位时仅对前i+1位更改过,那更改前i位之后第i+1位为多少就能O(1)时间计算出来),最终答案为dp[n][m]

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const LL N = 5e3 + 10, MOD = 998244353;
LL n, a[N], s[N], dp[N][N], avg, m;
LL fun(LL i, LL j)//求前i位有j个avg+1时,第i+1位的大小
{
	return a[i + 1] - (i * avg + j) + s[i];
}
void solve()
{
	scanf("%lld", &n);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%lld", &a[i]);
		s[i] = s[i - 1] + a[i];
	}
	avg = s[n] / n, m = (s[n] % n + n) % n;
	if (m && avg <= 0)avg--;//负数情况手动向下取整
	//最终数组中有n-m个avg,m个avg+1
	//求前i位有j个avg+1的情况下的最小花费
	memset(dp, 0x3f, sizeof dp);
	dp[0][0] = 0;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 0; j <= min((LL)i, m); ++j)
		{
			if (j < i)//第i位为avg
				dp[i][j] = min(dp[i][j], dp[i - 1][j] + abs(fun(i - 1, j) - avg));
			if (j > 0)//第i位为avg+1
				dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + abs(fun(i - 1, j - 1) - avg - 1));
			//printf("dp[%d][%d]=%lld\n", i, j, dp[i][j]);
		}
	}
	printf("%lld", dp[n][m]);
}
int main()
{
	int T = 1;
	//scanf("%d", &T);
	while (T--)
	{
		solve();
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值