GPA Involution

今晚试了试一道很久以前不会的题,居然真做出来了。。
链接:牛客网竞赛
先找出两队的最大值,取其中最小的一个。即只选A队 (或B队),则A题扣分为A队的最大值,B题的扣分为0。

再深搜更新一下答案。。深搜想了好久怎么实现。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#define int long long

using namespace std;

const int maxn = 2e5 + 5;

int a[maxn], b[maxn];
int  ans, n;

void dfs (int step, int sum, int ma, int mb)
{
	if (step == n + 1) {
		ans = min (ans, sum);
		//cout << sum << endl;
		return;
	}
	if (sum >= ans) return;
	dfs (step + 1, max (ma + mb, a[step] + mb ), max (ma, a[step]), mb ); //选A
	dfs (step + 1, max (ma + mb, ma + b[step] ), ma, max (mb, b[step]) );  //选B
}

signed main()
{
	scanf ("%lld", &n);
	int max_a = 0, max_b = 0;
	int id_a, id_b;
	for (int i = 1; i <= n; i++) {
		scanf ("%lld", &a[i]);
		max_a = max (max_a, a[i]);
	}
	for (int i = 1; i <= n; i++) {
		scanf ("%lld", &b[i]);
		max_b = max (max_b, b[i]);
	}
	ans = min (max_a, max_b);
	dfs (1, 0, 0, 0);
	cout << ans;
    return 0;
}

下面分享一波我自己想出来的数据:(答案应该是7)

5
10 9 8 7 5
1 1 2 1 11

这是学长的代码,用了排序和优先队列。
学长的神仙代码
学长的代码的思想应该就是,结构体储存数据后,从大到小的将A值排序。再从1到n+1循环,循环中不断将B值加入优先队列,如果某时有A值加上B最大值 (即前面的都选择了B,这次选A)小于当前答案,则更新最大值。

——————发个AC图片纪念一下愉悦的心情——————
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值