Title Link: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=173144
Test instructions
Enter n number, to make this n the number of different, and only add, the output of at least how much to add.
Case:
1) input
4
1 3 1 4
Output
1
2) input
5
1 2 3) 2 5
Output
2
Thinking Analysis:
Using the intervening spaces method. Minimize circulation.
First, the array is sorted, then the same number is found, and the same number exists in the other arrays C. Because the array c is also sorted, just find the missing number of x that is larger than C.
Finally, just add the number in all array B minus the number in array C, which is the number you want to output.
The source code is as follows:
1#include <iostream>2#include <algorithm>3 #defineMax 30004 using namespacestd;5 intMain ()6 {7 intN,a[max],i,count=0, j=0, k,b[max],c[max],x=0;8Cin>>N;9 for(i=0; i<n;i++)TenCin>>A[i]; OneSort (a,a+n); Ak=a[0]; - for(i=0; i<n;i++) - if(a[i]==a[i+1]) the { -c[x]=A[i]; -X + +; - } + while(1) - { + for(i=j;i<n;i++) A if(k==A[i]) at Break; - if(i>=N) - if(k>C[j]) - { -b[j]=K; -J + +; in } - if(j>=x) to Break; +k++; - } the for(i=0; i<x;i++) *count+=b[i]-C[i]; $cout<<count<<Endl;Panax Notoginseng return 0; -}
Soldier and Badges