今晚试了试一道很久以前不会的题,居然真做出来了。。
链接:牛客网竞赛
先找出两队的最大值,取其中最小的一个。即只选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图片纪念一下愉悦的心情——————