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 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:

Na1 a2  a3N

Output

Print the maximum possible score of a'.


Sample Input 1

Copy
23 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
11 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
38 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;}



原创粉丝点击