AtCoder: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 3Na=(a1,a2,…,a3N). Snuke is constructing a new sequence of length 2Na', 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

  • 1N105
  • ai is an integer.
  • 1ai109

Partial Score

  • In the test set worth 300 points, N1000.

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

Copy
2
3 1 4 1 5 9

Sample Output 1

Copy
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

Copy
1
1 2 3

Sample Output 2

Copy
-1

For example, when a1 are removed, a' will be (2,3), which has a score of 23=−1.


Sample Input 3

Copy
3
8 2 2 7 4 6 5 3 8

Sample Output 3

Copy
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.

题意:给3*N个数,要求移走N个数,使剩下的数左半部分之和 减 右半部份之和 最大,输出这个最大值。

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值