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:
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.
思路:显然前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;
}