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. 倍福TwinCAT(贝福Beckhoff)应用教程11.1 TwinCAT应用小程序1 贝福IO模块介绍

    EL1002,EL1004,EL1008都是数字输入模块(2个点,4个点,8个点),输入高的范围是15V到30V,低的范围是-3V到5V         EL2002,EL2004,EL2008都是数 ...

  2. 解决 XMLHttpRequest status = 0 问题 及 返回值为null问题

    1.XMLHttpRequest status = 0 问题 XMLHttpRequest的说明:http://www.w3.org/TR/XMLHttpRequest/ . The status a ...

  3. 用箭头函数精简Vue 模块

    https://www.zcfy.cc/article/clean-up-your-vue-modules-with-es6-arrow-functions-dotdev 使用箭头函数,this指向v ...

  4. 轻松学习JavaScript十四:JavaScript的RegExp对象(正則表達式)

    一RegExp对象概述 RegExp对象表示正則表達式,RegExp是正則表達式的缩写.它是对字符串运行模式匹配的强大工具. RegExp 对象用于规定在文本中检索的内容. 当您检索某个文本时.能够使 ...

  5. SQL 子查询 EXISTS 和 NOT EXISTS

    MySQL EXISTS 和 NOT EXISTS 子查询语法如下: SELECT … FROM table WHERE EXISTS (subquery) 该语法可以理解为:将主查询的数据,放到子查 ...

  6. php 结合md5的加密,解密方法

    php 结合md5的加密,解密方法 张映 发表于 2012-06-28 分类目录: php 标签:md5, php 最近在整理代码发现了一个不错的东西,结合md5的加解密算法.网上关于php结合md5 ...

  7. iOS开发多线程篇 10 —NSOperation基本操作

    iOS开发多线程篇—NSOperation基本操作 一.并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. ...

  8. Unity3d中使用自带动画系统制作下雨效果(二)

    接着昨天的(一),今天上下雨效果的后半部分.在最后附上网盘链接,有使用的素材及本次的工程源文件,想看看的童鞋可以下载~~ 下雨效果分两部分:地上的涟漪和空中的雨滴.那么现在就开始,是使用unity3d ...

  9. 6.5安装nagios

    最近因为,科研需要,接触上了Nagios,这里,我将安装笔记做个详解.为自己后续需要和博友们学习! VMware workstation 11 的下载 VMWare Workstation 11的安装 ...

  10. php基本语法之逻辑运算符

    百度经验 | 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验 音乐 图片 视频 地图 百科 文库 帮助 首页 分类 杂志 任务 签到 回享计划 商城 知道 百度经验 > ...