Title: Click Here
Test instructions: give you n, then n number, n number may repeat, may not be 1 to n in the number. Then you use the fewest number of changes, let the sequence contain 1 to n all numbers, and output the final sequence.
Analysis: Greed.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intM = 1e5+5;4 5 intN;6 intA[M];//given sequence7 intMARK[M];//Mark[i] Indicates the number of occurrences of I in a given sequence8 intNOTAP[M];//elements that are not present in the given sequence9 BOOLfirstout;Ten One voidPrintintx) {//output Format Control PS: tested on CF, regardless of the format can be AC A if(firstout) { -printf"%d", x); -Firstout =false; the } - Else -printf"%d", x); - } + - intMain () { + while(~SCANF ("%d", &N)) { Amemset (Mark,0,sizeof(Mark)); atmemset (Notap,0,sizeof(Notap)); - for(intI=1; i<=n; i++ ) { -scanf"%d", A +i); -mark[a[i]]++; - } - intCNT =1; in for(intI=1; i<=n; i++ ) - if( !Mark[i]) { toNOTAP[CNT] =i; +cnt++; - } theFirstout =true; * inttop =1; $ for(intI=1; i<=n; i++ )Panax Notoginseng if(Mark[a[i]] = =1&& A[i] <= N)//this number is in the given sequence and the range is valid - print (a[i]); the Else { +Print (Notap[top]);//Otherwise, a number is ejected from the sequence that is not Atop++; themark[a[i]]--; + } -printf"\ n"); $ } $ return 0; -}
Codeforces Round #315 (Div. 2) 569B Inventory