Alguns problemas de PD em range um dia antes da OBI
This commit is contained in:
parent
601881b5e6
commit
571072b7dc
52
Problems/CSES/cses_rectanglecutting.cpp
Normal file
52
Problems/CSES/cses_rectanglecutting.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
vector<vector<int>> 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<int>(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;
|
||||
}
|
||||
37
Problems/USACO/usaco2016_3.cpp
Normal file
37
Problems/USACO/usaco2016_3.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define USACOIN ifstream cin("248.in"); ofstream cout("248.out");
|
||||
|
||||
using namespace std;
|
||||
vector<vector<int>> dp;
|
||||
vector<int>v;
|
||||
|
||||
int main(){
|
||||
USACOIN
|
||||
ios::sync_with_stdio(false);
|
||||
|
||||
int n, answ = 0;
|
||||
cin >> n;
|
||||
|
||||
v.resize(n);
|
||||
dp.assign(n, vector<int>(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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user