[CSP-S Simulation Test]: Race (third greedy +)

Title Description

Due to the depletion of the topic and thinking people think so fun background.
There are $ n $ th article, the price of the $ i $ th item is $ v_i $, there are two people, everyone likes to $ n $ th articles of some of the items.
$ M $ asked to pick just one article, the article satisfying the selected at least one article is $ k $ first crush, $ k $ th article is like the second person. And the value of minimum prices and.


Input Format

The first three row number $ n, m, k $.
$ $ N-number of the second line, the first number represents $ I $ $ v_i $.
The third line contains a number $ a $, indicates the number of articles of the first personal favorite.
The fourth line contains $ a $ number, represents the first personal favorite items which a few.
The fifth line contains a number $ b $, the second represents the number of items personally like.
The sixth line contains the number $ b $, represents the second personal favorite items which a few.


Output Format

A number indicates the answer. If there is a legitimate program output $ -1 $.


Sample

Sample input:

4 3 2
3 2 2 1
2
1 2
2
1 3

Sample output:

7


Data range and tips

For the test point $ 1 \ sim 4 $: $ n \ leqslant 20 $.
For the test point $ 5 \ sim 10 $: the absence of an item to be two people like.
For the test point $ 11 \ sim 15 $: $ n \ leqslant 2 \ times 10 ^ 3 $.
For the test point $ 16 \ sim 20 $: no special restrictions.
For all data, $ n \ leqslant 2 \ times 10 ^ 5, m, k \ leqslant n, v_i \ leqslant 10 ^ 9 $.


answer

This question is outstanding randomization can get $ 95 $ points ......

We can set up the intersection of the two favorite items for the number of $ r $, then we can be greedy.

The answer was found to meet single valley, so we can thirds.

In fact, one-third are also vulnerable, because some of the $ r $ might correspond to the same answer, but apparently not random data card.

Time complexity: $ \ Theta (n \ log k) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int N,M,K,A,B;
int v[200001];
bool a[200001],b[200001];
int que[4][200001],top1,top2,top3,top4;
long long ans=1LL<<60;
bool cmp(int x,int y){return v[x]<v[y];}
long long judge(int x)
{
	long long res=0;
	for(int i=1;i<=x;i++)res+=v[que[3][i]];
	for(int i=1;i<=K-x;i++){res+=v[que[1][i]];res+=v[que[2][i]];}
	int lst=M-x-2*max(K-x,0);
	int flag1=max(K-x,0)+1;
	int flag2=max(K-x,0)+1;
	int flag3=1;
	while(lst)
	{
		if(v[que[1][flag1]]<=v[que[2][flag2]]&&v[que[1][flag1]]<=v[que[0][flag3]])
		{
			res+=v[que[1][flag1]];
			flag1++;
		}
		else if(v[que[2][flag2]]<=v[que[1][flag1]]&&v[que[2][flag2]]<=v[que[0][flag3]])
		{
			res+=v[que[2][flag2]];
			flag2++;
		}
		else
		{
			res+=v[que[0][flag3]];
			flag3++;
		}
		lst--;
	}
	return res;
}
int main()
{
	scanf("%d%d%d",&N,&M,&K);
	for(int i=1;i<=N;i++)scanf("%d",&v[i]);
	scanf("%d",&A);
	for(int i=1;i<=A;i++)
	{
		int x;
		scanf("%d",&x);
		a[x]=1;
	}
	scanf("%d",&B);
	for(int i=1;i<=B;i++)
	{
		int x;
		scanf("%d",&x);
		b[x]=1;
	}
	for(int i=1;i<=N;i++)
	{
		if(!a[i]&&!b[i])que[0][++top1]=i;
		if( a[i]&&!b[i])que[1][++top2]=i;
		if(!a[i]&& b[i])que[2][++top3]=i;
		if( a[i]&& b[i])que[3][++top4]=i;
	}
	if(top2+top4<K||top3+top4<K||top4<max(2*K-M,0)||M<K||min(top4,K)+2*max(K-top4,0)>M){puts("-1");return 0;}
	sort(que[0]+1,que[0]+top1+1,cmp);
	sort(que[1]+1,que[1]+top2+1,cmp);
	sort(que[2]+1,que[2]+top3+1,cmp);
	sort(que[3]+1,que[3]+top4+1,cmp);
	v[0]=0x3f3f3f3f;
	int lft=max(K-min(top2,top3),max(2*K-M,0));
	int rht=min(K,top4);
	while(rht-lft>2)
	{
		int midl=lft+(rht-lft)/3;
		int midr=rht-(rht-lft)/3;
		if(judge(midl)<judge(midr))rht=midr;
		else lft=midl;
	}
	ans=min(ans,judge(lft));
	ans=min(ans,judge(lft+1));
	ans=min(ans,judge(rht));
	if(ans==(1LL<<60))puts("-1");
	else printf("%lld",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11615892.html