fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7.  
  8. int n;
  9. cin>> n;
  10.  
  11. vector<int> ml(n,0), fl(n,0);
  12.  
  13. for(int i=0; i<n; i++){
  14. cin>>ml[i];
  15. }
  16. for(int i=0; i<n; i++){
  17. cin>>fl[i];
  18. }
  19.  
  20. sort(ml.begin(), ml.end(), greater<int>());
  21. sort(fl.begin(), fl.end());
  22.  
  23. int pm=0,pf=0;
  24.  
  25. // for(int i=0; i<n; i++){
  26. // cout<<ml[i]<<" "<<fl[i]<<endl;
  27. // }
  28.  
  29. int count = 0;
  30.  
  31. while(pm<n && pf<n){
  32. int m = ml[pm];
  33. int f = fl[pf];
  34.  
  35. // can be pairs??
  36. if(m*f<0){
  37. // they meet their choice?
  38. if(m<0){
  39. if(-m>f){
  40. count++;
  41. cout<<pm<<" "<<pf<<endl;
  42. pm++;
  43. pf++;
  44.  
  45. }
  46. // m is -ve, f is +ve but |m| <= |f|
  47. // ab right me hi ja skte h.
  48. // right me |m| will increase.
  49. // right me |f| will increase.
  50. // we want larger m and smaller f
  51. else{
  52. pm++;
  53. }
  54. }
  55. else{
  56. if(m<-f){
  57. count++;
  58. cout<<pm<<" "<<pf<<endl;
  59. pm++;
  60. pf++;
  61. }
  62. // m is +ve, f is -ve but |m| >= |f|
  63. // ab right me hi ja skte h.
  64. // right me |m| will decrease.
  65. // right me |f| will decrease.
  66. // we want smaller m and larger f
  67. else{
  68. pm++;
  69. }
  70.  
  71. }
  72. }
  73. else{
  74. // both +ve
  75. if(m>0){
  76. while(ml[pm]>0){
  77. pm++;
  78. }
  79. }
  80. // both -ve
  81. else if(f<0){
  82. while(fl[pf]<0){
  83. pf++;
  84. }
  85. }
  86. }
  87. }
  88.  
  89. cout << count;
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0s 5316KB
stdin
2
-1800 -2200
1900 1800
stdout
1 0
1