fork download
  1. /* package codechef; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Codechef
  9. {
  10.  
  11. public static void main(String[] args) throws IOException {
  12. FastReader in = new FastReader(System.in);
  13.  
  14. int n=in.nextInt();
  15. int arr[]=new int[n];
  16.  
  17. for(int i=0;i<n;i++)
  18. arr[i]=in.nextInt();
  19. boolean found=true;
  20. long ans=0;
  21. while(found){
  22.  
  23. found=false;
  24. int x=-1,y=-1;
  25. int l=Integer.MAX_VALUE;
  26. for(int i=0;i<n;i++){
  27. int prev=arr[i];
  28. for(int j=i+1;j<n;j++){
  29. if(arr[j]==arr[i] && arr[i]!=prev){
  30. if(l>(j-i+1)){
  31. x=i;
  32. y=j;
  33. found=true;
  34. l=j-i+1;
  35. break;
  36. }
  37.  
  38. }
  39. prev=arr[j];
  40. }
  41. }
  42. if(found){
  43. HashSet<Integer> set=new HashSet<Integer>();
  44. for(int i=x+1;i<y;i++){
  45.  
  46. set.add(arr[i]);
  47. arr[i]=arr[i-1];
  48. }
  49. ans+=(set.size());
  50. }
  51. else{
  52. HashSet<Integer> set=new HashSet<Integer>();
  53. for(int i=0;i<n;i++){
  54. set.add(arr[i]);
  55. }
  56. ans+=(set.size()-1);
  57. break;
  58. }
  59. }
  60. System.out.println(ans);
  61.  
  62. }
  63. }
  64.  
  65. class FastReader {
  66.  
  67. byte[] buf = new byte[2048];
  68. int index, total;
  69.  
  70. FastReader(InputStream is) {
  71. in = is;
  72. }
  73.  
  74. int scan() throws IOException {
  75. if (index >= total) {
  76. index = 0;
  77. total = in.read(buf);
  78. if (total <= 0) {
  79. return -1;
  80. }
  81. }
  82. return buf[index++];
  83. }
  84.  
  85. String next() throws IOException {
  86. int c;
  87. for (c = scan(); c <= 32; c = scan()) ;
  88. StringBuilder sb = new StringBuilder();
  89. for (; c > 32; c = scan()) {
  90. sb.append((char) c);
  91. }
  92. return sb.toString();
  93. }
  94.  
  95. String nextLine() throws IOException {
  96. int c;
  97. for (c = scan(); c <= 32; c = scan()) ;
  98. StringBuilder sb = new StringBuilder();
  99. for (; c != 10 && c != 13; c = scan()) {
  100. sb.append((char) c);
  101. }
  102. return sb.toString();
  103. }
  104.  
  105. char nextChar() throws IOException {
  106. int c;
  107. for (c = scan(); c <= 32; c = scan()) ;
  108. return (char) c;
  109. }
  110.  
  111. int nextInt() throws IOException {
  112. int c, val = 0;
  113. for (c = scan(); c <= 32; c = scan()) ;
  114. boolean neg = c == '-';
  115. if (c == '-' || c == '+') {
  116. c = scan();
  117. }
  118. for (; c >= '0' && c <= '9'; c = scan()) {
  119. val = (val << 3) + (val << 1) + (c & 15);
  120. }
  121. return neg ? -val : val;
  122. }
  123.  
  124. long nextLong() throws IOException {
  125. int c;
  126. long val = 0;
  127. for (c = scan(); c <= 32; c = scan()) ;
  128. boolean neg = c == '-';
  129. if (c == '-' || c == '+') {
  130. c = scan();
  131. }
  132. for (; c >= '0' && c <= '9'; c = scan()) {
  133. val = (val << 3) + (val << 1) + (c & 15);
  134. }
  135. return neg ? -val : val;
  136. }
  137. }
Success #stdin #stdout 0.06s 32356KB
stdin
10
1 1 1 1 1 1 2 1 3 1
stdout
2