fork(1) download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int bs(int i , vector<vector<int>>&mat,int x){
  6. int n = mat[i].size();
  7. int low = 0, high = n-1;
  8. int ans = INT_MIN;
  9. while(low<=high){
  10. //try to find the last element in the sorted row whose value is less
  11. // than equal to x
  12. int mid = (low+high)/2;
  13. if(mat[i][mid]<=x){
  14. low = mid+1;
  15. ans = max(ans,mid);
  16. }
  17. else{
  18. high = mid-1;
  19. }
  20. }
  21. return ans+1 ;
  22. }
  23.  
  24. int main() {
  25. // your code goes here
  26. int n ;
  27. cin>>n;
  28. int x ; cin>>x;
  29. vector<vector<int>>mat(n,vector<int>(n,0));
  30. for(int i = 0 ; i<n;i++){
  31. for(int j = 0; j<n;j++){
  32. cin>>mat[i][j];
  33. }
  34. }
  35.  
  36. int count = 0 ;
  37. for(int i = 0 ; i<n;i++){
  38. count = count + bs(i,mat,x);
  39. }
  40. cout<<count;
  41. return 0;
  42. }
Success #stdin #stdout 0s 5320KB
stdin
3
10
1 2 11
5 7 12
10 14 15
stdout
5