Codeforces Round #564 (Div. 2) C. Nauuo and Cards(二分)

C. Nauuo and Cards

Nauuo is a girl who loves playing cards.

One day she was playing cards but found that the cards were mixed with some empty ones.

There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.

In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.

Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?

Input
The first line contains a single integer n (1≤n≤2⋅105) — the number of numbered cards.

The second line contains n integers a1,a2,…,an (0≤ai≤n) — the initial cards in Nauuo's hands. 0 represents an empty card.

The third line contains n integers b1,b2,…,bn (0≤bi≤n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.

It is guaranteed that each number from 1 to n appears exactly once, either in a1..n or b1..n.

Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.

 

题意:给定一个整数n,然后有两个长度为 n 的数组a,b。其中,a 表示你现在手中的牌,b 表示牌堆,从上往下表示。每一次操作,你都可以在手中先往牌低放一张牌,然后抽走牌顶。每一张牌的范围是 1 ~ n 且不重复,空牌用 0 表示。问最后至少需要多少次操作,才能使牌堆从上往下是 1 ~ n 的顺序。

思路:这个题分为两种情况,一种是 牌堆的结尾为 1 2 3 4 ... 这样可以,不是从1开始放置的,  另一种就是  需要从1开始放置。所以对于情况1,我们只需要判断一下就可以了。

对于情况2:我们可以二分,每一次都二分一个牌堆的位置 i ,表示先操作 i - 1 次,将 i 位置之前的牌先全部抽走,然后到位置 i 时,就开始放 1 ,判断能不能放出一个1 ~ n 的序列。

两种情况取最小值即可

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;
const int maxn = 2e5 + 10;
int a[maxn],b[maxn];
int n;
unordered_map<int,int>mp;

bool check(int mid){
    mp.clear();
    for(int i = 1;i <= n;++ i)
        mp[a[i]] = 1;
    for(int i = 1;i < mid;++ i)
        mp[b[i]] = 1;
    int fi = 1;
    for(int i = mid;i <= n;++ i){
        if(!mp.count(fi)) return 0;
        mp[b[i]] = 1;
        ++ fi;
    }
    return 1;
}

int main() {
    while(~scanf("%d",&n)){
        for(int i = 1;i <= n;++ i)
            scanf("%d",&a[i]);
        int be;
        for(int i = 1;i <= n;++ i){
            scanf("%d",&b[i]);
            if(b[i] == 1) be = i;
        }
        int Begin = 1,flag = 1;
        for(int i = be;i <= n;++ i){
            if(b[i] != Begin){
                flag = 0;
                break;
            }
            ++ Begin;
        }

        int l = 1,r = n;
        int ans = 2 * n + 1;
        while(l <= r){
            int mid = (l + r) >> 1;
            if(check(mid)){
                r = mid - 1;
                ans = min(ans,n + mid);
            }
            else l = mid + 1;
        }
        int ans1 = 0x3f3f3f3f;
        if(flag){
            mp.clear();
            int ok = 1;
            for(int i = 1;i <= n;++ i) mp[a[i]] = 1;
            int fi = Begin;
            for(int i = 1;i < be;++ i){
                if(!mp.count(fi)) ok = 0;
                mp[b[i]] = 1;
                ++ fi;
            }
            if(ok) ans1 = be - 1;
        }
        printf("%d\n",min(ans - 1,ans1));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值