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. Quartz与Spring的整合使用

    之前说到过Quartz的基本使用(猛戳这里看文章).在实际使用中,我们一般会将定时任务交由spring容器来管理.所以今天我们来说说Quartz与spring的整合. 咱们还是依照Quartz的三大元 ...

  2. 微信小程序 - 多地点标识(map)

    演示如下: wxml <map id="map" scale="{{scale}}" controls="{{controls}}" ...

  3. EffectManager

    using UnityEngine; using System.Collections; public class EffectManager : MonoBehaviour { public Ani ...

  4. 取给定正整数的指定bit位開始的指定长度的数据

    接口说明 原型: unsigned int GetBitsValue(unsigned int input, unsigned int startbit, unsigned int bitlen) 输 ...

  5. Docker -CentOS 6.5上安装

    开始安装daoker之旅: 1. [root@localhost ~]# uname -r -.el6.x86_64 2. [root@localhost ~]# cat /etc/issue Cen ...

  6. 转MQTT SERVER 性能测试报告

    硬件环境: 内存4G CPU4核 SERVER及端口: apollo端口 61619 mosquitto:端口 1884 activeMQ端口:1883 emqtt 端口1885 测试方法 并发测试: ...

  7. jq时间戳转化为可视化时间

    //2016年5月21日 23:12:07 function getDateTimeToDate(dt){ var dateTime = new Date(dt); var date = dateTi ...

  8. C6455 CSL_EMIF详解

    C6455 CSL_EMIF详解 原网址http://www.61ic.com/Article/C6000/C64X/201303/47507.html C6455CSL详解 和DSP6455的EMI ...

  9. 手把手教你用git和SourceTree上传项目到github细解(转)

    尊重原创:https://blog.csdn.net/qq_32365567/article/details/52859166 一.引言 我想大家现在都很熟悉github了,也能运用github上开源 ...

  10. dubbo_实现Hessian的远程调用协议

    1.优点 连接个数:多连接 连接方式:短连接 传输协议:HTTP 传输方式:同步传输 序列化:Hessian二进制序列化 适用范围:传入传出参数数据包较大,提供者比消费者个数多,提供者压力较大,可传文 ...