蓝桥杯真题讲解:三国游戏(贪心)
- 一、视频讲解
- 二、正解代码
一、视频讲解
蓝桥杯真题讲解:三国游戏(贪心)
二、正解代码
//三国游戏:贪心
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;
int n;int cal_ans(vector<int>&a, vector<int>&b, vector<int>&c)
{vector<int>tmp(n);int sum = 0;for(int i = 0; i < n; i ++)tmp[i] = a[i] - (b[i] + c[i]);sort(tmp.begin(), tmp.end());int cnt = 0;for(int i = n - 1; i >= 0; i --){if(sum + tmp[i] > 0){sum += tmp[i];cnt ++;}elsebreak;}return cnt;
}void solve()
{cin >> n;vector<int>a(n), b(n), c(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 ans = 0;ans = cal_ans(a, b, c);//x赢ans = max(ans, cal_ans(b, a, c));//y赢ans = max(ans, cal_ans(c, a, b));//z赢if(ans)cout << ans << endl;elsecout << -1 << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;//cin >> t;while(t--)solve();
}