Advertisement
a53

deminare

a53
Mar 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. // prof. Maria Pandele - 100 puncte
  2.  
  3. #include <cmath>
  4. #include <fstream>
  5. #include <vector>
  6.  
  7. int solve1(std::vector<std::vector<int>>& a, std::ofstream& cout) {
  8. int maxim = 0;
  9. std::vector<int> cnt(a.size());
  10. for (int i = 0; i < a.size(); ++i) {
  11. cnt[i] = 0;
  12. for (int j = 0; j < a[i].size(); ++j) {
  13. cnt[i] += a[i][j];
  14. }
  15. maxim = std::max(maxim, cnt[i]);
  16. }
  17.  
  18. for (int i = 0; i < cnt.size(); ++i) {
  19. if (cnt[i] == maxim) {
  20. cout << i + 1 << " ";
  21. }
  22. }
  23. cout << "\n";
  24. }
  25.  
  26. int verif(std::vector<std::vector<int>>& sp, int& l, int &c, int&h , int& w) {
  27. int ans = -1;
  28.  
  29. if (h <= l && w <= c) {
  30. for (int i = 1; i <= l - h + 1; ++i) {
  31. for (int j = 1; j <= c - w + 1; ++j) {
  32. int x = i + h - 1;
  33. int y = j + w - 1;
  34. ans = std::max(ans, sp[x][y] - sp[i - 1][y] - sp[x][j - 1] + sp[i - 1][j - 1]);
  35. }
  36. }
  37. }
  38. return ans;
  39. }
  40.  
  41. int solve2(std::vector<std::vector<int>>& a, int& l, int& c, int& m) {
  42. std::vector<std::vector<int>> sp(l + 1, std::vector<int>(c + 1, 0));
  43. for (int i = 0; i < l; ++i) {
  44. sp[i][0] = a[i][0];
  45. }
  46.  
  47. for (int j = 0; j < c; ++j) {
  48. sp[0][j] = a[0][j];
  49. }
  50.  
  51. for (int i = 1; i <= l; ++i) {
  52. for (int j = 1; j <= c; ++j) {
  53. sp[i][j] = sp[i - 1][j] + sp[i][j - 1] - sp[i - 1][j - 1] + a[i - 1][j - 1];
  54. }
  55. }
  56.  
  57. int lim = sqrt(m);
  58. int ans = -1;
  59. for (int d = 1; d <= lim; ++d) {
  60. if (m % d == 0) {
  61. int h = d;
  62. int w = m / d;
  63.  
  64. ans = std::max(ans, verif(sp, l, c, h, w));
  65. ans = std::max(ans, verif(sp, l, c, w, h));
  66. }
  67. }
  68. if (ans == -1) {
  69. return -1;
  70. }
  71. return m - ans;
  72. }
  73.  
  74. int main() {
  75. std::ifstream cin("deminare.in");
  76. std::ofstream cout("deminare.out");
  77.  
  78. int v, l, c, m;
  79. cin >> v >> l >> c >> m;
  80. std::vector<std::vector<int>> a(l, std::vector<int>(c, 0));
  81. int x, y;
  82. for (int i = 0; i < m; ++i) {
  83. cin >> x >> y;
  84. --x; --y;
  85. a[x][y] = 1;
  86. }
  87.  
  88. if (v == 1) {
  89. solve1(a, cout);
  90. }
  91. else {
  92. cout << solve2(a, l, c, m) << "\n";
  93. }
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement