题目: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. 【Android】手机号码获取问题

    手机号码不是所有的都能获取.只是有一部分可以拿到.这个是由于移动运营商没有把手机号码的数据写入到sim卡中.SIM卡只有唯一的编号,供网络与设备识别那就是IMSI号码,手机的信号也可以说是通过这个号码 ...

  2. 提供基于Lesktop的IM二次开发,联系QQ:87172811

    提供基于Lesktop的IM二次开发,联系QQ:87172811

  3. 完成端口(Completion Port)详解(超级长,超级清楚)

    http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html

  4. Gimp制作圆角透明图片

    用蒙版制作圆角透明图片,步骤如下: 1,用Gimp(2.8版本)打开图片 2,在图层窗口右键当前图层创建蒙版 3,选择蒙版类型黑色(全透明) 4,结果如下 5,用圆角矩形选择工具选择图片,设置圆角半径 ...

  5. Intersecting Lines(数学)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12844   Accepted: 57 ...

  6. 74HC595的中文资料

    74HC595--具有三态输出锁存功能的8位串行输入.串行/并行输出移位寄存器 本文翻译自NXP的74HC595的datasheet 74HC595和74HCT595是带有存储寄存器和三态输出的8位串 ...

  7. GDB调试之暂停

    暂停机制: 有3种方式可以通知GDB暂停程序的执行. a.断点: 通知GDB在程序中的特定位置暂停执行: b.监视点:通知GDB当特定内存位置(或者涉及一个或多个位置的表达式)的值发生变化时暂停执行: ...

  8. 去掉word2007回车后自动编号

    1. 在使用word自动编号时,回车续写上一编号内容可能会行距很宽,这时候使用shift+回车不会自动下一编号,而且是正常行距 取消回车自动编号: 打开Word文档,单击菜单栏上的[格式]→[自动套用 ...

  9. linux命令 收集

    https://jaywcjlove.github.io/linux-command/ 源码:https://github.com/jaywcjlove/linux-command Linux思维导图 ...

  10. Null指针

    C++ Null 指针 C++ 指针 C++ 指针 在变量声明的时候,如果没有确切的地址可以赋值,为指针变量赋一个 NULL 值是一个良好的编程习惯.赋为 NULL 值的指针被称为空指针. NULL ...