fork download
  1. /* package whatever; // 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 Ideone{
  9. public static int perfectMatch(List<Long> n1,List<Long> n2){
  10. int j = 0;
  11. int i = 0;
  12. int c = 0;
  13. while (i<n1.size() && j<n2.size()) {
  14. if (n2.get(j) > n1.get(i)) {
  15. c++;
  16. j++;
  17. i++;
  18. } else {
  19. j++;
  20. }
  21. }
  22. return c;
  23. }
  24. public static void main (String[] args) throws java.lang.Exception
  25. {
  26. Scanner sc = new Scanner(System.in);
  27. long n = sc.nextLong();
  28. List<Long> pm = new ArrayList<>();
  29. List<Long> nm = new ArrayList<>();
  30.  
  31. long c = 0;
  32. for(long i=0;i<n;i++) {
  33. long y = sc.nextLong();
  34. if (y < 0) {
  35. nm.add(Math.abs(y));
  36. } else {
  37. pm.add(y);
  38. }
  39. }
  40.  
  41. List<Long> pf = new ArrayList<>();
  42. List<Long> nf = new ArrayList<>();
  43. for(long i=0;i<n;i++) {
  44. long y = sc.nextLong();
  45. if (y < 0) {
  46. nf.add(Math.abs(y));
  47. } else {
  48. pf.add(y);
  49. }
  50. }
  51.  
  52. if (pm.size() > 0)Collections.sort(pm);
  53. if (nm.size() > 0)Collections.sort(nm);
  54. if (pf.size() > 0)Collections.sort(pf);
  55. if (nf.size() > 0)Collections.sort(nf);
  56.  
  57. int c1 = perfectMatch(pf,nm);
  58. int c2 = perfectMatch(pm,nf);
  59. System.out.println(c1+c2);
  60. }
  61. }
Success #stdin #stdout 0.12s 56544KB
stdin
7
-1900 -2000 -2500 1500 1600 2500 -2500 
1800 -1550 2200 -1550 2100 -2500 -1700 
stdout
5