Advertisement
a53

matrice9

a53
Mar 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. #define N_MAX 1024
  4.  
  5. #define SEQ_UNKNOWN 0
  6. #define SEQ_INCREASING 1
  7. #define SEQ_DECREASING 2
  8.  
  9. int N, M;
  10. short A[N_MAX][N_MAX];
  11.  
  12. int Height[N_MAX], Type[N_MAX], Start[N_MAX];
  13. int Count, Stack[N_MAX], Top[N_MAX];
  14.  
  15. int Best, Best1l, Best1c, Best2l, Best2c;
  16.  
  17. void solve ()
  18. {
  19. int i, j, top;
  20.  
  21. for (j = 0; j < M; j++)
  22. {
  23. for (i = 0; i <= N; i++)
  24. {
  25. // Get the height of the column
  26. if (i == N)
  27. Height[i] = 0;
  28. else if (j == 0)
  29. Height[i] = 1;
  30. else if (Type[i] == SEQ_UNKNOWN)
  31. {
  32. Type[i] = (A[i][j] > A[i][j - 1]) ? SEQ_INCREASING : SEQ_DECREASING;
  33. Height[i]++;
  34. }
  35. else if (Type[i] == SEQ_INCREASING)
  36. {
  37. if (A[i][j] < A[i][j - 1])
  38. {
  39. Height[i] = j - Start[i] + 1;
  40. Start[i] = j - 1;
  41. Type[i] = SEQ_DECREASING;
  42. }
  43. else
  44. Height[i]++;
  45. }
  46. else if (Type[i] == SEQ_DECREASING)
  47. {
  48. if (A[i][j] > A[i][j - 1])
  49. {
  50. Height[i] = j - Start[i] + 1;
  51. Start[i] = j - 1;
  52. Type[i] = SEQ_INCREASING;
  53. }
  54. else
  55. Height[i]++;
  56. }
  57.  
  58. // Do the stack thing
  59. top = i;
  60. while (Count > 0 && Height[i] <= Stack[Count - 1])
  61. {
  62. top = Top[Count - 1];
  63. if (Stack[Count - 1] * (i - top) > Best)
  64. {
  65. Best = Stack[Count - 1] * (i - top);
  66. Best1l = top;
  67. Best2l = i - 1;
  68. Best1c = j - Stack[Count - 1] + 1;
  69. Best2c = j;
  70. }
  71. Count--;
  72. }
  73. if (Height[i] > 0)
  74. {
  75. Stack[Count] = Height[i];
  76. Top[Count++] = top;
  77. }
  78. }
  79. }
  80.  
  81. printf("%d %d %d %d\n", Best1l + 1, Best1c + 1, Best2l + 1, Best2c + 1);
  82. }
  83.  
  84. void read_solve ()
  85. {
  86. int i, j;
  87.  
  88. freopen("matrice9.in", "r", stdin);
  89. freopen("matrice9.out", "w", stdout);
  90. scanf("%d%d", &N, &M);
  91. for (i = 0; i < N; i++)
  92. for (j = 0; j < M; j++)
  93. scanf("%d", &A[i][j]);
  94.  
  95. solve();
  96. }
  97.  
  98. int main ()
  99. {
  100. read_solve();
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement