题解——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代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
随机推荐
- Flask视图函数报fmalformed url rule错误的原因
Flask视图函数报fmalformed url rule错误,原因可能是包含中文字符了 把标点符号都重新写一遍英文格式的,可能就不会报这个了
- 微信小程序制作家庭记账本之六
第六天,自己根据样本代码做的记账本可实现添加账目,删除账目和消费总和,实机可以测试,但是没有域名,别人无法访问.
- 匿名内部类和内部类中的this
package test; public class A extends B { public String toString() { return "A"; } public s ...
- excel vba 数据分析
(Visual Basic Application) VBA(Visual Basic for Application)是Microsoft Office系列软件的内置编程语言,其语法结构与Visua ...
- node.js读取到的文件列表
var fs = require('fs'); var path = require('path'); //解析需要遍历的文件夹,我这以E盘根目录为例 var filePath = path.reso ...
- 垂直打击之JVM剖析
让Java应用程序运行是一回事,但让他们跑得快就是另外一回事了.在面对对象的环境中,性能问题就像来势凶猛的野兽.但JVM的复杂性将性能调整的复杂程度增加了一个级别.这里Refcard涵盖了JVM in ...
- Javaweb笔记—03(BS及分页的业务流程)
DAO部分:中间层声明该有的变量 pagerBook pageData sumRow sumPage求出总的记录数id唯一标识:select count(id) as rowsum from book ...
- MyEclipse配置Struts2的本地工作空间中的DTD约束文件
1.拷贝URI http://struts.apache.org/dtds/struts-2.3.dtd 2.配置XML Catalog 3.struts-2.3.dtd文件来源 解压jar包\Web ...
- 在nginx的http模块下面,一个server就可以看做一个站点,配置形式大概是这样的:
http { index index.php index.htm index.html; server { server_name www.site1.com; location / { # [... ...
- ssh-keygen 命令
功能 生成.管理和转换认证密钥,包括 RSA 和 DSA 两种密钥,密钥类型可以用 -t 选项指定.如果没有指定则默认生成用于SSH-2的RSA密钥,系统管理员还可以用它产生主机密钥. 通常,这个程序 ...