Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest dp

E. The Contest

A team of three programmers is going to play a contest. The contest consists of 𝑛 problems, numbered from 1 to 𝑛. Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong — the statements were printed in the wrong order, so the contestants have received the problems in some random order.

The first contestant has received problems 𝑎1,1,𝑎1,2,…,𝑎1,𝑘1. The second one has received problems 𝑎2,1,𝑎2,2,…,𝑎2,𝑘2. The third one has received all remaining problems (𝑎3,1,𝑎3,2,…,𝑎3,𝑘3).

The contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems.

During one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems?

It is possible that after redistribution some participant (or even two of them) will not have any problems.

Input

The first line contains three integers 𝑘1,𝑘2 and 𝑘3 (1≤𝑘1,𝑘2,𝑘3≤2⋅105,𝑘1+𝑘2+𝑘3≤2⋅105) — the number of problems initially taken by the first, the second and the third participant, respectively.

The second line contains 𝑘1 integers 𝑎1,1,𝑎1,2,…,𝑎1,𝑘1 — the problems initially taken by the first participant.

The third line contains 𝑘2 integers 𝑎2,1,𝑎2,2,…,𝑎2,𝑘2 — the problems initially taken by the second participant.

The fourth line contains 𝑘3 integers 𝑎3,1,𝑎3,2,…,𝑎3,𝑘3 — the problems initially taken by the third participant.

It is guaranteed that no problem has been taken by two (or three) participants, and each integer 𝑎𝑖,𝑗 meets the condition 1≤𝑎𝑖,𝑗≤𝑛, where 𝑛=𝑘1+𝑘2+𝑘3.

Output

Print one integer — the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems.

Examples

input
2 1 2
3 1
4
2 5
output
1
input
3 2 1
3 2 1
5 4
6
output
0
input
2 1 3
5 6
4
1 2 3
output
3
input
1 5 1
6
5 1 2 4 7
3
output
2

Note

In the first example the third contestant should give the problem 2 to the first contestant, so the first contestant has 3 first problems, the third contestant has 1 last problem, and the second contestant has 1 remaining problem.

In the second example the distribution of problems is already valid: the first contestant has 3 first problems, the third contestant has 1 last problem, and the second contestant has 2 remaining problems.

The best course of action in the third example is to give all problems to the third contestant.

The best course of action in the fourth example is to give all problems to the second contestant.

题意

现在有三个队列,每个队列都有一堆的数。现在你要使得a队列里面的数为排列的前缀,c队列的数是排列的后缀,然后b队列是其他的数。每次操作你可以让一个数到另外的队列里面去,问你最少操作多少次,能够符合需求。

题解

我们考虑排完序之后拼在一起,那么这个序列的lis,就是最后能够保持不变的。

那么nlogn跑一个lis就行。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int k1,k2,k3,n,a[maxn],dp[maxn],b[maxn],len;
void lis(){
	len=1;b[1]=a[0];
	for(int i=1;i<n;i++){
		b[len+1]=n+1;
		int l=0,r=len+1,ans=0;
		while(l<=r){
			int mid=(l+r)/2;
			if(a[i]<b[mid]){
				ans=mid;
				r=mid-1;
			}else{
				l=mid+1;
			}
		}
		b[ans]=a[i];
		if(ans>len)len++;
	}
	cout<<n-len<<endl;
}
int main(){
	scanf("%d%d%d",&k1,&k2,&k3);
	n=k1+k2+k3;
	for(int i=0;i<k1+k2+k3;i++){
		scanf("%d",&a[i]);
	}
	sort(a,a+k1);
	sort(a+k1,a+k1+k2);
	sort(a+k1+k2,a+k1+k2+k3);
	lis();
}
posted @ 2019-11-14 19:43  qscqesze  阅读(460)  评论(0编辑  收藏  举报