题目:Click here

题意:给你n,然后n个数,n个数中可能重复,可能不是1到n中的数。然后你用最少的改变数,让这个序列包含1到n所有数,并输出最后的序列。

分析:贪心。

 #include <bits/stdc++.h>
using namespace std;
const int M = 1e5+; int n;
int a[M]; // 给定序列
int mark[M]; // mark[i] 表示i在给定序列中的出现次数
int notap[M]; // 给的序列中没有出现的元素
bool firstout; void print( int x ) { // 输出格式控制 ps:在CF上测试了一下,不管这个格式也能AC
if( firstout ) {
printf("%d", x );
firstout = false;
}
else
printf(" %d", x );
} int main() {
while( ~scanf("%d", &n ) ) {
memset( mark, , sizeof(mark) );
memset( notap, , sizeof(notap) );
for( int i=; i<=n; i++ ) {
scanf("%d", a+i );
mark[a[i]]++;
}
int cnt = ;
for( int i=; i<=n; i++ )
if( !mark[i] ) {
notap[cnt] = i;
cnt++;
}
firstout = true;
int top = ;
for( int i=; i<=n; i++ )
if( mark[a[i]] == && a[i] <= n ) // 给定序列中有这个数并且范围合法
print( a[i] );
else {
print( notap[top] ); // 不然从没有的序列中弹出一个数
top++;
mark[a[i]]--;
}
printf("\n");
}
return ;
}

Codeforces Round #315 (Div. 2B) 569B Inventory 贪心的更多相关文章

  1. Codeforces Round #315 (Div. 2)【贪心/重排去掉大于n的元素和替换重复的元素】

    B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. Codeforces Round #316 (Div. 2B) 570B Simple Game 贪心

    题目:Click here #include <bits/stdc++.h> using namespace std; typedef long long ll; const int IN ...

  3. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...

  4. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  5. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  6. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  7. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  8. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  9. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

随机推荐

  1. php开发中的一些常用统计的日期

    <?php echo '<br>今天:<br>'; echo date("Y-m-d",strtotime("now")), &q ...

  2. php不同版本特性记录

    最近在用php开发时项目中遇到了版本问题,特此记录下php不同版本的一些特性记录,以备忘. 一:php5.3中的新特性 1)开始支持命名空间(Namespace) 2)支持延迟静态绑定(Late St ...

  3. <转> Python的优雅技巧

    枚举 不要这么做: 全选复制放进笔记 i = 0 for item in iterable: print i, item i += 1 而是这样: 全选复制放进笔记 for i, item in en ...

  4. 转 释一首美国民谣:沉默之音(The Sound Of Silence)

    Ask not what your country can do for you , ask what you can do for your country.    六十年代对美国而言是个多事之秋的 ...

  5. delphi 文件夹权限设置(执行一个小脚本的笨办法)

    如题,研究了一天,也没再网上找到比较好的方式,自己做了一个.方法如下: 1.创建一个 cmd 命令文件.2.调用该命令. 代码如下:   S:='echo y|cacls h: /t /c /g ev ...

  6. What is a good EPUB reader on Linux

    Last updated on August 20, 2014 Authored by Adrien Brochard 12 Comments If the habit on reading book ...

  7. Oracle 经典SQL 专为笔试准备

    相信把这99条sql搞定,Oracle基本笔试简直就像玩的一样(史上最全,最经典的入门级Oracle查询语句) 1. select * from emp; 2. select empno, ename ...

  8. Android ScrollView嵌套HorizontalScrollView 滑动问题 ScrollView包括GridView显示问题

    今天项目使用到ScrollView嵌套HorizontalScrollView,ScrollView里包括GridView,发现几个问题非常经典.在此记录: 问题1.ScrollView嵌套Horiz ...

  9. 比较和排序 IComparable And IComparer

    1.List<Student>默认排序 为类创建默认排序实现IComparable,此代码的实现为年龄升序 using System; using System.Collections.G ...

  10. 学习validate

    jQuery Validate (转自http://www.w3cschool.cc/jquery/jquery-plugin-validate.html?utm_source=tuicool) jQ ...