Codeforces Round #695 (Div. 2) C. Three Bags (贪心,思维)

- 题意:有三个背包,每个背包里都用一些数字,你可以选择某一个背包的数字\(a\),从另外一个背包拿出\(b\)(拿出之后就没有了),然后将\(a\)替换为\(a-b\),你可以进行任意次这样的操作,使得最后只剩下一个数,问这个数最大能是多少.
- 题解:我的思路是,先任意选两个背包,假设\(x\)和\(y\),我们假设选\(x\)中的最大值,\(y\)中的最小值,很明显,我们让\(y\)的最小值减去没有选的背包的所有数和除了\(x\)最大值的所有数一定是最优的,但是\(y\)中除了最小值的其他数不好处理,这里我们可以先让\(x\)中的最小值减去\(y\)除了最小值的其他数,因为小的减大的可以使贡献损失最小,这里要注意,假如减完后,\(x\)的最小值是正数,那么我们让\(y\)的最小值直接减去\(x\)操作后最小值即可,如果是负数的话,我们就要将这个数减到没有用过的那个背包里面,然后再用\(y\)的最小值减去没有选的背包的所有数,其实这里我们只要加一个绝对值就可以了,最后的最大值就是用\(x\)中的最大值减去操作后的\(y\)中的最小值.因为只有三个背包,所以一共有\(6\)种情况,我们一一列举维护一个最大值就好了.
- 代码:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
int n1,n2,n3;
ll s1,s2,s3;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n1>>n2>>n3;
vector<ll> a(n1),b(n2),c(n3);
for(auto &w:a) cin>>w,s1+=w;
for(auto &w:b) cin>>w,s2+=w;
for(auto &w:c) cin>>w,s3+=w;
n1--,n2--,n3--;
sort(a.begin(),a.end());
sort(b.begin(),b.end());
sort(c.begin(),c.end());
ll ans=0;
//max_1,min_2
ll sum1=s1-a[n1]-a[0];
ll sum2=s2-b[0];
ll sum3=s3;
ans=max(ans,a[n1]-(b[0]-sum3-sum1-abs(a[0]-sum2)));
//max_1,min_3
sum1=s1-a[n1]-a[0];
sum2=s2;
sum3=s3-c[0];
ans=max(ans,a[n1]-(c[0]-sum2-sum1-abs(a[0]-sum3)));
//max_2,min_1
sum1=s1-a[0];
sum2=s2-b[n2]-b[0];
sum3=s3;
ans=max(ans,b[n2]-(a[0]-sum3-sum2-abs(b[0]-sum1)));
//max_2,min_3
sum1=s1;
sum2=s2-b[n2]-b[0];
sum3=s3-c[0];
ans=max(ans,b[n2]-(c[0]-sum1-sum2-abs(b[0]-sum3)));
//max_3,min_1
sum1=s1-a[0];
sum2=s2;
sum3=s3-c[n3]-c[0];
ans=max(ans,c[n3]-(a[0]-sum2-sum3-abs(c[0]-sum1)));
//max_3,min_2
sum1=s1;
sum2=s2-b[0];
sum3=s3-c[n3]-c[0];
ans=max(ans,c[n3]-(b[0]-sum1-sum3-abs(c[0]-sum2)));
cout<<ans<<'\n';
return 0;
}
Codeforces Round #695 (Div. 2) C. Three Bags (贪心,思维)的更多相关文章
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- Codeforces Round #695 (Div. 2)
比赛地址 A (水题) 题目链接 题目: 给出\(n\)个面板,每个面板初始时间相同,每过1s面板上数字会加1(数字在\(0\sim9\)循环播放),任意时刻选择一个面板\(x\)使他的时间停止,其他 ...
- Codeforces Round #180 (Div. 2) D. Fish Weight 贪心
D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...
- Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心
A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...
- Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心
C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- Codeforces Round #548 (Div. 2) F splay(新坑) + 思维
https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\), ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...
随机推荐
- tf.argmax(vector,axis)函数的使用
1.返回值 vector为向量,返回行或列的最大值的索引号: vector为矩阵,返回值是向量,返回每行或每列的最大值的索引号. 2.参数 vector为向量或者矩阵 axis = 0 或1 0:返回 ...
- LeetCode876 链表的中间结点
给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4 ...
- python3实现计算器
实验内容 1.简单计算器的设计 请设计简单的"加减乘除"计算器并从键盘上输入数据进行计算 数字的加减乘除,input返回的结果是str类型的,通过截取字符串中的运算符,来提取数字, ...
- 区间合并 C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...
- FAT32、NTFS、exFAT有什么区别?
文件系统 我们经常会对电脑硬盘.U盘.移动硬盘进行格式化,而在格式化硬盘的时候会弹出文件系统的选项,分别有FAT32.NTFS.exFAT三种格式,那么FAT32.NTFS.exFAT有什么区别? 在 ...
- windows下使用mingw和msvc静态编译Qt5.15.xx
windows下使用mingw和msvc静态编译Qt5.15.xx 下载并安装相关依赖软件 Python version 2.7 https://www.python.org/downloads/ ( ...
- win10/windows 安装Pytorch
https://pytorch.org/get-started/locally/ 去官网,选择你需要的版本. 把 pip install torch==1.5.0+cu101 torchvision= ...
- 【Problems】Could not set property 'id' of 'xxx' with value '' Cause argument type mismatch
一个问题:向comment表添加记录时,报错, 无法设置值. reflection.ReflectionException: Could not set property 'id' of 'class ...
- js 前端词典对象的属性和值读取
通常服务端返回比较奇葩的数据对象,不知道该怎么将这个对象转换为可用实体,想了很久,突发奇想想到了这么个方法. 需求是这样:企业有多个产品,产品有分为很几个种类.服务端有获取产品的接口,和单独获取产品种 ...
- b站视频_下载_去水印_视频转mp4-批量下载神器
b站下载_视频_去水印_转mp4_批量下载的解决办法 以下问题均可解决 b站下载的视频如何保存到本地 b站下载的视频在那个文件夹里 b站下载视频转mp4 b站下载app b站下载在哪 b站下载视频电脑 ...