第十四届蓝桥杯省赛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;
}

【在线测评】

相关推荐
一匹电信狗5 分钟前
【LeetCode_876_2.02】快慢指针在链表中的简单应用
c语言·数据结构·c++·算法·leetcode·链表·stl
keineahnung23456 分钟前
C++中的Aggregate initialization
c++·1024程序员节
胖咕噜的稞达鸭9 分钟前
算法入门---专题二:滑动窗口2(最大连续1的个数,无重复字符的最长子串 )
c语言·数据结构·c++·算法·推荐算法·1024程序员节
Yupureki14 分钟前
从零开始的C++学习生活 15:哈希表的使用和封装unordered_map/set
c语言·数据结构·c++·学习·visual studio·1024程序员节
我是华为OD~HR~栗栗呀18 分钟前
华为OD-Java面经-21届考研
java·c++·后端·python·华为od·华为·面试
努力学习的小廉1 小时前
我爱学算法之—— 分治-归并
c++·算法·1024程序员节
仰泳的熊猫1 小时前
LeetCode:200. 岛屿数量
数据结构·c++·算法·leetcode
sulikey1 小时前
Qt 入门简洁笔记:从框架概念到开发环境搭建
开发语言·前端·c++·qt·前端框架·visual studio·qt框架
傻童:CPU1 小时前
C语言需要掌握的基础知识点之链表
c语言·1024程序员节
zzzsde1 小时前
【C++】stack和queue:优先级队列的使用及底层原理
开发语言·c++