sgu 183

来源:互联网 发布:linux文件系统损坏 编辑:程序博客网 时间:2024/05/19 16:48

动态规划

开始不会做,
看了Owaski的题解才会的


#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#define INF (1<<30)-1const int MAXN = 10005, MAXM = 105;int c[MAXN] = {0} , n , m;//f(i,j) = min{f(k,i)} + c(j)  //j-i < m  i-k < mint f[MAXM][MAXM] = {0};#define mod(a) (a%MAXM)int main(){    int ans = INF;#ifndef ONLINE_JUDGE    freopen("sgu183.in","r",stdin);    freopen("sgu183.out","w",stdout);#endif       scanf("%d%d",&n,&m);     for(int i = 1; i <= n; i++)     {       scanf("%d",&c[i]);       f[0][i] = c[i];     }     for(int i = 1; i < n; i ++)       for(int j = std::min(i + m -1, n); j > i; j --)       {         f[mod(i)][mod(j)] = INF;         for(int k = std::max(j - m,0); k < i ; k ++)         {            f[mod(i)][mod(j)] = std::min(f[mod(i)][mod(j)],f[mod(k)][mod(i)] + c[j]);         }       }      for(int i = std::max(0 ,n - m + 1); i < n; i ++)        for(int j = i + 1 ; j <= n ; j++)          {            ans = std::min(ans ,f[mod(i)][mod(j)]);         }     printf("%d",ans);    #ifndef ONLINE_JUDGE        fclose(stdin);    fclose(stdout);#endif  }
0 0