Atcoder Beginner Contest 374 . abc 题解

A - Takahashi san 2

Problem Statement

KEYENCE has a culture of addressing everyone with the suffix "-san," regardless of roles, age, or positions.

You are given a string SS consisting of lowercase English letters.
If SS ends with san, print Yes; otherwise, print No.

字符串问题 要求判断字符最后三个字母是否为 s a n

令 j = s.length(), 判断 j-3, j-2, j-1即可.

代码如下:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 7e7+10, inf = 2e9;
#define rep(i, a, n)  for(int i = a; i <= n; i++)
#define per(i, n, a)  for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int main()
{
	IOS
    string s;
    cin >> s;
    int k = s.length();
    if(s[k-3] == 's' && s[k-2] == 'a' && s[k-1] =='n' )
        cout << "Yes";
    else cout << "No";    
	return 0;
}

B - Unvarnished Report.

Problem Statement

KEYENCE has a culture of reporting things as they are, whether good or bad.
So we want to check whether the reported content is exactly the same as the original text.

You are given two strings SS and TT, consisting of lowercase English letters.
If SS and TT are equal, print 00; otherwise, print the position of the first character where they differ.
Here, if the ii-th character exists in only one of SS and TT, consider that the ii-th characters are different.

More precisely, if SS and TT are not equal, print the smallest integer ii satisfying one of the following conditions:

  • 1≤i≤∣S∣1≤i≤∣S∣, 1≤i≤∣T∣1≤i≤∣T∣, and Si≠TiSi​=Ti​.
  • ∣S∣<i≤∣T∣∣S∣<i≤∣T∣.
  • ∣T∣<i≤∣S∣∣T∣<i≤∣S∣.

Here, ∣S∣∣S∣ and ∣T∣∣T∣ denote the lengths of SS and TT, respectively, and SiSi​ and TiTi​ denote the ii-th characters of SS and TT, respectively.

字符串问题 要求判断两个字符是否一致 如果不一致 输出第一个不一致的字符

代码如下:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 7e7+10, inf = 2e9;
#define rep(i, a, n)  for(int i = a; i <= n; i++)
#define per(i, n, a)  for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int main()
{
	IOS
    string a, b;
    cin >> a >> b;
    int k = max(a.length(), b.length());
    int ok = 0, mark = 0;
    rep(i, 0, k-1)
    {
    	if(a[i] == b[i])  continue;
    	else {
    		ok = 1;
			mark = i;
    		break;
		}
	}
	if(ok == 0)  cout << "0";
	else cout << mark+1;
	return 0;
}

C - Separated Lunch .

Problem Statement

As KEYENCE headquarters have more and more workers, they decided to divide the departments in the headquarters into two groups and stagger their lunch breaks.

KEYENCE headquarters have NN departments, and the number of people in the ii-th department (1≤i≤N)(1≤i≤N) is KiKi​.

When assigning each department to Group AA or Group BB, having each group take lunch breaks at the same time, and ensuring that the lunch break times of Group AA and Group BB do not overlap, find the minimum possible value of the maximum number of people taking a lunch break at the same time.
In other words, find the minimum possible value of the larger of the following: the total number of people in departments assigned to Group AA, and the total number of people in departments assigned to Group BB.

问题;给出多组人数 要求尽可能重新分为两组 使得两组人数之差最小

思路:使用DFS求解 先上代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 2e9;
const ll N = 100000005;
#define rep(i, a, n)  for(int i = a; i <= n; i++)
#define per(i, n, a)  for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define int long long
int n, ans = inf, cnt, sum, now, a[N];
void dfs(int now, int cnt)
{
	if(now > n)
	{
		ans = min(ans, max(cnt, sum-cnt));
	    return ;
	}
	dfs(now+1, cnt);
	dfs(now+1, cnt+a[now]);  
}
signed main()
{
	IOS
    cin >> n;
    rep(i, 1, n)
    {
    	cin >> a[i];
    	sum += a[i];
	}
	dfs(0,0);
	cout << ans << endl;
	return 0;
}

用数组a存储初始时每一组的人数 用sum表示总人数 dfs中的两个参数分别表示已经遍历过的组和目前拿出的人数(没拿出的自动归入另一组), 即 对于每一组 只有两种可能 拿出或者不拿出

再已经获得的答案与这一轮更新的答案中取最小值即可。

附上另外一道题 来自洛谷

P2392 kkksc03考前临时抱佛脚

内容省略

四个科目 每个科目有n个作业 可同时处理一个科目的两个作业 仍然采用深度优先搜索

四个科目 采用四个for循环遍历 用二维数组存储每个科目与对应的作业量。

dfs()中的两个参数分别为组别和目前搜索到的题目

将左右脑分别取出 类似与分组的思想

对于每个科目下的作业 可以放入左脑 也可以放入右脑

当然也可以回溯

优化的方式是取左右脑max与当前已经获得的Min的最小值

define int long long
#define rep(i, a, n)  for(int i = a; i <= n; i++)
#define per(i, n, a)  for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int s[5], a[5][25], l, r, minn = inf, ans = 0;
void dfs(int paper, int mark)
{
	if(mark > s[paper])
	{
		minn = min(max(l, r), minn);
		return ;
	}
	l += a[paper][mark];
	dfs(paper, mark+1);
	l -= a[paper][mark];
	r += a[paper][mark];
	dfs(paper, mark+1);
	r -= a[paper][mark];
}
signed main()
{
	IOS
    rep(i, 1, 4)
        cin >> s[i];
    rep(i, 1, 4)
	    rep(j, 1, s[i])
		    cin >> a[i][j];
	rep(i, 1, 4)
	{
		l = 0;
		r = 0;
		dfs(i, 1);
		ans += minn;
		minn = inf;
	}
	cout << ans;		    
	return 0;
}

不要忘记void函数的return ;

AtCoder Beginner Contest 134 是一场 AtCoder 的入门级比赛,以下是每道题的简要题解: A - Dodecagon 题目描述:已知一个正十二边形的边长,求它的面积。 解题思路:正十二边形的内角为 $150^\circ$,因此可以将正十二边形拆分为 12 个等腰三角形,通过三角形面积公式计算面积即可。 B - Golden Apple 题目描述:有 $N$ 个苹果和 $D$ 个盘子,每个盘子最多可以装下 $2D+1$ 个苹果,求最少需要多少个盘子才能装下所有的苹果。 解题思路:每个盘子最多可以装下 $2D+1$ 个苹果,因此可以将苹果平均分配到每个盘子中,可以得到最少需要 $\lceil \frac{N}{2D+1} \rceil$ 个盘子。 C - Exception Handling 题目描述:给定一个长度为 $N$ 的整数序列 $a$,求除了第 $i$ 个数以外的最大值。 解题思路:可以使用两个变量 $m_1$ 和 $m_2$ 分别记录最大值和次大值。遍历整个序列,当当前数不是第 $i$ 个数时,更新最大值和次大值。因此,最后的结果应该是 $m_1$ 或 $m_2$ 中较小的一个。 D - Preparing Boxes 题目描述:有 $N$ 个盒子和 $M$ 个物品,第 $i$ 个盒子可以放入 $a_i$ 个物品,每个物品只能放在一个盒子中。现在需要将所有的物品放入盒子中,每次操作可以将一个盒子内的物品全部取出并分配到其他盒子中,求最少需要多少次操作才能完成任务。 解题思路:首先可以计算出所有盒子中物品的总数 $S$,然后判断是否存在一个盒子的物品数量大于 $\lceil \frac{S}{2} \rceil$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值