题单链接
#include
#include
#include
using namespace std;
int n,c,f[60][60][2],sum[60],inf=1e7;
struct lamp{
int p,c;
}s[60];
int main(){
cin>>n>>c;
for(int i=1;i<=n;i++){
cin>>s[i].p>>s[i].c;
sum[i]=sum[i-1]+s[i].c;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j][0]=f[i][j][1]=inf;
}
}
f[c][c][0]=f[c][c][1]=0;
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
f[i][j][0]=min(f[i+1][j][0]+(s[i+1].p-s[i].p)*(sum[n]-(sum[j]-sum[i])),
f[i+1][j][1]+(s[j].p-s[i].p)*(sum[n]-(sum[j]-sum[i])));
f[i][j][1]=min(f[i][j-1][0]+(s[j].p-s[i].p)*(sum[n]-(sum[j-1]-sum[i-1])),
f[i][j-1][1]+(s[j].p-s[j-1].p)*(sum[n]-(sum[j-1]-sum[i-1])));
}
}
cout<<min(f[1][n][0],f[1][n][1])<<endl;
}
#include
#include
using namespace std;
int n,h[1010],mod=19650827,f[1010][1010][2];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>h[i];
f[i][i][0]=1;
}
for(int len=1;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
if(h[i]<h[i+1]) f[i][j][0]+=f[i+1][j][0];
if(h[i]<h[j]) f[i][j][0]+=f[i+1][j][1];
if(h[j]>h[j-1]) f[i][j][1]+=f[i][j-1][1];
if(h[j]>h[i]) f[i][j][1]+=f[i][j-1][0];
f[i][j][0]%=mod;f[i][j][1]%=mod;
}
}
cout<<(f[1][n][0]+f[1][n][1])%mod<<endl;
}
#include
#include
using namespace std;
int n,a[210],sum[210],f[210][210],g[210][210],inf=1e9,maxans,minans=inf;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
a[n+i]=a[i];
}
for(int i=1;i<=2*n-1;i++){
sum[i]=sum[i-1]+a[i];
}
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=2*n-1;i++){
int j=i+len-1;
g[i][j]=inf;
for(int k=i;k<j;k++){
f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]+sum[j]-sum[i-1]);
g[i][j]=min(g[i][j],g[i][k]+g[k+1][j]+sum[j]-sum[i-1]);
}
}
}
for(int i=1;i<=n;i++){
maxans=max(maxans,f[i][i+n-1]);
minans=min(minans,g[i][i+n-1]);
}
cout<<minans<<endl<<maxans<<endl;
}
#include
#include
using namespace std;
int head[300],f[300][300];
int n,ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>head[i];
head[n+i]=head[i];
}
for(int len=1;len<=n;len++){
for(int i=1;i+len-1<=2*n-1;i++){
int j=i+len-1;
for(int k=i;k<j;k++){
f[i][j]=max(f[i][j],head[i]*head[k+1]*head[j+1]+f[i][k]+f[k+1][j]);
}
ans=max(ans,f[i][j]);
}
}
cout<<ans<<endl;
}
#include
#include
#include
using namespace std;
int n,m,a[90];
long long dp[90][90],sum,ans;
long long quick_pow(long long a,long long x){
long long ans=1;
while(x){
if(x&1) ans=ans*a;
a=a*a;
x>>=1;
}
return ans;
}
int main(){
cin>>n>>m;
while(n--){
memset(dp,0,sizeof dp);
ans=0;
for(int i=1;i<=m;i++) cin>>a[i];
for(int i=1;i<=m;i++){
for(int j=m;j>=i;j--){
long long x=quick_pow(2,m-(j-i)-1);
dp[i][j]=max(dp[i][j],max(dp[i-1][j]+x*a[i-1],dp[i][j+1]+x*a[j+1]));
}
}
for(int i=1;i<=m;i++){
ans=max(ans,dp[i][i]+quick_pow(2,m)*a[i]);
}
sum+=ans;
}
cout<<sum<<endl;
}
#include
#include
using namespace std;
int n,a[250],f[250][250],ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];f[i][i]=a[i];
}
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
for(int k=i;k<j;k++){
if(f[i][k]==f[k+1][j]){
f[i][j]=max(f[i][j],f[i][k]+1);
ans=max(ans,f[i][j]);
}
}
}
}
cout<<ans<<endl;
}
f [ i ] [ j ] f[i][j] f[i][j]表示区间 [ i , j ] [i,j] [i,j]的最少涂色次数
f [ i ] [ j ] = { 1 , i = = j m i n ( f [ i + 1 ] [ j ] , f [ i ] [ j − 1 ] , s [ i ] = = s [ j ] m i n ( f [ i ] [ k ] + f [ k + 1 ] [ j ] ) , k ∈ [ i , j − 1 ] , s [ i ] ≠ s [ j ] f[i][j]=\left\{ \begin{aligned} 1,i==j\\ min(f[i+1][j],f[i][j-1],s[i]==s[j] \\ min(f[i][k]+f[k+1][j]),k\in[i,j-1],s[i]\neq s[j] \end{aligned} \right. f[i][j]=⎩⎪⎨⎪⎧1,i==jmin(f[i+1][j],f[i][j−1],s[i]==s[j]min(f[i][k]+f[k+1][j]),k∈[i,j−1],s[i]=s[j]
#include
#include
#include
using namespace std;
string s;
int n,f[60][60],inf=1e9;
int main(){
cin>>s;n=s.length();s.insert(0," ");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j]=inf;
}
}
for(int i=1;i<=n;i++){
f[i][i]=1;
}
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
if(s[i]==s[j]) f[i][j]=min(f[i+1][j],f[i][j-1]);
else{
for(int k=i;k<j;k++){
f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
}
}
}
}
cout<<f[1][n]<<endl;
}
#include
#include
using namespace std;
int n,a[600],f[600][600],inf=2e9;
void init(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j]=inf;
}
}
for(int i=1;i<=n;i++){
f[i][i]=1;
}
for(int i=1;i<n;i++){
if(a[i]==a[i+1]) f[i][i+1]=1;
else f[i][i+1]=2;
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
init();
for(int len=3;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
if(a[i]==a[j]) f[i][j]=f[i+1][j-1];
for(int k=i;k<j;k++)
f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
}
}
cout<<f[1][n]<<endl;
}