Advertisement
i_love_rao_khushboo

2D Prefix Sum

Sep 19th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. /*PROBLEM: Sayan's friend has left a gamefield organized as an Nβˆ—N grid. Each square in the grid either has
  2. or does not have a gold coin. He has to divide the gamefield with his three other friends as
  3. follows:
  4. he will draw one horizontal line and one vertical line to divide the field into four rectangles. His friends will choose three of the four smaller fields and he gets the last one.
  5.  
  6. He wants to divide the field so that he gets the maximum number of goldcoins, assuming that his friends
  7. will pick the best three rectangles (i.e. π‘Ίπ’‚π’šπ’‚π’ π’˜π’Šπ’π’ π’‚π’π’˜π’‚π’šπ’” π’ˆπ’†π’• 𝒕𝒉𝒆 π’“π’†π’„π’•π’‚π’π’ˆπ’π’† π’‰π’‚π’—π’Šπ’π’ˆ π’Žπ’Šπ’π’Šπ’Žπ’–π’Ž #π’ˆπ’π’π’… π’„π’π’Šπ’π’”).
  8.  
  9. Input Format:
  10. The first line contains 2 integers N and M, the size of the gamefield and the number of gold coins in the
  11. field respectively.
  12.  
  13. The next M lines contain two integers, Xi and Yi, giving the coordinated of the i-th gold coin. It is guaranteed that all Xi and Yi are pairwise distinct.
  14.  
  15. Constraints:
  16. 1 ≀ N ≀ 1000
  17. 0 ≀ M ≀ NΒ²
  18. 1 ≀ Xα΅’,Yα΅’ ≀ N
  19.  
  20. Output Format:
  21. Output one integer, the maximum number of gold coins Sayan can get.
  22.  
  23. Sample Input:
  24. 6 13
  25. 1 2
  26. 1 3
  27. 2 1
  28. 2 4
  29. 2 5
  30. 3 2
  31. 4 2
  32. 4 3
  33. 4 6
  34. 5 1
  35. 5 4
  36. 5 5
  37. 6 2
  38.  
  39. Sample Output: 3
  40. */
  41.  
  42. /*UNDERLYING CONCEPT ----->
  43. # This problem is based on the concept of 2 - D Prefix Sum.
  44. # ∴ a dp[][] matrix will be formed where dp[i][j] stores the total number of gold coins present in the
  45. submatrix whose left upper cell is dp[0][0] and lower right cell is dp[i][j].
  46. The above precomputation is done in O(n * m) time, where n and m are the #rows and #columns in the i/p
  47. matrix.
  48. # After the above precomputation just traverse the dp[][] matrix by considering each case when Sayan divide
  49. the matrix at the current dp[i][j] cell. The minimum of the 4 parts is taken as π‘Ίπ’‚π’šπ’‚π’ π’˜π’Šπ’π’ π’‚π’π’˜π’‚π’šπ’” π’ˆπ’†π’•
  50. 𝒕𝒉𝒆 π’“π’†π’„π’•π’‚π’π’ˆπ’π’† π’‰π’‚π’—π’Šπ’π’ˆ π’Žπ’Šπ’π’Šπ’Žπ’–π’Ž #π’ˆπ’π’π’… π’„π’π’Šπ’π’”, and then this minimum value is maximized.
  51. */
  52.  
  53. #include<bits/stdc++.h>
  54. using namespace std;
  55.  
  56. vector<vector<int>> dp(1001, vector<int>(1001, 0));
  57.  
  58. void twoDPrefixSum(int n, int m)
  59. {
  60. for(int i=1; i<n; i++)
  61. dp[i][0]+=dp[i-1][0];
  62.  
  63. for(int j=1; j<m; j++)
  64. dp[0][j]+=dp[0][j-1];
  65.  
  66. for(int i=1; i<n; i++)
  67. {
  68. for(int j=1; j<m; j++)
  69. dp[i][j] += dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
  70. }
  71. }
  72.  
  73. bool comp(int a, int b)
  74. {
  75. return (a < b);
  76. }
  77.  
  78. void solve()
  79. {
  80. int n, m; cin>>n>>m;
  81. for(int i=0; i<m; i++)
  82. {
  83. int x, y; cin>>x>>y;
  84. dp[x-1][y-1]=1;
  85. }
  86.  
  87. twoDPrefixSum(n, n);
  88.  
  89. int res=INT_MIN;
  90.  
  91. for(int i=0; i<n; i++)
  92. {
  93. for(int j=0; j<n; j++)
  94. {
  95. int part1=dp[i][j];
  96. int part2=dp[i][n-1] - dp[i][j];
  97. int part3=dp[n-1][j] - dp[i][j];
  98. int part4=dp[n-1][n-1] - (part1 + part2 + part3);
  99.  
  100. res=max(res, min({part1, part2, part3, part4}, comp));
  101. }
  102. }
  103.  
  104. cout<<res;
  105. }
  106.  
  107. int main() {
  108.  
  109. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  110. srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  111.  
  112. int t = 1;
  113. //cin >> t;
  114. while(t--) {
  115. solve();
  116. }
  117.  
  118. return 0;
  119. }
  120.  
  121. /*# Time complexity: O(r*c), where r and c are the #rows and #columns in the given i/p 2 D Matrix.
  122. # Auxiliary Space: O(r*c), where r and c are the #rows and #columns in the given 2 D Matrix.
  123. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement