fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int binary(vector<int>&a,int key) //By chandan date 03-04-2015
  4. {
  5. int l=0,h=a.size(),cnt=0;
  6. while(l<=h)
  7. {
  8. int mid=l+(h-l)/2;
  9. if(a[mid]==key)
  10. cnt++;
  11. else if(a[mid]>h)
  12. l=mid+1;
  13. else if(a[mid]<h)
  14. h=mid-1;
  15. }
  16. return cnt;
  17. }
  18. int main() {
  19. // your code goes here
  20. int n,cnt=0;
  21. cin>>n;
  22. vector<int>a(n);
  23. vector<int>h(n);
  24. for(int i=0;i<n;i++)
  25. cin>>a[i],cin>>h[i];
  26. sort(a.begin(),a.end());
  27. for(int i=0;i<h.size();i++)
  28. cnt+=binary(a,h[i]);
  29. cout<<cnt;
  30. return 0;
  31. }
Time limit exceeded #stdin #stdout 5s 3276KB
stdin
4
100 42
42 100
5 42
100 5
stdout
Standard output is empty