fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. // your code goes here
  11. int n = Integer.parseInt(br.readLine().trim());
  12. String temp[] = br.readLine().trim().split(" ");
  13. int arr[] = new int[n];
  14. for(int i = 0 ; i<n ; i++) arr[i]=Integer.parseInt(temp[i]);
  15. int cnt[] = new int[1000100];
  16. Arrays.fill(cnt,0);
  17. for(int i = 0 ; i< n;i++) cnt[arr[i]]++;
  18. int ans=0;
  19. for(int i = 0 ; i< 1000099; i++){
  20. if(cnt[i]%2==0){
  21. cnt[i+1]+=cnt[i]/2;
  22. cnt[i]=0;
  23. }
  24. else{
  25. cnt[i+1]+=cnt[i]/2;
  26. cnt[i]=1;
  27. ans++;
  28. }
  29. }
  30. pw.print(ans);
  31. pw.flush();
  32. }
  33. }
Success #stdin #stdout 0.14s 320512KB
stdin
4
0 1 2 3
stdout
4