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 ...
随机推荐
- 白日梦的Elasticsearch笔记(一)基础篇
目录 一.导读 1.1.认识ES 1.2.安装.启动ES.Kibana.IK分词器 二.核心概念 2.1.Near Realtime (NRT) 2.2.Cluster 2.3.Node 2.4.In ...
- TCP连接的建立与释放(超详细)
前言:在计算机网络协议中,TCP只是其中一个,然而在网络使用中,TCP也是最离不开的协议之一,它的重要性毋庸置疑,最最重要的是,面试的重点就是它啊,呜呜~~,今天我们一起来看下TCP的连接建立与释放, ...
- Mybatis 报错java.sql.SQLException: No suitable driver found for http://www.example.com
运行项目报错 Error querying database. Cause: java.sql.SQLException: No suitable driver found for http://ww ...
- memcached+magent的集群部署详细过程
问题描述 Memcached在实现分布集群部署时, Memcached服务端的之间是没有通讯的,服务端是伪分布式,实现分布式是由客户端实现的,客户端实现了分布式算法把数据保存到不同的Memcached ...
- IE浏览器直接在页面中显示7z文件而不是下载问题解决
IE浏览器中输入7z文件的完整下载URL后,不是保存文件,而是直接在页面中显示(当然是乱码) 这是因为浏览器对不同的资源文件处理的方式不同,例如图片文件,一般会直接打开,所以我们可以不用7z,使用zi ...
- Ubuntu安装Vivado
Step1 安装必要的库文件: sudo apt install libncurses5 build-essential openjdk-11-jdk Step2 进入vivado的安装文件夹 sud ...
- LuoguP5488 差分与前缀和
题意 给定一个长为\(n\)的序列\(a\),求出其\(k\)阶差分或前缀和.结果的每一项都需要对\(1004535809\)取模. 打表找规律 先看前缀和,设\(n=5\),\(k=4\),按照阶从 ...
- OpenCV 和 Dlib 人脸识别基础
00 环境配置 Anaconda 安装 1 下载 https://repo.anaconda.com/archive/ 考虑到兼容性问题,推荐下载Anaconda3-5.2.0版本. 2 安装 3 测 ...
- Flink的状态与容错
本文主要运行到Flink以下内容 检查点机制(CheckPoint) 状态管理器(StateBackend) 状态周期(StateTtlConfig) 关系 首先要将state和checkpoint概 ...
- REST 架构的替代方案 为什么说GraphQL是API的未来?
Managing enterprise accounts - GitHub Docs https://docs.github.com/en/graphql/guides/managing-enterp ...