AtCoder Beginner Contest 245

文章包含三道编程题目,第一题是关于比较两个人起床时间的,通过比较小时和分钟确定谁先起床;第二题是寻找给定序列中未出现的最小非负整数,即Mex问题;第三题考察动态规划,判断两个序列中选取元素能否保证相邻元素差的绝对值不超过k。
摘要由CSDN通过智能技术生成

A

Good morning

题意:

高桥A点过B分起床,赤木C点过D分起床(晚一秒,如果高桥和赤木时间一样高桥先起床)。求谁先起床。

题解:

先比较A,C的大小,C大说明赤木晚起。

如果A,C大小相同,那么再比较B,D如果D大于B说明赤木晚起。

代码:

#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2 * 1e5 + 10, inf = 1e9, mod = 998244353;
int n;
int a, b, c, d;
void solve()
{
	cin >> a >> b >> c >> d;
	if (a < c)
		cout << "Takahashi";
	else if (a == c)
		if (b <= d)
			cout << "Takahashi";
		else
			cout << "Aoki";
	else
		cout << "Aoki";
}

int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

B

Mex 

题意:

由N个数组成的序列判断其中未出现的最小的非负整数。

题解:
将序列中的数存入map中计为1,然后从0开始遍历,直到出现map未出现的数,输出这个数。

代码:

#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2 * 1e5 + 10, inf = 1e9, mod = 998244353;
int n;
int a[N];
map<int, int>m;
void solve()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		m[a[i]] = 1;
	}
	for (int i = 0;; i++)
	{
		if (m[i] == 0)
		{
			cout << i;
			return;
		}
	}
}

int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

C

Choose Elements 

题意:

给你两个序列A,B,判断序列X对于每个i,Xi=Ai or Bi,是否都能满足|Xi-Xi+1|<=k。 

题解:

这题可用动态规划来做。

首先我们用dp[1][i]=1来表示a[i]可用。

              用dp[0][i]=1来表示b[i]可用。

那么判断当前数字是否可用只需要判断当前数字与a[i-1],b[i-1]中可用的数字之差绝对值小于等于k。

如果当前数字可用如果为a[i]那么标记dp[1][i]=1,反之标记dp[0][i]=1。

注意在最开始时要注意要使dp[1][1],dp[0][1]初始化为1,因为第一个数字肯定是可以选的。

代码:

#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2 * 1e5 + 10, inf = 1e9, mod = 998244353;
int n, k;
int a[N], b[N];
int dp[2][N], p[N];
map<int, int>m;
void solve()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	dp[1][1] = 1; dp[0][1] = 1;
	for (int i = 2; i <= n; i++)
	{
		if ((dp[1][i - 1] && abs(a[i] - a[i - 1]) <= k) || (dp[0][i - 1] && abs(a[i] - b[i - 1]) <= k))
			dp[1][i] = 1;
		if ((dp[0][i - 1] && abs(b[i] - b[i - 1]) <= k) || (dp[1][i - 1] && abs(b[i] - a[i - 1]) <= k))
			dp[0][i] = 1;
	}
	if (dp[0][n] == 0 && dp[1][n] == 0)
		cout << "No";
	else
		cout << "Yes";
}

int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值