codeforce 436 D贪心思维题Make a Permutation!
2 seconds
256 megabytes
standard input
standard output
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.
In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
- 4
3 2 2 3
- 2
1 2 4 3
- 6
4 5 6 3 2 1
- 0
4 5 6 3 2 1
- 10
6 8 4 6 7 1 6 3 4 5
- 3
2 8 4 6 7 1 9 3 10 5- http://www.cnblogs.com/GraceSkyer/p/7591345.html
- #include<cstdio>
- #include<vector>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int N=2e5+;
- int vis[N];
- bool used[N];
- int p[N],num[N];
- struct node
- {
- int pos,val;
- bool operator < (const node &A)const
- {
- return pos<A.pos;
- }
- } q[N];
- int ptos[N];
- int main()
- {
- int n,tot1=,tot2=,x;
- scanf("%d",&n);
- for(int i=; i<=n; ++i)
- {
- scanf("%d",&x);
- num[i]=x;
- if(!vis[x]) vis[x]=;
- else if(vis[x]==)
- {
- vis[x]=;
- q[tot1].val=x;
- q[tot1++].pos=ptos[x];
- q[tot1].val=x;
- q[tot1++].pos=i;
- }
- else
- {
- ++vis[x];
- q[tot1].val=x;
- q[tot1++].pos=i;
- }
- ptos[x]=i;
- }
- for(int i=; i<=n; ++i) if(!vis[i]) p[tot2++]=i;
- sort(q,q+tot1);
- int ct=;
- for(int i=;i<tot1&&ct<tot2;++i) {
- if(!used[q[i].val]&&q[i].val<p[ct]) used[q[i].val]=;
- else if(vis[q[i].val]!=) {--vis[q[i].val]; num[q[i].pos]=p[ct++];}
- }
- printf("%d\n",tot2);
- for(int i=; i<=n; ++i) printf("%d ",num[i]);
- puts("");
- }
codeforce 436 D贪心思维题Make a Permutation!的更多相关文章
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands
看似区间数据结构的一道题 题目描述 Bessie and her friends are playing a unique version of poker involving a deck with ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*
1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...
- 51nod 1563 坐标轴上的最大团(今日gg模拟第一题) | 线段覆盖 贪心 思维题
51nod 1563 坐标轴上的最大团 坐标轴上有n个点,每个点有一个权值.第i个点的坐标是 xi ,权值是 wi .现在对这些点建图.对于点对 (i,j) ,如果 |xi−xj|≥wi+wj ,那么 ...
- HDU 6047 贪心思维题
Maximum Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
随机推荐
- QML-密码管理器
Intro 年初刚学Qml时写的密码管理器.用到Socket通信.AES加密等.UI采用Material Design,并实现了Android App的一些常见基本功能,如下拉刷新.弹出提示.再按一次 ...
- jQuery dataTables四种数据来源[转]
2019独角兽企业重金招聘Python工程师标准>>> 四种数据来源 对于 dataTables 来说,支持四种表格数据来源. 最为基本的就是来源于网页,网页被浏览器解析为 DOM ...
- NPM采用Rust以消除性能瓶颈
Npam的指数级增长促使npm工程团队从Node.js切换到Rust,以处理那些将成为性能瓶颈的CPU绑定任务.最近的一份白皮书概述了在Rust中开发这个新服务,以及将其投入生产一年多的经验. 大部分 ...
- qcow2快照原理
关键术语:cluster 一个Qcow2 img文件由固定大小的单元组成,该单元称为cluster,默认大小为65536bytes/64Ksector 数据块读写的最小单元,大小为512字节host ...
- C语言程序设计实验报告(第一次实验)
C程序设计实验报告 实验项目:C语言程序设计教程实验1.3.2:1.3.3:1.3.4:2.3.1:2.3.2 姓名:赖瑾 实验地点:家 实验时间:2020.2.25 目录 C程序设计实验报告 一.实 ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- D. Misha, Grisha and Underground 树链剖分
D. Misha, Grisha and Underground 这个题目算一个树链剖分的裸题,但是这个时间复杂度注意优化. 这个题目可以选择树剖+线段树,时间复杂度有点高,比较这个本身就有n*log ...
- 单调队列+二分 G - Queue 小阳买水果
B. Queue 这个题目会做的很偶然,突然想到的,因为我们要求离这只海象的最远的比他年轻的海象,这个年轻的海象可以用单调栈维护. 就是从前往后遍历一遍,单调栈里面存年龄从小往大的海象,这个为什么这么 ...
- spark是怎么从RDD升级到DataFrame的?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是spark专题的第五篇,我们来看看DataFrame. 用过Python做过机器学习的同学对Python当中pandas当中的Data ...
- Linux内核驱动学习(四)Platform设备驱动模型
Linux platform设备驱动模型 文章目录 Linux platform设备驱动模型 前言 框架 设备与驱动的分离 设备(device) 驱动(driver) 匹配(match) 参考 前言 ...