Advertisement
Fastrail08

Arrange Buildings

Jun 4th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int arrangeBuildings(int plot, int lastPlotUsedFor, int &n, vector<vector<int> > &memo){
  5.     if(plot >= n){
  6.         return 1;
  7.     }
  8.    
  9.     if(lastPlotUsedFor != -1 && memo[plot][lastPlotUsedFor] != 0){
  10.         return memo[plot][lastPlotUsedFor];
  11.     }
  12.    
  13.     //levels - plot
  14.     //options - either make a building in the plot or not(leave as is)
  15.     /*
  16.     lastPlotUsedFor = -1 [we are exploring options for 0th plot]
  17.                     = 0  [last plot was left as is]
  18.                     = 1  [last plot was used for a building]
  19.     */
  20.    
  21.     // At each level, explore all the valid options
  22.    
  23.     //make building
  24.     //This call can only be made if we are at the first plot (which have no previous), or the previous plot had a space
  25.     // We do this as if the previous plot/level was used for a building, making a building at the current level would make buildings consecutive, breaking the constraint.
  26.     int built = 0, notBuilt = 0;
  27.     if(lastPlotUsedFor == -1 || lastPlotUsedFor == 0){
  28.         built = arrangeBuildings(plot + 1, 1, n, memo);
  29.     }
  30.    
  31.     //not make building
  32.     notBuilt = arrangeBuildings(plot + 1, 0, n, memo);
  33.     if(lastPlotUsedFor != -1){
  34.         memo[plot][lastPlotUsedFor] = built + notBuilt;
  35.     }
  36.     return built + notBuilt;
  37. }
  38.  
  39.  
  40. long long arrangeBuildingsDP(int &n){
  41.     /*
  42.     Storage & Meaning - dp[i][0] = ways to arrange buildings in i plots where last plot is to be left (should be empty)
  43.                         dp[i][1] = ways to arrange buildings in i plots where last plot is used to make buildings
  44.     */
  45.     vector<vector<int> > dp(n + 1, vector<int>(2, 0));
  46.    
  47.     /*
  48.     Direction - Smallest Problem at:
  49.     dp[1][0] = ways to arrange buildings if only 1 plot is there and last plot should be empty = 1 way (leave it empty)
  50.     dp[1][1] = ways to arrange buildings if only 1 plot is there and last plot should have a building = 1 way (make one)
  51.    
  52.     Largest problem at dp[n][0] & dp[n][1]
  53.     As our problem asks how many ways are there to arrange buildings if there were n plots
  54.     dp[n][0] = ways to arrange if n plots available, last plot should be empty
  55.     dp[n][1] = ways to arrange if n plots available, last plot should have a building
  56.     Either of them have n plots, totalling/summation them would give the answer to the question
  57.    
  58.     */
  59.     dp[1][0] = dp[1][1] = 1;
  60.    
  61.     /*
  62.     Travel & Solve - Travel in the direction from small problem to large problem with transitions as captured in tree/memo/recursive code
  63.     */
  64.     for(int i = 2; i <= n; i++){
  65.         dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
  66.         dp[i][1] = dp[i - 1][0];
  67.     }
  68.    
  69.     // As there are 2 rows of plot (each would return dp[n] ways)
  70.     // So the total ways would be dp[n] * dp[n];
  71.     return pow((long long) (dp[n][0] + dp[n][1]), 2);
  72. }
  73.  
  74.  
  75. int main() {
  76.     // your code goes here
  77.     int n;
  78.     cin >> n;
  79.     vector<vector<int> > memo(n + 1, vector<int>(2, 0));
  80.     cout << pow((long long) arrangeBuildings(0, -1, n, memo), 2) << '\n';
  81.     cout << arrangeBuildingsDP(n) << '\n';
  82.    
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement