CF1172A Nauuo and Cards 题解 贪心

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 n n cards numbered from 1 1 1 to n n n, and they were mixed with another n n n empty cards. She piled up the 2 n 2n 2n cards and drew n n n of them. The n n n cards in Nauuo’s hands are given. The remaining n n 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 n n numbered cards piled up in increasing order (the i i i-th card in the pile from top to bottom is the card i i i) as quickly as possible. Can you tell her the minimum number of operations?

Input

The first line contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1\le n\le 2\cdot 10^5 1n2105) — the number of numbered cards.

The second line contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an ( 0 ≤ a i ≤ n 0\le a_i\le n 0ain) — the initial cards in Nauuo’s hands. 0 0 0 represents an empty card.

The third line contains n n n integers b 1 , b 2 , … , b n b_1,b_2,\ldots,b_n b1,b2,,bn ( 0 ≤ b i ≤ n 0\le b_i\le n 0bin) — the initial cards in the pile, given in order from top to bottom. 0 0 0 represents an empty card.

It is guaranteed that each number from 1 1 1 to n n n appears exactly once, either in a 1.. n a_{1..n} a1..n or b 1.. n b_{1..n} b1..n.

Output

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

Examples

input #1

3
0 2 0
3 0 1

output #1

2

input #2

3
0 2 0
1 0 3

output #2

4

input #3

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

output #3

18

Note

Example 1

We can play the card 2 2 2 and draw the card 3 3 3 in the first operation. After that, we have [ 0 , 3 , 0 ] [0,3,0] [0,3,0] in hands and the cards in the pile are [ 0 , 1 , 2 ] [0,1,2] [0,1,2] from top to bottom.

Then, we play the card 3 3 3 in the second operation. The cards in the pile are [ 1 , 2 , 3 ] [1,2,3] [1,2,3], in which the cards are piled up in increasing order.

Example 2

Play an empty card and draw the card 1 1 1, then play 1 1 1, 2 2 2, 3 3 3 in order.

题目翻译

Nauuo 是一个喜欢玩牌的女孩。

有一天,她正在玩牌,却发现牌里夹杂着一些空牌。

1 1 1 n n n 一共有 n n n 张牌,它们和另外 n n n 张空牌混在一起。她把 2 n 2n 2n 张牌堆在一起,抽出了其中的 n n n 张。现给出诺诺手中的 n n n 张牌。牌堆中剩下的 n n n 张牌也按从上到下的顺序给出。

在一次操作中,她可以选择手中的一张牌并将其打出–将其放在牌堆的底部,然后从牌堆中抽取最上面的一张牌。

Nauuo 想尽快让 n n n 张数字牌按递增顺序堆起来(从上到下牌堆中的第 i i i 张牌是 i i i 张牌)。你能告诉她最少的操作次数吗?

输入格式

第一行包含一个整数 n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1\le n\le 2\cdot 10^5 1n2105 ) - 编号卡片的数量。

第二行包含 n n n 个整数 a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an 0 ≤ a i ≤ n 0\le a_i\le n 0ain )–诺诺手中的初始牌。 0 0 0 代表空牌。

第三行包含 n n n 个整数 b 1 , b 2 , … , b n b_1,b_2,\ldots,b_n b1,b2,,bn 。( 0 ≤ b i ≤ n 0\le b_i\le n 0bin ) -牌堆中的初始牌,从上到下依次排列。 0 0 0 代表一张空牌。

可以保证从 1 1 1 n n n 的每个数字都正好出现一次,要么出现在 a 1.. n a_{1..n} a1..n 中,要么出现在 b 1.. n b_{1..n} b1..n 中。

输出格式

输出包含一个整数,即按递增顺序堆叠 n n n 号纸牌的最少操作次数。

提示

样例 1

在第一次操作中,我们可以出牌 2 2 2 ,抽牌 3 3 3 。之后,我们手中有 [ 0 , 3 , 0 ] [0,3,0] [0,3,0] ,牌堆中的牌从上到下为 [ 0 , 1 , 2 ] [0,1,2] [0,1,2]

