# 【PA2014】【BZOJ3728】Final Zarowki

Description

Input

Output

Sample Input

6 2

12 1 7 5 2 10

1 4 11 4 7 5
Sample Output

33
HINT

Source

heap1为小根堆,heap2为大根堆

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define MAXN 500010
#define GET (ch>='0'&&ch<='9')
using namespace std;
int n,k;
int a[MAXN],b[MAXN];
long long ans;
void in(int &x)
{
char ch=getchar();x=0;
while (!GET)    ch=getchar();
while (GET) x=x*10+ch-'0',ch=getchar();
}
struct Node {int x;bool operator <(const Node& a)const  {return x>a.x;}};
struct node {int x;bool operator <(const node& a)const  {return x<a.x;}};
priority_queue<Node>heap1;
priority_queue<node>heap2;
int main()
{
in(n);in(k);
for (int i=1;i<=n;i++)  in(a[i]);
for (int i=1;i<=n;i++)  in(b[i]);
sort(a+1,a+n+1);sort(b+1,b+n+1);
for (int j=n,i=n;i;i--)
{
for (;j&&a[j]>=b[i];j--)    heap1.push((Node){a[j]});
if (!heap1.empty())
{
int t=heap1.top().x;ans+=heap1.top().x;
heap1.pop();heap2.push((node){t-b[i]});continue;
}
k--;ans+=b[i];
if (k<0)    {puts("NIE");return 0;}
}
while (k--) ans-=heap2.top().x,heap2.pop();
cout<<ans<<endl;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：【PA2014】【BZOJ3728】Final Zarowki 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)