第十四届蓝桥杯省赛C++C组C题【三国游戏】题解(AC)





解题思路

由于三种国家都有获胜的可能,所以我们需要分别枚举 X , Y , Z X,Y,Z X,Y,Z 获胜的情况。

设 X X X 获胜,那么对于第 i i i 个事件的贡献为 a [ i ] − ( b [ i ] + c [ i ] ) a[i]-(b[i]+c[i]) a[i]−(b[i]+c[i]),根据贪心的策略,我们可以考虑将所有时间按照贡献进行排序,贡献越大则优先选择。

Y , Z Y,Z Y,Z 获胜同理。

注意
  • X X X 的获胜条件为 X > Y + Z X > Y + Z X>Y+Z,非 X ≥ Y + Z X \geq Y + Z X≥Y+Z。
  • 无获胜国时,输出 -1

AC_Code

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n;
int a[N], b[N], c[N], d[N];

int get(int a[], int b[], int c[])
{
    for (int i = 0; i < n; ++ i )
        d[i] = a[i] - b[i] - c[i];
    
    sort(d, d + n, greater<int>());
    
    LL res = 0;
    for (int i = 0; i < n; ++ i )
    {
        res += d[i];
        if (res <= 0)
            return i;
    }
    
    return n;
}

int main()
{
    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];
    
    int res = max({get(a, b, c), get(b, a, c), get(c, a, b)});
    
    if (res == 0)
        res = -1;
    
    cout << res << endl;
    
    return 0;
}

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n;
struct Node
{
    int x, y, z;
}q[N];

bool cmp_x(Node a, Node b)
{
    return a.x - a.y - a.z > b.x - b.y - b.z;
}

bool cmp_y(Node a, Node b)
{
    return a.y - a.x - a.z > b.y - b.x - b.z;
}

bool cmp_z(Node a, Node b)
{
    return a.z - a.x - a.y > b.z - b.x - b.y;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++ i )
        cin >> q[i].x;
    for (int i = 0; i < n; ++ i )
        cin >> q[i].y;
    for (int i = 0; i < n; ++ i )
        cin >> q[i].z;
    
    LL res = -1, sum = 0, t = 0;
    sort(q, q + n, cmp_x);
    for (int i = 0; i < n; ++ i )
    {
        sum += q[i].x - q[i].y - q[i].z;
        if (sum <= 0)
            break;
        t ++;
    }
    res = max(res, t);
    
    sum = 0, t = 0;
    sort(q, q + n, cmp_y);
    for (int i = 0; i < n; ++ i )
    {
        sum += q[i].y - q[i].x - q[i].z;
        if (sum <= 0)
            break;
        t ++;
    }
    res = max(res, t);
    
    sum = 0, t = 0;
    sort(q, q + n, cmp_z);
    for (int i = 0; i < n; ++ i )
    {
        sum += q[i].z - q[i].x - q[i].y;
        if (sum <= 0)
            break;
        t ++;
    }
    res = max(res, t);
    
    cout << (!res? -1: res) << endl;
    
    return 0;
}

【在线测评】

相关推荐
XXYBMOOO20 分钟前
理解 C++ 中的字节序转换函数 `swapEndian`
开发语言·arm开发·c++
毕加锁28 分钟前
深度解析昇腾Catlass:C++模板元编程与高性能算子开发范式(1)
开发语言·c++
你好音视频1 小时前
FFmpeg FLV编码器原理深度解析
c++·ffmpeg·音视频
Qt学视觉1 小时前
PaddlePaddle-2wget下载安装
c++·人工智能·paddlepaddle
老秦包你会1 小时前
C++进阶------C++的类型转换
java·开发语言·c++
superman超哥1 小时前
仓颉性能瓶颈定位方法深度解析
c语言·开发语言·c++·python·仓颉
leaves falling1 小时前
c语言-static和extern
c语言·开发语言
黎雁·泠崖1 小时前
C 语言的内存函数:memcpy/memmove/memset/memcmp 精讲(含模拟实现)
c语言·开发语言
HUST1 小时前
C 语言 第八讲:VS实用调试技巧
运维·c语言·开发语言·数据结构·算法·c#
历程里程碑2 小时前
LeetCode128:哈希集合巧解最长连续序列
开发语言·数据结构·c++·算法·leetcode·哈希算法·散列表