Advertisement
a53

matricea

a53
May 30th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <fstream>
  2. #define MAX_NM 30000
  3. using namespace std;
  4. ifstream fin("matrice.in");
  5. ofstream fout("matrice.out");
  6. int n, m, a, L[MAX_NM + 1], C[MAX_NM + 1];
  7. int zmax = 0, nsol = 0;
  8.  
  9. void Solve(int, int);
  10. void Update(int, int);
  11.  
  12. int main()
  13. {
  14. fin >> n >> m >> a;
  15.  
  16. // L si C reprezinta sumele partiale pentru linia de sus si coloana din stanga.
  17. for (int i = 1; i <= n; ++i)
  18. {
  19. fin >> C[i];
  20. C[i] += C[i - 1];
  21. }
  22.  
  23. for (int j = 1; j <= m; ++j)
  24. {
  25. fin >> L[j];
  26. L[j] += L[j - 1];
  27. }
  28.  
  29. for (int d = 1; d * d <= a; ++d)
  30. {
  31. if (a % d == 0)
  32. {
  33. Solve(d, a / d);
  34.  
  35. if (d * d != a)
  36. Solve(a / d, d);
  37. }
  38. }
  39.  
  40. fout << zmax << ' ' << nsol;
  41.  
  42. return 0;
  43. }
  44.  
  45. void Solve(int w, int h) // Rezolva dreptunghiurile de latime w si inaltime h.
  46. {
  47. if (w > m || h > n)
  48. return;
  49.  
  50. int ma1 = 0, cnt_ma1 = 0, ma0 = 0, cnt_ma0 = 0;
  51.  
  52. for (int st = 1, dr = w; dr <= m; ++st, ++dr)
  53. {
  54. int cnt1 = L[dr] - L[st - 1], cnt0 = w - cnt1;
  55.  
  56. if (cnt1 > ma1)
  57. {
  58. ma1 = cnt1;
  59. cnt_ma1 = 1;
  60. }
  61. else if (cnt1 == ma1)
  62. ++cnt_ma1;
  63.  
  64. if (cnt0 > ma0)
  65. {
  66. ma0 = cnt0;
  67. cnt_ma0 = 1;
  68. }
  69. else if (cnt0 == ma0)
  70. ++cnt_ma0;
  71. }
  72.  
  73. for (int sus = 1, jos = h; jos <= n; ++sus, ++jos)
  74. {
  75. int cnt1 = C[jos] - C[sus - 1], cnt0 = h - cnt1;
  76.  
  77. if (cnt1 > cnt0)
  78. {
  79. int z = cnt1 * ma1 + cnt0 * (w - ma1);
  80. Update(z, cnt_ma1);
  81. }
  82. else if (cnt0 > cnt1)
  83. {
  84. int z = cnt0 * ma0 + cnt1 * (w - ma0);
  85. Update(z, cnt_ma0);
  86. }
  87. else // cnt1 == cnt0
  88. {
  89. int z = cnt1 * w;
  90. Update(z, m - w + 1);
  91. }
  92. }
  93. }
  94.  
  95. void Update(int z, int cnt) // Am gasit cnt dreptunghiuri noi cu z 0-uri. Updatez raspunsul.
  96. {
  97. if (z > zmax)
  98. {
  99. zmax = z;
  100. nsol = cnt;
  101. }
  102. else if (z == zmax)
  103. nsol += cnt;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement