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;//就輸出第一種情況的值。

	} 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章