Guest User

number_of_rects.cpp

a guest
May 18th, 2018
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. // Reading input array:
  4. std::vector<std::string> input() {
  5.     int nRows, nCols;
  6.     std::cin >> nRows >> nCols;
  7.     std::vector<std::string> arr(nRows);
  8.     for (int row = 0; row < nRows; ++row) {
  9.         std::cin >> arr[row];
  10.     }
  11.     return arr;
  12. }
  13.  
  14. // Solve for arr:
  15. int solve(const std::vector<std::string>& arr) {
  16.     const int nRows = (int)arr.size();
  17.     const int nCols = (int)arr[0].size();
  18.     // Calculate max length of '#' from pos (row, col) in side of increasing col:
  19.     std::vector<std::vector<int>> maxLenRight(nRows, std::vector<int>(nCols+1, 0));
  20.     for (int row = 0; row < nRows; ++row) {
  21.         for (int col = nCols-1; col >= 0; --col) {
  22.             if (arr[row][col] == '.') {
  23.                 maxLenRight[row][col] = 0;
  24.             } else {
  25.                 maxLenRight[row][col] = maxLenRight[row][col+1] + 1;
  26.             }
  27.         }
  28.     }
  29.     // Calculate answer (with rectangles, which have height = 2):
  30.     int answer = 0;
  31.     for (int left = 0; left < nCols - 2; ++left) {
  32.         // Group all points with col = left by maxLenRight and calc total count of then:
  33.         int count = 0;
  34.         std::vector<int> countWithLen(nCols, 0);
  35.         for (int row = 0; row < nRows; ++row) {
  36.             int len = maxLenRight[row][left];
  37.             if (len >= 3) {
  38.                 countWithLen[len]++;
  39.                 count++;
  40.             }
  41.         }
  42.         // Moving right border, increase answer, remove not interested lengths:
  43.         for (int right = left + 2; right < nCols; ++right) {  
  44.             answer += count * (count-1) / 2;
  45.             count -= countWithLen[right-left+1];
  46.             countWithLen[right-left+1] = 0;
  47.         }
  48.     }
  49.     // Decreasing number of rectangles with height = 2:
  50.     for (int row1 = 0; row1 < nRows-1; ++row1) {
  51.         const int row2 = row1+1;
  52.         int same_len = 0;
  53.         for (int col = 0; col < nCols; ++col) {
  54.             if (arr[row1][col] == '#' && arr[row2][col] == '#') {
  55.                 same_len++;
  56.             } else {
  57.                 if (same_len >= 3) {
  58.                     answer -= (same_len-1) * (same_len-2) / 2;
  59.                 }
  60.                 same_len = 0;
  61.             }
  62.         }
  63.         if (same_len >= 3) {
  64.             answer -= (same_len-1) * (same_len-2) / 2;
  65.         }
  66.     }
  67.     return answer;
  68. }
  69.  
  70. int main() {
  71.     std::ios_base::sync_with_stdio(false);
  72.     std::cin.tie(0); std::cout.tie(0);
  73.    
  74.     int nQueries; std::cin >> nQueries;
  75.     for (int q = 0; q < nQueries; ++q) {
  76.         std::cout << solve(input()) << std::endl;
  77.     }
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment