题目: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. 操作百度API

    string json = ""; try { //虽然两者都是异步请求事件,但是WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程 WebCli ...

  2. Python主要模块和常用方法简览

    原文地址:http://blog.csdn.net/hwhjava/article/details/22284399 PY核心模块方法1. os模块: os.remove() #删除文件 os.unl ...

  3. verilog中读取文件中的字符串_modelsim高级仿真

    今天给个程序大家玩玩.因为今天遇到一个问题,就是要向UART发送指令,指令非常多,都是字符串.一直copy 函数 UART ("COMM_1");  UART ("COM ...

  4. photoshop自动切图

    自动切图 前面的话 随着photoshop版本的不断升级,软件本身增加了很多新的功能,也为切图工作增加了很多的便利.photoshop最新的版本新增了自动切图功能,本文将详细介绍photoshop的这 ...

  5. LintCode-乱序字符串

    题目描述: 给出一个字符串数组S,找到其中所有的乱序字符串(Anagram).如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中. 注意事项 所有的字符串都只包含小写字 ...

  6. Mac OS X Mavericks or Yosemite 安装Nginx、PHP、Mysql、phpMyAdmin

    翻译:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/ 最 ...

  7. 基于Visual C++2013拆解世界五百强面试题--题16-进制分析

    清写出下列代码的输出内容 #include <stdio.h> int main() { int a = -1, b = -12, c = -123, d = -1234; printf( ...

  8. HDU Computer Transformation1041 题解

    Problem Description A sequence consisting of one digit, the number 1 is initially written into a com ...

  9. C#静态方法

    C#静态方法     学习C#静态函数及变量的一个精典例子与代码 (1)用于对静态字段.只读字段等的初始化. (2)添加static关键字,不能添加访问修饰符,因为静态构造函数都是私有的. (3)类的 ...

  10. Android的回调模拟

    想要彻底理解安卓中用的回调,最好的办法是自己写一个类似的实现安卓中回调功能的实现方法. 我自己写了一个可以实现setOnClickListener回调的工程: 具体目录: 工程源码的具体地址:http ...