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

A team of three programmers is going to play a contest. The contest consists of nn problems, numbered from 11 to nn. 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,k1a1,1,a1,2,…,a1,k1. The second one has received problems a2,1,a2,2,,a2,k2a2,1,a2,2,…,a2,k2. The third one has received all remaining problems (a3,1,a3,2,,a3,k3a3,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,k2k1,k2 and k3k3 (1k1,k2,k32105,k1+k2+k321051≤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 k1k1 integers a1,1,a1,2,,a1,k1a1,1,a1,2,…,a1,k1 — the problems initially taken by the first participant.

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

The fourth line contains k3k3 integers a3,1,a3,2,,a3,k3a3,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,jai,j meets the condition 1ai,jn1≤ai,j≤n, where n=k1+k2+k3n=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
input
Copy
2 1 2
3 1
4
2 5
output
Copy
1
input
Copy
3 2 1
3 2 1
5 4
6
output
Copy
0
input
Copy
2 1 3
5 6
4
1 2 3
output
Copy
3
input
Copy
1 5 1
6
5 1 2 4 7
3
output
Copy
2
Note

In the first example the third contestant should give the problem 22 to the first contestant, so the first contestant has 33 first problems, the third contestant has 11 last problem, and the second contestant has 11 remaining problem.

In the second example the distribution of problems is already valid: the first contestant has 33 first problems, the third contestant has 11 last problem, and the second contestant has 22 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.

#include <bits/stdc++.h>
using namespace std;
const int N = 250000;
int t[4*N],a[5][N],b[5][N],c[5][N];
void build(int x,int l,int r) {
    if (l == r) {
        t[x] = c[3][l];
        return;
    }
    int mid = (l + r) >>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    t[x] = min(t[x<<1] , t[x<<1|1]);
}
int query(int x,int l,int r,int ll,int rr) {
    if (ll <= l && r <= rr) return t[x];
    int mid = (l + r) >> 1;
    int ans = 1<<30;
    if (ll <= mid) ans = min(ans,query(x<<1,l,mid,ll,rr));
    if (rr > mid ) ans = min(ans,query(x<<1|1,mid+1,r,ll,rr));
    return ans;
}
int main() {
    int k1,k2,k3;
    cin >> k1 >> k2 >> k3;
    int n = k1+k2+k3;
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for (int i = 1; i <= k1; i++) {
        cin >> a[1][i];
        b[1][a[1][i]] = 1;
    }
    for (int i = 1; i <= k2; i++) cin >> a[2][i];
    for (int i = 1; i <= k3; i++) {
        cin >> a[3][i];
        b[3][a[3][i]] = 1;
    }
    c[1][0] = k1;
    for (int i = 1; i <= n; i++) {
        b[1][i] = b[1][i-1]+b[1][i]; //记录第一个人前i个数中拥有几个
        c[1][i] = i - b[1][i] + k1 - b[1][i];//i-b[1][i]为需要移动到第一个人的操作数,k1-b[1][i]为第一个人把数移动出去的操作数
    }
    c[3][n+1] = k3;
    for (int i = n; i; i--) {
        b[3][i] = b[3][i+1] + b[3][i];
        c[3][i] = n-i+1-b[3][i] +k3 - b[3][i]-(k1-b[1][i-1]); //如果是第一个人可以移动到第三个人的数会重复需要减掉
    }
    for (int i = 1; i < n; i++)
        c[1][i] -= k3-b[3][i+1];//第三个人可以移动到第一个人的数
    build(1,0,n+1);
    int ans = c[1][n];
    for (int i = 0; i < n; i++) {
        ans = min(ans, c[1][i] + query(1,0,n,i+1,n+1));
    }
    cout << ans << endl;
}
//设c[1][i]为第一个人m1为i时需要移动的次数,
//c[3][i]为m2为i是第三个人需要操作的次数,当其他两个人数字合法时,
//第二个人的数字也会合法.枚举第一个人的每个i,查询m2为(i+1~n+1)的最小操作次数,
//ans = min{c[1][i]+min(c[3][k](i<k<=n))} 查询操作可用线段树维护

猜你喜欢

转载自www.cnblogs.com/QingyuYYYYY/p/11863748.html
今日推荐