CF1920B Summation Game
题目传送门
题面
Alice and Bob are playing a game. They have an array- First, Alice will remove at most
elements from the array. - Second, Bob will multiply at most
elements of the array by .
Alice wants to maximize the sum of elements of the array while Bob wants to minimize it. Find the sum of elements of the array after the game if both players play optimally.
Input
Each test consists of multiple test cases. The first line contains a single integer
The first line of each test case contains three integers
The second line of each test case contains
It is guaranteed that the sum of
Output
For each test case, output a single integer — the sum of elements of the array after the game if both players play optimally.
题目大意
爱丽丝和鲍勃正在玩一个游戏。他们有一个数组
- 首先,爱丽丝将从数组中移除最多
个元素。 - 第二步,鲍勃将数组中最多
个元素乘以 。
爱丽丝希望最大化数组元素之和,而鲍勃希望最小化数组元素之和。如果双方都以最佳方式进行游戏,请找出游戏结束后数组元素的总和。
思路
鲍勃肯定会选择更大的数进行乘以
代码
#include <bits/stdc++.h> #define int long long #define endl '\n' using namespace std; void solve() { int n, k, x; cin >> n >> k >> x; vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 排序 vector<int> b(n + 1, 0); for (int i = 1; i <= n; i++) { b[i] = b[i - 1] + a[i]; // 记录前缀和 } int ans = LLONG_MIN; // 答案初始化为一个很小的数 for (int i = n; i >= n - k; i--) // 枚举爱丽丝移除的数的数量 { ans = max(ans, b[i] - 2 * (b[i] - b[max(0LL, i - x)])); } cout << ans << endl; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】