CF864D Make a Permutation!
阿新 • • 發佈:2017-10-06
strong clas ont 模擬 logs 思路 () code i++
思路:
貪心,構造,模擬。
實現:
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!