- 我们开始分析
- 假设1-10 我们取3 和 5和 7
- 1 - 10
- 1-4 5 6-10 hanshu(1,10)=hanshu(1,4)+hanshu(6,10)+ (a[10]-a[1]-2)这部分是取5所能获得的
- 基本dp我们就找到了。通过记忆化dp来做
-
-
-
-
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- using namespace std;
- const int maxn=110;
- const int inf=1e9;
- int n,m,a[maxn],d[maxn][maxn];
- void initial(){
- for(int i=0;i<=m+1;i++)
- for(int j=0;j<=m+1;j++)
- d[i][j]=inf;
- }
- void input(){
- a[0]=0;a[m+1]=n+1;
- for(int i=1;i<=m;i++) scanf("%d",&a[i]);
- }
- int dp(int i,int j){
- if(d[i][j]!=inf) return d[i][j];
- if(i+1>=j) return 0;
- for(int k=i+1;k<=j-1;k++){
- d[i][j]=min(dp(i,k)+dp(k,j)+(a[j]-a[i]-2),d[i][j]);
- }
- return d[i][j];
- }
- void computing(){
- cout<<dp(0,m+1)<<endl;
- }
- int main(){
- freopen("C-large-practice.in","r",stdin);
- freopen("C-large-practice.out","w",stdout);
- int t;
- cin>>t;
- for(int i=1;i<=t;i++){
- scanf("%d%d",&n,&m);
- initial();
- input();
- printf("Case #%d: ",i);
- computing();
- }
- return 0;
- }