贪心的选取最优解

然后相减好

记得要开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 (贪心)的更多相关文章

  1. 题解——Codeforces Round #508 (Div. 2) T2 (构造)

    按照题意构造集合即可 注意无解情况的判断 #include <cstdio> #include <algorithm> #include <cstring> #in ...

  2. 题解——Codeforces Round #508 (Div. 2) T1 (模拟)

    依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include &l ...

  3. Codeforces Round #508 (Div. 2)

    Codeforces Round #508 (Div. 2) http://codeforces.com/contest/1038 A #include<bits/stdc++.h> us ...

  4. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  5. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  6. Codeforces 1038F Wrap Around (Codeforces Round #508 (Div. 2) F) 题解

    写在前面 此题数据量居然才出到\(n=40\)???(黑人问号)以下给出一个串长\(|S|=100,n=10^9\)的题解. 题目描述 给一个长度不超过\(m\)的01串S,问有多少个长度不超过\(n ...

  7. Codeforces Round #547 (Div. 3) F 贪心 + 离散化

    https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...

  8. Codeforces Round #508 (Div. 2) D. Slime

    D. Slime 题目链接:https://codeforces.com/contest/1038/problem/D 题意: 给出两个数,然后每次可以对相邻的两个数合并,比如x,y,那么合并过后就是 ...

  9. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

随机推荐

  1. C# & ASP.NET Core 入门官方资料汇总

    借助给公司实习生培训事宜,整理了一些微软官方的适合新同学入门的资料,这里分享一下: 工具: Visual Studio 2017 Community 版本下载地址:https://www.visual ...

  2. codeforces 975C Valhalla Siege

    题意: 有n个巫师站成一列,每个巫师有自己的血量. 一个人射箭攻击他们,每次造成若干点伤害,巫师按照给定的顺序承受伤害,如果伤害大了,那么死掉,伤害落到下一个巫师身上. 如果一轮攻击之后,所有的巫师都 ...

  3. NSOperation、NSOperationQueue(III)

    NSOperation.NSOperationQueue 常用属性和方法归纳 NSOperation 常用属性和方法 a. 取消操作方法 //可取消操作,实质是标记 isCancelled 状态. - ...

  4. python 简单了解namedtuple

    namedtuple类位于collections模块,有了namedtuple后通过属性访问数据能够让我们的代码更加的直观更好维护 namedtuple能够用来创建类似于元祖的数据类型,除了能够用索引 ...

  5. AtCoder Beginner Contest 069 ABCD题

    题目链接:http://abc069.contest.atcoder.jp/assignments A - K-City Time limit : 2sec / Memory limit : 256M ...

  6. Codeforce 294A - Shaass and Oskols (模拟)

    Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to ...

  7. 通过junit/TestNG+java 实现自动化测试

    第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-188026 ...

  8. scala语言中的case关键字在spark中的一个奇特使用

    package com.spark.demo import com.spark.demo.util.SparkUtil import org.apache.spark.rdd.RDD import s ...

  9. 前端框架VUE----node.js的简单介绍

    一.什么是node.js? 它是可以运行JavaScript的服务平台,可以吧它当做一门后端程序,只是它的开发语言是JavaScript 二.安装 1.node.js的特性: - 非阻塞IO模型 - ...

  10. 一位前辈的博客,收获颇丰,包括Android、Java、linux、前端、大数据、网络安全等等

    https://www.cnblogs.com/lr393993507/   魔流剑