D - 3N Numbers


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3Na=(a1,a2,…,a3N). Snuke is constructing a new sequence of length 2Na', by removing exactly N elements from a without changing the order of the remaining elements. Here, the score of a' is defined as follows: (the sum of the elements in the first half of a')−(the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

  • 1≤N≤105
  • ai is an integer.
  • 1≤ai≤109

Partial Score

  • In the test set worth 300 points, N≤1000.

Input

Input is given from Standard Input in the following format:

N
a1 a2 a3N

Output

Print the maximum possible score of a'.


Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a2 and a6 are removed, a' will be (3,4,1,5), which has a score of (3+4)−(1+5)=1.


Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a1 are removed, a' will be (2,3), which has a score of 2−3=−1.


Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a2a3 and a9 are removed, a' will be (8,7,4,6,5,3), which has a score of (8+7+4)−(6+5+3)=5.

/// 题意是:有一个 3n 长的序列,现拿走 n 个数,然后分成前 n 个数,和后 n 个数 ,求前n个数和减后 n 个数和的最大值

// 用一个优先队列保存区间最大 n 数和,并赋给数组保存

用一个优先队列保存区间最小 n 数和,并赋给数组保存

最后循环一遍即可

 #include <bits/stdc++.h>
using namespace std;
#define LL long long
#define INF (1LL<<62)
#define MX 100005*3 LL a[MX];
LL ma[MX];
LL mi[MX]; int main()
{
LL n;
cin>>n; for (int i=;i<=*n;i++)
scanf("%lld",&a[i]);
priority_queue <LL> Q;
LL sum = ;
for (int i=;i<=*n;i++)
{
Q.push(-a[i]);
sum+=a[i]; if (i>n) sum += Q.top(),Q.pop();
ma[i]=sum;
} while (!Q.empty()) Q.pop();
sum=;
for (int i=*n;i>=n+;i--)
{
Q.push(a[i]);
sum+=a[i]; if (i<=*n) sum -= Q.top(),Q.pop();
mi[i]=sum;
} LL ans = -INF;
for (int i=n;i<=*n;i++)
ans = max (ans, ma[i]-mi[i+]);
cout<<ans<<endl;
return ;
}

3N Numbers的更多相关文章

  1. Atcoder 2566 3N Numbers(优先队列优化DP)

    問題文N を 1 以上の整数とします. 長さ 3N の数列 a=(a1,a2,…,a3N) があります. すぬけ君は.a からちょうど N 個の要素を取り除き.残った 2N 個の要素を元の順序で並べ. ...

  2. Atcoder D - 3N Numbers(优先队列+dp)

    题目链接:http://abc062.contest.atcoder.jp/tasks/arc074_b 题意:给出3*n个数要求去掉n个数使得剩下的前n个数-后n个数的差最大. 题解:显然是一道如果 ...

  3. 【AtCoder】ARC074

    ARC 074 C - Chocolate Bar 直接枚举第一刀横切竖切,然后另一块要求如果横切分成\(H / 2\)竖切分成\(W/2\)即可 #include <bits/stdc++.h ...

  4. Atcoder Regular-074 Writeup

    C - Chocolate Bar 题面 There is a bar of chocolate with a height of H blocks and a width of W blocks. ...

  5. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  6. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

  7. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  8. uva----(100)The 3n + 1 problem

     The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a ...

  9. 第二章 I - The 3n + 1 problem(2.4.2)

    这是一道很坑爹的题,一定注意输入的两个数的大小,并且不能简单的交换,因为在最后的输出的时候还需要将原来的数按照原来的顺序和大小,这就是为什么还得开辟两个值得原因 Description Problem ...

随机推荐

  1. Django——如何使用Template以及如何向template传递变量

    如何使用模板系统 在Python代码中使用Django模板的最基本方式如下: 可以用原始的模板代码字符串创建一个 Template 对象, Django同样支持用指定模板文件路径的方式来创建 Temp ...

  2. 语言那点事,crt

    C语言标准(不管是ANSI 还是ISO)包含2部分,一部分是语言本身的标准,另一部分是C标准函数库.C标准函数库规定了函数的原型和功能,但是并没限定这些函数要怎么实现.所谓满足标准C规定的C编译器,不 ...

  3. MATLAB读取黑白图像显示却是黑色,24位深转8位深黑白图像解决方法

    1.24位深转8位深: ps将24位深原图.png保存为GIF图256即为8位,再将8位gif图转为需要的.png,即转为8位深png图. 2.MATLAB读取黑白图像显示几乎全为黑色: 这是最近处理 ...

  4. windows 2008 R2 x64安装oracle12c报INS-30131

    问题描述:Window 2008 R2 x64安装Oracle12c报[INS-30131]执行安装程序验证所需的初始设置失败 解决方法:先在控制面板的“服务”,看SERVER服务是否启动,如果没有启 ...

  5. 网页中多一些常见效果之伸缩菜单(主要是学习js的书写方法)

    效果如下图: 代码很简单,如下: <!doctype html> <html lang="en"> <head> <meta charse ...

  6. #1214 - The used table type doesn't support FULLTEXT indexes解决办法

    #1214 - The used table type doesn't support FULLTEXT indexes报此错误的原因是:InnoDB不支持FULLTEXT类型的索引. 网上的解决办法 ...

  7. CentOS6.5配置PHP CI程序

    步骤: 1.安装CentOS6.5系统:     1.选择PHP+Mysql环境 2.关闭防火墙和SeLinux     1.chkconfig --level 35 iptables off     ...

  8. mac之homebrew安装

    通过homebrew可以管理众多开源软件的安装和卸载.使用homebrew可以非常容易的安装 os x中没有包含的unix工具包和语言包,比如wget;node;lua;rabbitmq;nginx等 ...

  9. Atitit。sql2016标准化的规划方案 v3 q2a

    Atitit.sql2016标准化的规划方案 v3 q2a 1. Sql标准化的历史3 1.1. Sql92标准4 1.2. Sql99标准4 1.3. SQL:2003为例,它包括以下9个部分 5 ...

  10. Xilinx DDR3 IP核使用问题汇总(持续更新)和感悟

    一度因为DDR3的IP核使用而发狂. 后来因为解决问题,得一感悟.后面此贴会完整讲述ddr3 ip的使用.(XILINX K7) 感悟:对于有供应商支持的产品,遇到问题找官方的流程.按照官方的指导进行 ...