RT, dp boom 0 了,蒟蒻求助(我样例都过不了)
#include <bits/stdc++.h>
using namespace std;
#define N 100010
inline int read(){
int x = 0, s = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-')s = -1;
c = getchar();
}
while(isdigit(c)){
x = x * 10 + (c ^ '0');
c = getchar();
}
return x * s;
}
int f[N], deth[N];
int main(){
int n = read();
memset(deth, 32, sizeof(deth));
memset(f, 30, sizeof(f));
f[1] = 0, f[2] = 0;
deth[1] = 1, deth[2] = 2;
for(int i = 1;i <= n; i++)
for(int j = 0;j <= i; j++)
deth[i] = min(deth[i], max(deth[j], (i - j - 1 >= 0 ? deth[i - j - 1] : 0)) + 1);
for(int i = 3;i <= n; i++){
for(int j = 0;j <= i; j++){
if(abs(deth[j] - (i - j - 1 >= 0 ? deth[i - j - 1] : 0)) <= 1){
f[i] = min(f[i], j + f[j] + (i - j - 1 >= 0 ? f[i - 1 - j] :0));
}
}
}
printf("%d\n", f[n]);
return 0;
}