Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int search(vector<vector<int>> mat, int x) {
- int n = mat.size(), m = mat[0].size();
- int i = 0, j = m - 1;
- while (i < n && j >= 0)
- {
- if (mat[i][j] == x) return 1;
- if (mat[i][j] > x) j--;
- else i++;
- }
- return 0;
- }
- int main()
- {
- int t, n, m, k;
- vector<int> temp;
- vector<vector<int>> matrix;
- cin >> t;
- while (t > 0)
- {
- t--;
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- {
- temp.clear();
- for (int j = 0; j < m; j++)
- {
- cin >> k;
- temp.push_back(k);
- }
- matrix.push_back(temp);
- }
- cin >> k;
- cout << search(matrix, k) << endl;
- matrix.clear();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment