Codeforces Round #564 (Div. 2)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.

Examples
inputCopy
3
0 2 0
3 0 1
outputCopy
2
inputCopy
3
0 2 0
1 0 3
outputCopy
4
inputCopy
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
outputCopy
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.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
#define dd double
using namespace std;

ll a[200005];
ll b[200005];

int main() {
	ios::sync_with_stdio(0);
	ll n; cin >> n;
	ll flag = 0;
	ll flag1 = 2;
	ll flag2 = 0;
	ll po;
	for (ll i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1, a + 1 + n);
	for (ll i = 1; i <= n; i++) {
		cin >> b[i];
		if (b[i] == 1) {
			po = i;
			flag = 1;
			continue;
		}
		if (flag == 1 && flag2 == 0) {
			if (b[i] == flag1) {
				flag1++;
			}
			else {
				flag1 = 0;
				flag2 = 1;
			}
		}
	}
	ll count = 0;
	ll flag4 = 0;
	ll po1;
	if (flag == 1) {
		if (flag2 == 0) {
			flag1--;
			ll q = 0;
			ll t1 = 0;
			ll zzz = b[n] + 1;
			for (ll i = 1; i <= n - flag1; i++) {
				if (b[i] == 0) {
					continue;
				}
				if (b[i] > b[n] + 1) {
					continue;
				}
				else {
					t1 = 1;
					break;
				}
			}
			for (ll i = 1; i <= n; i++) {
				if (a[i] != 0) {
					po1 = a[i];
					break;
				}
			}
			for (ll i = 1; i < po; i++) {
				if (b[i] == 0) {
					po1++;
					continue;
				}
				if (b[i] > po1) {
					po1++;
					continue;
				}
				else {
					flag4 = 1;
					break;
				}
			}
			if (t1 == 0) {
				if (flag4 == 1) {
					cout << n - flag1 + n + 1 << endl;
				}
				else {
					cout << n - flag1 << endl;
				}
			}
			else {
				count += po;
				count += n;
				cout << count << endl;
			}
		}
		else {
			count += po;
			ll z = 1;
			for (ll i = po + 1; i <= n; i++) {
				if (b[i] == 0) {
					z++;
					continue;
				}
				if (b[i] > z) {
					z++;
					continue;
				}
				else {
					count = count + (z + 1 - b[i]);
					z = b[i];
				}
			}
			cout << count + n<< endl;
		}
	}
	if (flag == 0) {
		ll z = 1;
		for (ll i = 1; i <= n; i++) {
			if (b[i] == 0) {
				z++;
				continue;
			}
			if (b[i] > z) {
				z++;
				continue;
			}
			else {
				count = count + (z - (b[i] - 1));
				z = b[i];
			}
		}
		cout << count + n << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值