AtCoder:3N Numbers(优先队列)
来源:互联网 发布:魔域单机登录器源码 编辑:程序博客网 时间:2024/06/06 19:53
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 awithout 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:
Na1 a2 … a3N
Output
Print the maximum possible score of a'.
Sample Input 1
23 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
11 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
38 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.
思路:显然前n个数不会出现在“右半部份”,后n个数不会出现在“左半部分”,考虑分界点,那么分界点在[n, 2*n],用优先队列预处理每个数左边最大的n个数之和,及其右边最小的n个数之和,就可以找到最优解。
# include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 1e5+3;LL a[maxn*3], l[maxn], r[maxn];priority_queue<LL, vector<LL>, greater<LL> >q;priority_queue<LL>p;int main(){ LL sum = 0, sum2=0, ans; int n, m; scanf("%d",&n); for(int i=1; i<=3*n; ++i) { scanf("%lld",&a[i]); if(i<=n) sum += a[i], q.push(a[i]); else if(i>2*n) sum2 += a[i], p.push(a[i]); } l[n] = sum; r[2*n+1] = sum2; for(int i=n+1; i<=2*n; ++i) { if(q.top() < a[i]) { sum = sum - q.top() + a[i]; q.pop(); q.push(a[i]); } l[i] = sum; } r[2*n] = sum2; for(int i=2*n-1; i>=n; --i) { if(p.top() > a[i+1]) { sum2 = sum2 - p.top() + a[i+1]; p.pop(); p.push(a[i+1]); } r[i] = sum2; } ans = l[n]-r[n]; for(int i=n+1; i<=2*n; ++i) ans = max(ans, l[i]-r[i]); printf("%lld\n",ans); return 0;}
- AtCoder:3N Numbers(优先队列)
- Humble Numbers(优先队列)
- poj - 1338 - Ugly Numbers(优先队列)
- poj1338 Ugly Numbers(set + 优先队列)
- UVa136(Ugly Numbers)优先队列
- poj Ugly Numbers(优先队列)
- HDU1058 Humble Numbers(优先队列+set)
- HDU 6168 Numbers 优先队列
- hdu1058 Humble Numbers【优先队列】
- DIJKSTRA算法(优先队列实现)(o(n*n))
- hdu 1058 Humble Numbers(优先队列+set)
- HDU 1058 Humble Numbers(丑数,优先队列,STL)
- UVA-136 Ugly Numbers (map容器+优先队列)
- UVa 136 Ugly Numbers(优先队列)
- uvaoj 136 Ugly Numbers 优先队列使用
- UVA136 Ugly Numbers(优先队列应用)
- UVA - 136 Ugly Numbers set+优先队列
- uva 136(Ugly Numbers, 优先队列基础)
- XML
- 关于有偿提供拼图响应式后台的通知
- OpenGL-- Shader 颜色 光照 4 光照贴图 柔光射灯
- 欢迎使用CSDN-markdown编辑器
- postfix+mysql
- AtCoder:3N Numbers(优先队列)
- pca
- zlib库使用简单讲解
- windows下安装pip
- Longest Subsequence codeforces 632D 暴力数学
- java多线程的常见例子
- XML约束
- Shell Cookbook
- Magic Numbers codeforces 628D 数位dp