Nauuo and Cards

                                                                                         Nauuo and Cards

                                                                                        time limit per test

                                                                                        1.5 seconds

                                                                                        memory limit per test

                                                                                        256 megabytes

input

standard input

output

standard output

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 ?

cards numbered from 1 to ?, and they were mixed with another ? empty cards. She piled up the 2? cards and drew ? of them. The ? cards in Nauuo's hands are given. The remaining ?

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 ?

numbered cards piled up in increasing order (the ?-th card in the pile from top to bottom is the card ?

) as quickly as possible. Can you tell her the minimum number of operations?

Input

The first line contains a single integer ?

(1≤?≤2⋅105

) — the number of numbered cards.

The second line contains ?

integers ?1,?2,…,?? (0≤??≤?) — the initial cards in Nauuo's hands. 0

represents an empty card.

The third line contains ?

integers ?1,?2,…,?? (0≤??≤?) — 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 ? appears exactly once, either in ?1..? or ?1..?

.

Output

The output contains a single integer — the minimum number of operations to make the ?

numbered cards piled up in increasing order.

Examples

Input

Copy

3
0 2 0
3 0 1

Output

Copy

2

Input

Copy

3
0 2 0
1 0 3

Output

Copy

4

Input

Copy

11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10

Output

Copy

18

Note

Example 1

We can play the card 2

and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2]

from top to bottom.

Then, we play the card 3

in the second operation. The cards in the pile are [1,2,3]

, in which the cards are piled up in increasing order.

Example 2

Play an empty card and draw the card 1

, then play 1, 2, 3

in order.

题意:n 张标号1——n的牌和 n张空白牌,n 张在手上,n张剩下在牌堆里,每次可以从手上选任意一张牌放到牌堆底部,并拿牌堆顶部的牌放在手上,需要使牌堆从上到下递增地放 1 ~ n,求最少操作数。

首先尝试不打空白牌能否直接完成,如果能就是最优解。

否则最优解一定是先打若干空白牌然后再也不打空白牌。计 p[i]为 i 在牌堆的初始位置(初始在手上为 0),那么答案为ans= max(p[i]−i+1+n)(每张牌最早在第 p[i] 张被放到手中,还要打 n−i +1张)。

 

 

#include<stdio.h>
#include<iostream>
using namespace std;
int p[200050];//p[i]为0表示牌i在手中,否则表示牌i在牌堆的位置

int main()
{
    int i,j,n,num,ans;
    scanf("%d",&n);
    for(i = 1; i <= n; i ++)//手中的牌
    {
        scanf("%d",&num);
        p[num] = 0;
    }
    
    for(i = 1; i <= n; i ++)//牌堆的牌
    {
        scanf("%d",&num);
        p[num] = i;
    }
    
    if(p[1])//如果牌1在牌堆里,则有可能通过不打空牌就能完成
    {
        for(i = 2; i<= n; i ++)//在牌堆里找从1开始的最大连续牌数
        {
            if(p[i] != p[1]+i-1)//到第i张牌不连续了
                break;
        }
        
        if(p[i-1] == n)//如果从牌1到堆底牌是有序的,则有可能通过不打空牌就能完成
        {
            for(j = i; j <= n; j ++)
            {
                if(p[j]  > j-i)//此牌能在其需要打出之前放到手中
                    break;
            }
            if(j > n)//所有牌都可以在被打出之前放到手中
            {
                printf("%d\n",p[1]-1);//可以通过不打空牌完成
                return 0;
            }
        }
    }

    ans = 0;
    for(i = 1; i <= n; i ++)
    {
        ans = max(ans,p[i]+n-i+1);//每张牌放到手中需要p[i]步,打到其对应的位置需要n-i+1步
    }
    printf("%d\n",ans);

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值