AtCoder - 2566 优先队列
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
- In the test set worth 300 points, N≤1000.
Input is given from Standard Input in the following format:
N a1 a2 … a3NOutput
Print the maximum possible score of a'.
Sample Input 12 3 1 4 1 5 9Sample 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 21 1 2 3Sample Output 2
-1
For example, when a1 are removed, a' will be (2,3), which has a score of 2−3=−1.
Sample Input 33 8 2 2 7 4 6 5 3 8Sample 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个数之后,前面一半的sum-后面一半的sum最大值;
我们可以用一个小根堆维护前面,用一个大根堆维护后面;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 2000005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-4 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ int n; ll a[maxn]; ll lftmax[maxn], minrgt[maxn]; int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i <3* n; i++)rdint(a[i]); priority_queue<int, vector<int>, greater<int> >q1; priority_queue<int>q2; for (int i = 0; i < n; i++) { lftmax[0] += a[i]; q1.push(a[i]); minrgt[n + 1] += a[3 * n - i - 1]; q2.push(a[3 * n - i - 1]); } for (int i = 1; i <= n; i++) { q1.push(a[n + i - 1]); lftmax[i] = lftmax[i - 1] + a[n + i - 1] - q1.top(); q1.pop(); q2.push(a[n * 2 - i]); minrgt[n - i + 1] = minrgt[n + 1 - i + 1] + a[n * 2 - i] - q2.top(); q2.pop(); } ll res = -inf; for (int i = 0; i <= n; i++) { if (i == 0)res = lftmax[i] - minrgt[i + 1]; else res = max(res, lftmax[i] - minrgt[i + 1]); } cout << res << endl; return 0; }