diff --git a/Problems/CSES/cses_rectanglecutting.cpp b/Problems/CSES/cses_rectanglecutting.cpp new file mode 100644 index 0000000..556a629 --- /dev/null +++ b/Problems/CSES/cses_rectanglecutting.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; +vector> dp; // i = altura, j = largura + +// ex 1x3 == (1+ 1x2 + 1x1) +int cortedp(int i, int j){ + if(dp[i][j] != -1) return dp[i][j]; + if(i == j) return 0; + + int answ = __INT32_MAX__; + for(int l = i-1; l > 0; l--){ + answ = min(answ, 1+cortedp(i-l, j)+cortedp(l,j)); + } + for(int r = j-1; r > 0; r--){ + answ = min(answ, 1+cortedp(i, j-r)+cortedp(i,r)); + } + + return dp[i][j] = answ; +} + +int main(){ + ios::sync_with_stdio(false); + + int a,b; + cin >> a >> b; + dp.assign(a+1, vector(b+1, -1)); + + //cout << cortedp(a,b) << '\n'; Recursiva (AC) + + for(int i = 1; i <= a; i++){ + for(int j = 1; j <= b; j++){ // Calculating rectangle ixj + if(i == j){ // Base case, no actions + dp[i][j] = 0; + }else{ + dp[i][j] = __INT32_MAX__; + // Vertical cuts + for(int k = i-1; k > 0; k--){ + dp[i][j] = min(dp[i][j], 1+ dp[i-k][j]+dp[k][j]); + } + // Horizontal cuts + for(int k = j-1; k > 0; k--){ + dp[i][j] = min(dp[i][j], 1+ dp[i][j-k]+dp[i][k]); + } + } + } + } + + cout << dp[a][b] << '\n'; + + return 0; +} \ No newline at end of file diff --git a/Problems/USACO/usaco2016_3.cpp b/Problems/USACO/usaco2016_3.cpp new file mode 100644 index 0000000..2743818 --- /dev/null +++ b/Problems/USACO/usaco2016_3.cpp @@ -0,0 +1,37 @@ +#include +#define USACOIN ifstream cin("248.in"); ofstream cout("248.out"); + +using namespace std; +vector> dp; +vectorv; + +int main(){ + USACOIN + ios::sync_with_stdio(false); + + int n, answ = 0; + cin >> n; + + v.resize(n); + dp.assign(n, vector(n, 0)); + for(int i = 0; i < n; i++){ + cin >> v[i]; + dp[i][i] = v[i]; + answ = max(answ, v[i]); + } + + for(int j = 1; j < n; j++){ + for(int i = j-1; i >= 0; i--){ // Creating answer for interval i-j + for(int k = i; k < j; k++){ // Checking if best answer is merging i-k with k+1-j + if(dp[i][k] != 0 && dp[k+1][j] != 0 && dp[i][k] == dp[k+1][j]){ + dp[i][j] = dp[i][k]+1; + answ = max(answ, dp[i][j]); + } + } + } + } + + cout << answ << '\n'; + + return 0; +}