3N Numbers
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 3N, a=(a1,a2,…,a3N). Snuke is constructing a new sequence of length 2N, a', 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 a2, a3 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的更多相关文章
- Atcoder 2566 3N Numbers(优先队列优化DP)
問題文N を 1 以上の整数とします. 長さ 3N の数列 a=(a1,a2,…,a3N) があります. すぬけ君は.a からちょうど N 個の要素を取り除き.残った 2N 個の要素を元の順序で並べ. ...
- Atcoder D - 3N Numbers(优先队列+dp)
题目链接:http://abc062.contest.atcoder.jp/tasks/arc074_b 题意:给出3*n个数要求去掉n个数使得剩下的前n个数-后n个数的差最大. 题解:显然是一道如果 ...
- 【AtCoder】ARC074
ARC 074 C - Chocolate Bar 直接枚举第一刀横切竖切,然后另一块要求如果横切分成\(H / 2\)竖切分成\(W/2\)即可 #include <bits/stdc++.h ...
- 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. ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 烟大 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 ...
- 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 ...
- uva----(100)The 3n + 1 problem
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- 第二章 I - The 3n + 1 problem(2.4.2)
这是一道很坑爹的题,一定注意输入的两个数的大小,并且不能简单的交换,因为在最后的输出的时候还需要将原来的数按照原来的顺序和大小,这就是为什么还得开辟两个值得原因 Description Problem ...
随机推荐
- vue - 总结build.js
1. 导入外部包,用关键字 const :ES2015->不可变量 内部使用变量,let -> 块级声明 2.无分号结尾(我猜大概是用了es6变量,避免了es5应缺少分号出现的一些问题) ...
- Android-LinearLayout布局技巧(一)
先看2张图 一.5.1寸 二.3.7寸 三.代码 <?xml version="1.0" encoding="utf-8"?> <Linear ...
- 金典 SQL笔记(2)
由于在本地笔记上写的.CSDN markdown编辑器仅仅支持.md格式导入, 图片没办法直接导进去.写的多了懒的一张一张图片切图上传; 直接整个文章切成图片上传上去了.
- Eclipse 安装应用SVN地址
SVN插件下载地址及更新地址,你根据需要选择你需要的版本.现在最新是1.8.xLinks for 1.8.x Release:Eclipse update site URL: http://subcl ...
- php文件加载路径
<?php require('reusable.php'); echo "相对路径加载<br/>"; /* ./ 表示当前文件所在的目录 ../ 表示当前文件所在 ...
- 使用dynamic类型来优化反射
什么是dynamic类型?微软给出的官方文档中这样解释:在通过 dynamic 类型实现的操作中,该类型的作用是绕过编译时类型检查. 改为在运行时解析这些操作. dynamic 类型简化了对 COM ...
- Apache Rewrite 规则详解
在开篇之前: 我想说这篇文章其实是我刚刚接触Rewrite的时候学习的文档,应属转载,但是在这里我不想写明原地址,原因是文章中大多数给出的配置命令经实验都是错误的.需要原文的可以在谷歌上搜索一下&qu ...
- hdu 3641 数论 二分求符合条件的最小值数学杂题
http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...
- URAL 1750 Pakhom and the Gully 计算几何+floyd
题目链接:点击打开链接 gg.. . #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cs ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...