然后,我们在第二次操作中出牌 3 3 3 。牌堆中的牌为 [ 1 , 2 , 3 ] [1,2,3] [1,2,3] ,其中牌按递增顺序堆叠。

样例 2

先出一张空牌,然后抽出牌 1 1 1 ,接着依次出 1 1 1 2 2 2 3 3 3

注明

以上来自 C o d e F o r c e s ,翻译: D e e p L 、本人。 以上来自CodeForces,翻译:DeepL、本人。 以上来自CodeForces,翻译:DeepL、本人。

解题思路

首先,在输入时记录牌堆中的每张牌 b i b_i bi 初始在牌堆中的位置为 P b i P_{b_i} Pbi,即将 P b i P_{b_i} Pbi 赋值为 i i i,看以下代码片段比较直观。

	for (int i = 1; i <= n; i++) cin >> b[i], P[b[i]] = i;

考虑是否使用空牌:

  • 如果必须使用空牌,那么最好的办法就是把所有数字牌拿到手,再按顺序摆回牌堆,这样的操作次数是 max ⁡ ( P i + 1 + n − i ) \max(P_i+1+n−i) max(Pi+1+ni)
  • 否则,次数为 P i − i P_i−i Pii P i P_i Pi P P P 数组中第一个满足条件: P i ≥ i P_i\ge i Pii 的元素)。

AC Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int Maxn = 200000 + 5;
int n, a[Maxn], b[Maxn];
int P[Maxn];
int Answer_1, Answer_2;
inline void Init();
inline void Solve();
inline void Work() {
	Init(), Solve();
	cout << (Answer_2 == INT_MAX ? Answer_1 : Answer_2) << endl;
}
signed main() {
	FAST_IO,Work();
	return 0;
}
inline void Init() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> b[i], P[b[i]] = i;
	Answer_1 = INT_MIN, Answer_2 = INT_MAX;
}
inline void Solve() {
	for (int i = 1, t; i <= n; i++) {
		t = P[i] + 1 + n - i;
		if (Answer_1 < t)Answer_1 = t;
	}
	for (int i = 1; i <= n; i++) if (P[i] >= i) {
			Answer_2 = P[i] - i;
			break;
		}
	for (int i = 1; i <= n; i++) if ((P[i] < i && Answer_2 < P[i] - i + n + 1) || (P[i] >= i && Answer_2 != P[i] - i)) {
			Answer_2 = INT_MAX;
			break;
		}
}

温馨提示:以上代码可通过魔改跑出 31 31 31ms的最优解。

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
国赛题目一般来源于科学与工程技术、人文与社会科学等领域经过适当简化加工的实际问题。参赛者不需要预先掌握深入的专门知识,只需要学过高等学校的数学基础课程。对于解题思路,可以根据题目要求进行以下步骤来完成建模和求解: 1. 理解题目:仔细阅读题目,理解问题的背景和要求。注意提取关键信息,明确问题的目标和约束条件。 2. 建立数学模型:根据题目的描述和要求,将实际问题转化为数学模型。可以使用已知的数学理论、公式和方法,结合问题的特点进行建模。根据问题类型,可以将问题归类为分类问题、优化问题、预测问题或评价问题。 3. 求解模型:根据建立的数学模型,使用适当的数学工具和方法进行求解。可能需要进行数值计算、优化算法或统计分析等操作,以得到问题的解答。 4. 分析和检验结果:对求解结果进行分析和检验,验证其合理性和正确性。可以通过对比实际数据或进行敏感性分析来评估模型的准确性和可靠性。 5. 模型的改进:根据对结果的分析和检验,对模型进行改进。可以尝试不同的假设或调整参数,以提高模型的性能和适应性。 综上所述,解题思路主要包括理解题目、建立数学模型、求解模型、分析和检验结果以及模型的改进。具体的解题思路会根据不同的题目和问题类型有所差异,建议参赛者根据具体题目要求和自身的数学知识经验,灵活运用数学方法和工具,进行问题的建模和求解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值