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的最优解。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/691625.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从 AGP 4.1.2 到 7.5.1——XmlParser、GPathResult、QName 过时

新年首发&#xff0c; 去年的问题&#xff0c;今年解决~ 问题 & 排查 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task :app:processCommonReleaseManifest. > org.xml.sax.SAXParseException; lineNumber: 1; columnNu…

【申请体验Sora】OpenAI Red Teaming Network application

网址&#xff1a;https://openai.com/form/red-teaming-network 使用Gmail &#xff0c; 国家选美国 两个问题&#xff1a; Why are you interested in joining the OpenAI Red Teaming Network? I’m eager to experience the powerful allure of Sora, which I believe wi…

postgresql 文件结构(一) 数据库、表对应的文件

1、问题 甲方要求提供数据库数据量大小&#xff0c;由于各个业务数据库共用一个postgres&#xff0c;因此想把每个数据库占用的空间都统计一下。 2、查找物理存储文件目录 如下图所示&#xff0c;可以查询表、库的物理存储文件名称 -- 查询表对应的文件 select oid,relname…

leetcode 01背包问题

典型的01背包问题可以暴力求解&#xff0c;直接将所有可能全部遍历然后挑选符合条件的即可&#xff0c;但这样时间复杂度过高&#xff0c;有2的n次方。 所以我们在这里采用动态规划的方式来做&#xff0c;并且&#xff0c;我们可以采用二维数组或者一维数组来做。 二维数组&a…

每天一个数据分析题(一百五十九)

主成分分析&#xff08;PCA&#xff09;不适宜单独用于哪种情况&#xff1f; A. 员工绩效的评估和排名 B. 描述产品情况&#xff0c;如子公司的业务发展状况 C. 消除数据分析中的共线性问题 D. PCA可以作为异常识别的算法使用 题目来源于CDA模拟题库 点击此处获取答案

PMP每日十题-2024年2月20日

项目经理发现&#xff0c;一些团队成员不清楚其职责和活动&#xff0c;哪一项工具或技术能够帮助项目经理明确定义这些职责和活动&#xff1f; A、组织图 B、工作分解结构 C、执行&#xff0c;负责&#xff0c;咨询和知情矩阵&#xff08;RACI&#xff09; D、项目成员派工单 答…

供水管网管道爆管事故发生原因及控制措施

经济的快速发展及城镇化的不断推进使得我国的城镇数量及规模都在不断地扩大。供水系统是城镇基础设施中 的重要的一环。现今在我国各城市中已经建立了相对完善的城市供水管网体系。但是在供水管网运行过程中爆管问题时 有发生&#xff0c;从而对城市用水的正常供应以及民众的正…

如何在 Angular 中使用 ng-container 元素

简介 ng-container 是 Angular 2 中可用的一个元素&#xff0c;可以作为结构指令的宿主。 在本文中&#xff0c;您将探讨可以使用 ng-container 解决的场景。 先决条件 如果您想跟着本文学习&#xff0c;您需要&#xff1a; 熟悉 DOM 的结构。要了解更多&#xff0c;请查看…

2024前端面试准备之Vue3篇

全文链接!!!!!! 1. Vue3相比Vue2有哪些改进? Composition API: Vue3引入了Composition API,使得组件的逻辑更加清晰和灵活。开发者可以根据功能逻辑将代码按照功能进行组织,而不是按照选项的方式。 更快的渲染性能:Vue3使用了Proxy代理对象来实现数据的响应式,相比…

Redis 缓存(Cache)

什么是缓存 缓存(cache)是计算机中的一个经典的概念在很多场景中都会涉及到。 核心思路就是把一些常用的数据放到触手可及(访问速度更快)的地方&#xff0c;方便随时读取。 这里所说的“触手可及”是个相对的概念 我们知道&#xff0c;对于硬件的访问速度来说&#xff0c;通常…

vue3实现瀑布流布局组件

