Soldier and Badges

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.