fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class key {
  8. int a, b, c, d;
  9.  
  10. public key(int a, int b, int c, int d) {
  11. this.a = a;
  12. this.b = b;
  13. this.c = c;
  14. this.d = d;
  15. }
  16.  
  17. @Override
  18. public int hashCode() {
  19. return Objects.hash(a, b, c, d);
  20. }
  21.  
  22. @Override
  23. public boolean equals(Object obj) {
  24. if (this == obj) return true;
  25. if (obj == null || getClass() != obj.getClass()) return false;
  26. key k = (key) obj;
  27. return a == k.a && b == k.b && c == k.c && d == k.d;
  28. }
  29. }
  30. class Ideone
  31. {
  32. public static void main (String[] args) throws java.lang.Exception
  33. {
  34. Scanner sc = new Scanner(System.in);
  35.  
  36. int n = sc.nextInt();
  37. int a = sc.nextInt();
  38. int b = sc.nextInt();
  39. int c = sc.nextInt();
  40. int d = sc.nextInt();
  41. int e = sc.nextInt();
  42.  
  43. int[] arr = new int[n];
  44. for (int i = 0; i < n; i++) {
  45. arr[i] = sc.nextInt();
  46. }
  47.  
  48. HashMap<key, Integer> mp = new HashMap<>();
  49. mp.put(new key(0, 0, 0, 0), 1);
  50.  
  51. int ax = 0, bx = 0, cx = 0, dx = 0, ex = 0;
  52. long count = 0;
  53.  
  54. for (int i = 0; i < n; i++) {
  55. if (arr[i] == a)
  56. ax++;
  57. else if (arr[i] == b)
  58. bx++;
  59. else if (arr[i] == c)
  60. cx++;
  61. else if (arr[i] == d)
  62. dx++;
  63. else if (arr[i] == e)
  64. ex++;
  65.  
  66. int d1 = bx - ax;
  67. int d2 = cx - bx;
  68. int d3 = dx - cx;
  69. int d4 = ex - dx;
  70.  
  71. key k = new key(d1, d2, d3, d4);
  72.  
  73. count += mp.getOrDefault(k, 0);
  74. mp.put(k, mp.getOrDefault(k, 0) + 1);
  75. }
  76.  
  77. System.out.println(count);
  78. sc.close();
  79. }
  80. }
Success #stdin #stdout 0.14s 54568KB
stdin
10
1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
stdout
7