先看效果图 直接上代码 utils.js // 用于模拟接口请求 export const getRemoteData (data 获取数据, time 2000) > {return new Promise((resolve) > {setTimeout(() > {console.log(模拟获取接口数据, data)resolve(data)}, time)}) }// 获取数组随机项 export…

给label-studio 配置sam(segment anything)ml 记录

给label-studio 配置sam&#xff08;segment anything&#xff09;ml 后端记录 配置ml后台下载代码下载模型文件创建环境模型转换后端服务启动 配置label-studio 前端配置模型后端连接配置标注模板标注界面使用 参考链接 配置ml后台 下载代码 git clone https://github.com/H…

AT24C02(I2C总线)通信的学习

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、存储器介绍二、AT24C02芯片二、I2C总线I2C电路规范I2C时序结构I2C数据帧AT24C02数据帧 总结 前言 学习AT24C02(I2C总线)芯片 一、存储器介绍 RAM&#xf…

洛谷P1420最长练号

题目描述 输入长度为 n 的一个正整数序列&#xff0c;要求输出序列中最长连号的长度。 连号指在序列中&#xff0c;从小到大的连续自然数。 输入格式 第一行&#xff0c;一个整数 n。 第二行&#xff0c;n 个整数 ai​&#xff0c;之间用空格隔开。 输出格式 一个数&…

QT-地形3D

QT-地形3D 一、 演示效果二、关键程序三、下载链接 一、 演示效果 二、关键程序 #include "ShaderProgram.h"namespace t3d::core {void ShaderProgram::init() {initializeOpenGLFunctions();loadShaders(); }void ShaderProgram::addShader(const QString &fil…

Qt中的事件该如何学习?(附带案例)

概述&#xff1a; 事件是Qt中比较重要的一部分&#xff0c;在初期如果理解不当学习可能会比较困难&#xff0c;这里提一嘴当初教我的那位老师水平是真的高&#xff0c;让我很轻易的就理解了事件的概念。 在平时我们见到那些界面上的某些快捷键就有可能是事件做的&#xff0c;…

2024年华为OD机试真题-计算面积-Python-OD统一考试(C卷)

题目描述: 绘图机器的绘图笔初始位置在原点(0, 0),机器启动后其绘图笔按下面规则绘制直线: 1)尝试沿着横向坐标轴正向绘制直线,直到给定的终点值E。 2)期间可通过指令在纵坐标轴方向进行偏移,并同时绘制直线,偏移后按规则1 绘制直线;指令的格式为X offsetY,表示在横…

计算机二级C语言的注意事项及相应真题-5-程序修改

目录 41.累加链表结点数据域中的数据作为函数值返回42.根据整型形参m&#xff0c;计算如下公式的值43.删除数列中值为x的元素44.从N个字符串中找出最长的那个串&#xff0c;并将其地址作为函数值返回45.将两个长度相等的纯数字字符串当作两个加数&#xff0c;求其代表的数值之和…

欢迎来到IT时代----盘点曾经爆火全网的计算机电影

计算机专业必看的几部电影 计算机专业必看的几部电影&#xff0c;就像一场精彩的编程盛宴&#xff01;《黑客帝国》让你穿越虚拟世界&#xff0c;感受高科技的魅力&#xff1b;《社交网络》揭示了互联网巨头的创业之路&#xff0c;《源代码》带你穿越时间解救世界&#xff0c;这…

【动态规划】【字符串】2167移除所有载有违禁货物车厢所需的最少时间

作者推荐 【深度优先搜索】【树】【有向图】【推荐】685. 冗余连接 II 本文涉及知识点 动态规划汇总 LeetCode2167移除所有载有违禁货物车厢所需的最少时间 给你一个下标从 0 开始的二进制字符串 s &#xff0c;表示一个列车车厢序列。s[i] ‘0’ 表示第 i 节车厢 不 含违…