本文主要是介绍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 1≤n≤2⋅105) — 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 0≤ai≤n) — 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 0≤bi≤n) — 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 1≤n≤2⋅105 ) - 编号卡片的数量。
第二行包含 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 0≤ai≤n )–诺诺手中的初始牌。 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 0≤bi≤n ) -牌堆中的初始牌,从上到下依次排列。 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+n−i);
- 否则,次数为 P i − i P_i−i Pi−i( P i P_i Pi 为 P P P 数组中第一个满足条件: P i ≥ i P_i\ge i Pi≥i 的元素)。
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的最优解。
这篇关于CF1172A Nauuo and Cards 题解 贪心的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!