fork download
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. class Main {
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10. int n = sc.nextInt();
  11.  
  12. List<Long> pMales = new ArrayList<>();
  13. List<Long> nMales = new ArrayList<>();
  14. for (int i = 0; i < n; i++) {
  15. long height = sc.nextLong();
  16. if (height < 0) {
  17. nMales.add(Math.abs(height));
  18. } else {
  19. pMales.add(height);
  20. }
  21. }
  22.  
  23. List<Long> pFemales = new ArrayList<>();
  24. List<Long> nFemales = new ArrayList<>();
  25. for (int i = 0; i < n; i++) {
  26. long height = sc.nextLong();
  27. if (height < 0) {
  28. nFemales.add(Math.abs(height));
  29. } else {
  30. pFemales.add(height);
  31. }
  32. }
  33.  
  34. sc.close();
  35.  
  36. int pairs1 = countPairs(pMales, nFemales);
  37. int pairs2 = countPairs(pFemales, nMales);
  38.  
  39. System.out.println(pairs1 + pairs2);
  40. }
  41.  
  42. private static int countPairs(List<Long> shorterGroup, List<Long> tallerGroup) {
  43. Collections.sort(shorterGroup);
  44. Collections.sort(tallerGroup);
  45.  
  46. int pairs = 0;
  47. int shorterPtr = 0;
  48. int tallerPtr = 0;
  49.  
  50. while (shorterPtr < shorterGroup.size() && tallerPtr < tallerGroup.size()) {
  51. if (tallerGroup.get(tallerPtr) > shorterGroup.get(shorterPtr)) {
  52. pairs++;
  53. shorterPtr++;
  54. tallerPtr++;
  55. } else {
  56. tallerPtr++;
  57. }
  58. }
  59. return pairs;
  60. }
  61. }
  62.  
Success #stdin #stdout 0.16s 54604KB
stdin
7
-1900 -2000 -2500 1500 1600 2500 -2500 
1800 -1550 2200 -1550 2100 -2500 -1700 
stdout
5