Guest User

Untitled

a guest
May 20th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int search(vector<vector<int>> mat, int x) {
  6. int n = mat.size(), m = mat[0].size();
  7. int i = 0, j = m - 1;
  8. while (i < n && j >= 0)
  9. {
  10. if (mat[i][j] == x) return 1;
  11.  
  12. if (mat[i][j] > x) j--;
  13. else i++;
  14. }
  15. return 0;
  16. }
  17.  
  18. int main()
  19. {
  20. int t, n, m, k;
  21. vector<int> temp;
  22. vector<vector<int>> matrix;
  23. cin >> t;
  24. while (t > 0)
  25. {
  26. t--;
  27. cin >> n >> m;
  28. for (int i = 0; i < n; i++)
  29. {
  30. temp.clear();
  31. for (int j = 0; j < m; j++)
  32. {
  33. cin >> k;
  34. temp.push_back(k);
  35. }
  36. matrix.push_back(temp);
  37. }
  38. cin >> k;
  39. cout << search(matrix, k) << endl;
  40. matrix.clear();
  41. }
  42. return 0;
  43. }
Add Comment
Please, Sign In to add comment