E. The Contest-------思维+LIS

173 篇文章 0 订阅
28 篇文章 0 订阅

A team of three programmers is going to play a contest. The contest consists of n problems, numbered from 1 to n. 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 a1,1,a1,2,…,a1,k1. The second one has received problems a2,1,a2,2,…,a2,k2. The third one has received all remaining problems (a3,1,a3,2,…,a3,k3).

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 k1,k2 and k3 (1≤k1,k2,k3≤2⋅105,k1+k2+k3≤2⋅105) — the number of problems initially taken by the first, the second and the third participant, respectively.

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

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

The fourth line contains k3 integers a3,1,a3,2,…,a3,k3 — 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 ai,j meets the condition 1≤ai,j≤n, where n=k1+k2+k3.

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
inputCopy

2 1 2
3 1
4
2 5
outputCopy
1
inputCopy
3 2 1
3 2 1
5 4
6
outputCopy
0
inputCopy
2 1 3
5 6
4
1 2 3
outputCopy
3
inputCopy
1 5 1
6
5 1 2 4 7
3
outputCopy
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 。通过LIS得到长度(len)一定是保持最大不会动的位置,位置是固定。那么还剩n-len个,就是我们需要的答案


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N];
int f[N];
int q[N];
int n,k1,k2,k3;
void lis()
{
    int len=0;
    q[0]=-2e9;
    for(int i=1;i<=n;i++)
    {
        int l=1,r=len+1;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(q[mid]<a[i]) l=mid;
            else r=mid-1;
        }
        len=max(len,r);
        q[r+1]=a[i];
    }
    cout<<n-len<<endl;
}
int main()
{
	scanf("%d %d %d",&k1,&k2,&k3);
	n=k1+k2+k3;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+1+k1);
	sort(a+1+k1,a+1+k2+k1);
	sort(a+1+k2+k1,a+1+k3+k1+k2);
	lis();
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值