Dpの間隔と最適化

感じ間隔DPの下で見て、ボード上の直接のコードだけで罰金ルーチンの一種です。

基本的な質問のACコード:石のマージ

#include <ビット/ STDC ++ H>
 使用して 名前空間STDを、
typedefの長い 長いLL。
typedefの符号なしの長い 長いULL。
INTの DIR [ 8 ] [ 2 ] = {{ 10 }、{ 01 }、{ 11 }、{ 1、 - 1 }、{ - 11 }、{ - 1、 - 1 }、 { 0、 - 1 }、{ - 10 }}。
#defineパイACOS(-1)
 の#define LS RT << 1つ
 の#defineのRS RT << 1 | 1つ
 の#define ME0(S)のmemset(S、0、はsizeof(S))
 の#define ME1(S)のmemset(s、1 、はsizeof(S))
 の#define MEF(S)のmemset(S、-1、はsizeof(S))
 の#define meinf(S)のmemset(S、INF、はsizeof(S))
 の#define INF 0x3f3f3f
 のconst  int型 N = 1E6 + 6 ; 
インラインint型リード(){
     チャー C = GETCHAR()。INT X = 0、F = 1 一方、(C < ' 0 ' | C> ' 9 '){もし(C == ' - ')、F = - 1 ; C = GETCHAR();}
     一方(> = C ' 0 ' && C <= ' 9 ')、X = X * 10 + C- ' 0 '、C = getchar関数();
    リターンのx *のF; 
} 
のLL exgcd(LLのB LL){ 
    場合(Bの== 0を返します
    exgcd(B、 B)。
} 
LL q_pow(LLのB -1,11,11- MOD){ 
    LL ANSS = 1 一方、(B){
        もし(B&1)ANSS = ANSS *%のMOD。= A *%のMOD。
        B >> = 1 
    } 
    戻りANSS。
} 
LL q_mul(LLのB -1,11,11- MOD){ 
    LL ANSS = 0 一方、(b)は、{
         もし、(B&1)ANSS =(ANSS + A)%MOD。=(A + A)%MOD。
        B >> = 1 
    } 
    戻りANSS。
} 
INT DP [ 105 ] [ 105 ]。
int型和[ 105 ]。
int型の石[ 105 ]。
INTメイン(int型 ARGC、チャー *のARGV []){ 
    IOS :: sync_with_stdio()。
    int型のn; 
    cinを >> N; 
    ME0(合計)。
    meinf(DP)。
    ためにint型 I = 1 ; iが<= N; iは++ ){ 
        CIN >> 石[I]。
        和[I] =和[I- 1 ] + 石[I]。
        DP [i]は[I] = 0 ; 
    } 
    のためにINT LEN = 1. ; LEN <= N-; LEN ++){ // 列挙長さ
        のためにINT J = 1 ; J + LEN <= N + 1。 ; J ++){ // 列挙開始点、終了<= N 
            INTは Jを終了= LENの+ 1 ;
             のためのint型 ; Iは、端部を<; I = JをIは++){ // 列挙分割点、セル更新の間の最適解     
                DP [j]は= [終了]分(DP [J] [終了]を、 DP [J] [I] + DPは[Iは+ 1 ] [終了] + SUMは、[終了] -sum [J- 1 ]); 
            } 
        } 
    } 
    COUT << DPは[ 1。 [N-] <<] ENDL;
     戻り 0 
}

但是这样一眼就看出来了复杂度是n3的复杂度,这个复杂度数据稍稍大点就爆了,所以还是要用到四边形不等式优化。

但是由于个人感觉很复杂,看了不是很懂,直接贴个链接:四边形不等式优化

优化过的AC的代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int dir[8][2]={{1,0},{0,1},{1,1},{1,-1},{-1,1},{-1,-1},{0,-1},{-1,0}};
#define pi acos(-1)
#define ls rt<<1
#define rs rt<<1|1
#define me0(s) memset(s,0,sizeof(s))
#define me1(s) memset(s,1,sizeof(s))
#define mef(s) memset(s,-1,sizeof(s))
#define meinf(s) memset(s,inf,sizeof(s))
#define inf 0x3f3f3f
const int N=1e6+6;
inline int read() {
    char c=getchar(); int x=0, f=1;
    while(c<'0'|c>'9') {if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}
ll exgcd(ll a,ll b){
    if(b==0) return a;
    exgcd(b,a%b);
}
ll q_pow(ll a,ll b,ll mod){
    ll anss=1;
    while(b){
        if(b&1) anss=anss*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return anss;
}
ll q_mul(ll a,ll b,ll mod){
    ll anss=0;
    while(b){
        if(b&1) anss=(anss+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return anss;
}
int dp[105][105];
int sum[105];
int stone[105];
int main(int argc, char * argv[]){
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    me0(sum);
    meinf(dp);
       int s[111][111];
    for(int i=1;i<=n;i++){
        cin>>stone[i];
        sum[i]=sum[i-1]+stone[i];
        dp[i][i]=0;
        s[i][i]=i;
    }
    for(int len=1;len<=n;len++){//枚举长度
        for(int j=1;j+len<=n+1;j++){//枚举起点,ends<=n
            int ends=j+len-1;
            for(int k=s[j][ends-1];k<=s[j+1][ends];k++){
                if(dp[j][ends]>dp[j][k]+dp[k+1][ends]+sum[ends]-sum[j-1]){
                    dp[j][ends]=dp[j][k]+dp[k+1][ends]+sum[ends]-sum[j-1];
                    s[j][ends]=k;
                }
            }
        }
    }
    cout<<dp[1][n]<<endl;
    return 0;
}

 

おすすめ

転載: www.cnblogs.com/wushengyang/p/11502339.html