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. GCD编程-串行队列与并发队列

    接着上面的GCD封装,以下进行列子验证 1.导入GCD.h 2.创一个串行队列: - (void)serailQueue{ //创建出队列 GCDQueue *queue =  [[GCDQueue  ...

  2. Unity Android && Ant Automate Build 之一

    这几天抽空研究下Android SDK 自动化打包.顺便温习下Unity 与 Android 交互机制. 一.Unity 与 Android 的 消息传递机制 Example: 导出 Android ...

  3. 数据结构之---C语言实现最小生成树之prim(普里姆)算法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  4. iOS开发-项目的完整重命名方法,图文教程。

    前言:在IOS开发中,有时候想改一下项目的名字,都会遇到很多麻烦.直接改项目名吧,XCODE又不会帮你改所有的名字.总是有很多文件.文件夹或者是项目设置的项.而且都是不能随便改的,有时候改着改着,编译 ...

  5. Asp.Net WebApi服务端解决跨域方案

    1.特性方式 主要是继承ActionFilterAttribute,重写OnActionExecuted方法,在action执行后,给响应头加上一个键值对. using System.Web.Http ...

  6. ConfigurationManager.AppSettings 属性 appSettings

    https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager.appsettings(v=vs. ...

  7. tomcat的安装、配置

    简单介绍 Tomcat server是一个免费的开放源码的Web 应用server,属于轻量级应用server, 在中小型系统和并发訪问用户不是非常多的场合下被普遍使用,是开发和调试JSP 程序的首选 ...

  8. jquery treegrid实例

    前台jqurey代码 function organDatagrid(){ $organ_treegrid = $('#organ_treegrid').treegrid({ url:ctx+'/pet ...

  9. Atitit.eclipse comment  template注释模板

    Atitit.eclipse comment  template注释模板 1. Code templet1 1.1. Settpath1 1.2. 设置存储1 1.3. 导出设置1 2. Java d ...

  10. OCR 即 光学字符识别

    OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...