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

C. Nauuo and Cards

time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

Examples

inputCopy

3
0 2 0
3 0 1

output

2

input

3
0 2 0
1 0 3

output

4

input

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

output

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.

思路:

两种情况:

一:需要取牌的情况。

找到值与值的下标加一(从0 开始)的差的最大的值,也就是取走值最小,且下标最大的情况,是要取走的,但不一定要直接直接取到下标的位置,取到:下标+1-(值-1),比如,2在下标3的位置,那么取到下标2的位置就行,在将1放到最后面时,2 可以直接出去。总次 : 数就是牌的总数值与值的下标加一(从0 开始)的差的最大的值

二:不需要取牌的情况。

也就是从1的位置到最后一个值是升序1+(n-1-下标)=最后一个值,判断1位置之前存不存在值与值的下标加一(从0 开始)的差是否有大于1的存在存在输出n-最后一个值不存在就输出第一种情况的值

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
int main(){
	 std::ios::sync_with_stdio(false);
	 ll n,x,y,i,j,sum,flag,flag1=-1,flag2=-1,maxn1,maxn2;vector<ll>v;
	while(cin>>n){
		v.clear();sum=n;flag1=0;flag2=-1;flag=-1;maxn1=0;maxn2=0;
		for(i=0;i<n;i++){
	 		cin>>x;if(x!=0)flag1=1;
		}
		for(j=0;j<n;j++){
			cin>>y;v.push_back(y);
			if(y==1)flag=flag2=j;
		}
		for(j=0;j<n;j++){
			if(v[j]<=i+1&&v[j]!=0){
				y=j+1-(v[j]-1);
				maxn1=max(maxn1,y);//找到值与值的下标加一(从0 开始)的差的最大的值,
				if(j<flag2){
					y=j-v[j]+3+v[n-1];
					maxn2=max(maxn2,y);
				}//判断1位置之前存不存在值与值的下标加一(从0 开始)的差是否有大于1的存在
			}
		}
		while(v[flag2]+1==v[flag2+1]&&(flag2<n-1)){
			flag2++;
		}//从1的位置到最后一个值是升序
		if(flag2==n-1){
			if(flag==0){
				cout<<"0"<<endl;continue;
			} //存在手里没牌,但下面的牌已经排好的情况
			if(flag1==1&&maxn2<=1){
				cout<<n-v[n-1]<<endl;continue;
			} //1+(n-1-下标)=最后一个值,判断1位置之前存不存在值与值的下标加一(从0 开始)的差是否有大于1的存在
		}
		cout<<sum+maxn1<<endl;//就输出第一种情况的值。

	} 
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/91346992
今日推荐