fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4.  
  5. int main() {
  6.  
  7. int n;
  8. cin >> n;
  9.  
  10. vector<ll> p1m, p2m;
  11. for (int i = 0; i < n; i++) {
  12. ll y;
  13. cin >> y;
  14. if (y > 0) {
  15. p1m.push_back(y);
  16. } else {
  17. p2m.push_back(abs(y));
  18. }
  19. }
  20.  
  21. vector<ll> p1f, p2f;
  22. for (int i = 0; i < n; i++) {
  23. ll y;
  24. cin >> y;
  25. if (y > 0) {
  26. p1f.push_back(y);
  27. } else {
  28. p2f.push_back(abs(y));
  29. }
  30. }
  31.  
  32. sort(p1m.begin(), p1m.end());
  33. sort(p2m.begin(), p2m.end());
  34. sort(p1f.begin(), p1f.end());
  35. sort(p2f.begin(), p2f.end());
  36.  
  37. int cnt = 0;
  38.  
  39. ll i = 0, j = 0;
  40. while (i < p1m.size() && j < p2f.size()) { // female wants small males
  41. if (p2f[j] > p1m[i]) {
  42. cnt++;
  43. i++;
  44. j++;
  45. } else {
  46. j++;
  47. }
  48. }
  49.  
  50. i = 0;
  51. j = 0;
  52. while (i < p2m.size() && j < p1f.size()) { // male wants small females
  53. if (p2m[i] > p1f[j]) {
  54. cnt++;
  55. i++;
  56. j++;
  57. } else {
  58. i++;
  59. }
  60. }
  61.  
  62. cout << "Valid Pairs: " << cnt;
  63. return 0;
  64. }
Success #stdin #stdout 0s 5320KB
stdin
7
-1900 -2000 -2500 1500 1600 2500 -2500 
1800 -1550 2200 -1550 2100 -2500 -1700 
stdout
Valid Pairs: 5