第十四届蓝桥杯 三国游戏

一开始的思路就是想着暴力,但是呢,如果真的用暴力一个一个列的话,连30%的数据都搞定不了,所以这里需要考虑别的办法。

这道题的思路就是贪心。

我们这样想:既然要满足至少一个国X>Y+Z,那么我们何不变成X-Y-Z>0呢?这样可能会更好想一点。我们就这样存储每一个事件发生之后的差,然后进行排序。

注意,这里的排序是最重要的一步,我们进行排序的目的就是为了找出最合适的选择的个数。

也就是说,我们从大的差值开始往下累加,这个时候和可能会变小,也可能会变大,取决于发生这件事之后的差值是如何的。所以,这就是一种贪心的思想。

注意,我们有三个国,所以要依次算出每个国赢的时候最多发生了多少个事件,所以需要比较3次,看看哪一个国赢的时候发生的事件最多。

上代码:

复制代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath> 
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<sstream>
#include<map>
#include<limits.h>
#include<set>
#define MAX 100010
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
LL n, m, counts, num;
LL a[MAX];
LL b[MAX];
LL c[MAX];
LL res = INT_MIN;
bool cmp(LL a, LL b)
{
    return a >= b;
}
LL solve(LL a[],LL b[], LL c[]) {
    LL cnt = 0;
    vector<LL>tmp;
    for (int i = 0; i < n; i++) {
        tmp.push_back(a[i] - b[i] - c[i]);//存储差值
    }
    sort(tmp.begin(), tmp.end(), cmp);//对各个事件之后的差值进行排序
    LL sum = 0;
    for (int i = 0; i < n; i++) {
        if (sum + tmp[i] > 0) {//从最大的差值开始累加,满足条件那么就是一个事件,cnt+1
            cnt++;
            sum += tmp[i];
        }
        else break;
    }
    return cnt;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 0; i < n; i++)
        cin >> b[i];
    for (int i = 0; i < n; i++)
        cin >> c[i];
    res = solve(a, b, c);//对于a赢时发生的最多事件
    res = max(res, solve(b, c, a));//b赢时发生的最多事件,后面两个的顺序是无所谓的。
    res = max(res, solve(c, b, a));//c赢时发生的最多事件。
    cout << res;
    return 0;
}
相关推荐
To_OC6 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
旖-旎7 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC7 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
delishcomcn8 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹8 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术9 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
河南花仙子科技10 小时前
花仙子科技详解:小游戏合规开发完整要点
科技·游戏
CV-Climber11 小时前
检索技术的实际应用
人工智能·算法
hhzz12 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型