fork(1) download
  1. //SOLUTIE 2_0_3_3 OPTIMIZARE DUPA SFATURI MARIAN CALL 26-11-2020
  2. //2021-11-04
  3.  
  4.  
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11. int n, m, mt[501][501], k;
  12.  
  13. cin >> n >> m;
  14. for(int i = 1; i <= n; i++){
  15. for(int j = 1; j <= m; j++){
  16. cin >> mt[i][j];
  17. }
  18. }
  19. cin >> k;
  20. int x, i;
  21. for(int p = 1; p <= k; p++){
  22. cin >> x;
  23.  
  24. if (x < mt[1][1] || x > mt[n][m]) {
  25. cout << "0NU\n";
  26. //break;
  27. } else { // mt[1][1] <= x && x <= mt[n][m] ...val x se incadreaza in interv de valori ale elem matricei
  28. //int i, j;
  29. if (x - mt[1][1] <= mt[n][m] - x) { // x e mai aproape de coltul din stg sus
  30. i = 1;
  31. //int left = 1, right = m;
  32. while (mt[i][1] <= x && x <= mt[i][m]) {
  33. int left = 1, right = m;
  34. while (left < right) {
  35. int mid = (left + right) / 2;
  36. if (mt[i][mid] < x) {
  37. left = mid + 1;
  38. } else { // x <= mt[i][mid]
  39. right = mid;
  40. }
  41. }
  42. cout << "x= " << x << " ";
  43. if (mt[i][left] == x) {
  44. cout << "1DA\n";
  45. //break;
  46. } else {
  47. cout << "1NU\n";
  48. //break;
  49. }
  50. if (i < n) {
  51. i++;
  52. }
  53. }
  54. } else { // x - mt[1][1] > mt[n][m] - x ...adica x e mai aproape de coltul din dr jos
  55. i = n;
  56. while (mt[i][1] <= x && x <= mt[i][m]) {
  57. int left = 1, right = m;
  58. while (left < right) {
  59. int mid = (left + right) / 2;
  60. if (mt[i][mid] < x) {
  61. left = mid + 1;
  62. } else { // x <= mt[i][mid]
  63. right = mid;
  64. }
  65. }
  66. cout << "x= " << x << " ";
  67. if (mt[i][left] == x) {
  68. cout << "2DA\n";
  69. //break;
  70. } else {
  71. cout << "2NU\n";
  72. //break;
  73. }
  74. if (i > 1) {
  75. i--;
  76. }
  77. }
  78. }
  79. }
  80.  
  81. }
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5348KB
stdin
3   3
1 	2 	3
2 	5 	6
5 	6 	7
5
2   4   10   7   5 
stdout
x= 2 1DA
x= 2 1DA
0NU
x= 7 2DA
x= 5 2DA
x= 5 2DA