首页 > 其他 > 详细

CF864D Make a Permutation!

时间:2017-10-06 16:42:04      阅读:224      评论:0      收藏:0      [点我收藏+]

思路:

贪心,构造,模拟。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int t[200005], a[200005], vis[200005], n;
 4 int main()
 5 {
 6     while (cin >> n)
 7     {
 8         memset(t, 0, sizeof t); memset(vis, 0, sizeof vis);
 9         int cnt = 0;
10         for (int i = 0; i < n; i++) 
11         {
12             cin >> a[i]; t[a[i]]++;
13             if (t[a[i]] > 1) cnt++;
14         }
15         cout << cnt << endl;
16         queue<int> q;
17         for (int i = 1; i <= n; i++) if (!t[i]) q.push(i);
18         for (int i = 0; i < n; i++)
19         {
20             if (!t[a[i]]) continue;
21             else if (t[a[i]] == 1 && !vis[a[i]]) 
22             {
23                 cout << a[i] << " "; t[a[i]]--;
24             }
25             else 
26             {
27                 if (vis[a[i]]) 
28                 {    
29                     cout << q.front() << " "; q.pop(); 
30                 }
31                 else if (q.front() < a[i])
32                 {
33                     cout << q.front() << " "; q.pop(); 
34                 }
35                 else 
36                 {
37                     cout << a[i] << " "; vis[a[i]] = 1;
38                 }
39                 t[a[i]]--;
40             }
41         }
42         cout << endl;
43     }
44     return 0;
45 }

 

CF864D Make a Permutation!

原文:http://www.cnblogs.com/wangyiming/p/7631703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!