题解——Codeforces Round #508 (Div. 2) T3 (贪心)
贪心的选取最优解
然后相减好
记得要开long long
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <queue>
#define int long long
using namespace std;
int ansa=,ansb=,posa=,posb=,n,a[],b[];
bool cmp(int a,int b){
if(a>b)
return true;
else
return false;
}
signed main(){
scanf("%I64d",&n);
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]);
for(int i=;i<=n;i++)
scanf("%I64d",&b[i]);
sort(a+,a+n+,cmp);
sort(b+,b+n+,cmp);
int whi=;// 0 a 1 b
while(posa<=n||posb<=n){
if(whi==){
if(a[posa]>b[posb]&&posa<=n){
ansa+=a[posa];
posa++;
whi=-whi;
}
else if(a[posa]<=b[posb]&&posb<=n){
posb++;
whi=-whi;
}
else if(posb<=n){
posb++;
whi=-whi;
}
else if(posa<=n){
ansa+=a[posa];
posa++;
whi=-whi;
}
}
else{
if(b[posb]>a[posa]&&posb<=n){
ansb+=b[posb];
posb++;
whi=-whi;
}
else if(b[posb]<=a[posa]&&posa<=n){
posa++;
whi=-whi;
}
else if(posa<=n){
posa++;
whi=-whi;
}
else if(posb<=n){
ansb+=b[posb];
posb++;
whi=-whi;
}
}
}
printf("%I64d",ansa-ansb);
return ;
}
题解——Codeforces Round #508 (Div. 2) T3 (贪心)的更多相关文章
- 题解——Codeforces Round #508 (Div. 2) T2 (构造)
按照题意构造集合即可 注意无解情况的判断 #include <cstdio> #include <algorithm> #include <cstring> #in ...
- 题解——Codeforces Round #508 (Div. 2) T1 (模拟)
依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include &l ...
- Codeforces Round #508 (Div. 2)
Codeforces Round #508 (Div. 2) http://codeforces.com/contest/1038 A #include<bits/stdc++.h> us ...
- [题解] Codeforces Round #549 (Div. 2) B. Nirvana
Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces 1038F Wrap Around (Codeforces Round #508 (Div. 2) F) 题解
写在前面 此题数据量居然才出到\(n=40\)???(黑人问号)以下给出一个串长\(|S|=100,n=10^9\)的题解. 题目描述 给一个长度不超过\(m\)的01串S,问有多少个长度不超过\(n ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Codeforces Round #508 (Div. 2) D. Slime
D. Slime 题目链接:https://codeforces.com/contest/1038/problem/D 题意: 给出两个数,然后每次可以对相邻的两个数合并,比如x,y,那么合并过后就是 ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
随机推荐
- C++的类型转换
一.类型转换名称和语法 1.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a 2.C++风格的类型转换提供了4种类型转换操作符来应对 ...
- 关于git上的一些错误信息
如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...
- 【Linux学习七】软件安装
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.编译安装1.解压 源码文件是压缩包 要先解压tar -zxvf t ...
- CAN学习网站
百度搜索:Controller Area Network http://www.esd-electronics-usa.com/Controller-Area-Network-CAN-Introduc ...
- GUI带有右键菜单,带有时间显示的
%带有右键菜单的GUI figure('Menubar','none'); h = uicontextmenu; uimenu(h,'Label','A'); uimenu(h,'Label','B' ...
- Codeforces 237A - Free Cash
题目链接:http://codeforces.com/problemset/problem/237/A Valera runs a 24/7 fast food cafe. He magically ...
- Codeforces 268B - Buttons
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...
- vue生产环境清除console.log
npm run build 后的生产环境的代码,会有很多开发时留下的console.log(),不可能每个页面不停地删除 在build/webpack.prod.conf.js文件里加上这样一段代码即 ...
- linux利用scp远程上传下载文件/文件夹
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度. 当你服务 ...
- Spring 注入的两种方式
Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...