洛谷P1133 教主的花园

恢复手感的第二天,一道简单dp

需要注意的是,1和n是相邻的,这个是环形

刚交的时候就WA了三个点,后来想到了一个结论,就是如果1和n相邻,那么这个限制比单纯的链肯定是次优的,所以环形从2 到 n + 1再做一遍就好了

可以滚动数组优化

#include <bits/stdc++.h>
using namespace std;
#define limit (200000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
inline ll read(){
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if(x/ 10) print(x / 10);
    *O++=x % 10+'0';
}
void write(ll x, char c = 't') {
    if(x < 0)putchar('-'),x = -x;
    print(x);
    if(!isalpha(c))*O++ = c;
    fwrite(obuf,O-obuf,1,stdout);
    O = obuf;
}
int kase;
int n, m,k;
int a[limit][4];
int dp[2][4][4];
//状态转移方程:
// 30可以从20和
void solve(){
    cin>>n;
    int ans = 0;
    int cur = 1, prev = 0;
    swap(cur, prev);
    rep(i,1,n){
        cin>>a[i][1]>>a[i][2]>>a[i][3];
        rep(f,1,3){
           a[i + n][f] = a[i][f];
        }
        rep(j, 1, 3){
            rep(h, 1, 3){
                rep(l, 1, 3){
                    if(h > l and h > j){
                        dp[cur][l][h] = max(dp[cur][l][h], dp[prev][h][j] + a[i][l]);
                    }
                    if(h < l and h < j){
                        dp[cur][l][h] = max(dp[cur][l][h], dp[prev][h][j] + a[i][l]);
                    }

                }
            }
        }
        if(i != n)swap(cur, prev);
    }
    int ans1 = 0;
    rep(i,1,3){
        rep(j, 1, 3){
            ans = max(ans, dp[cur][i][j]);
        }
    }
    memset(dp, 0, sizeof(dp));
    rep(i,2,n + 1){
        rep(j, 1, 3){
            rep(h, 1, 3){
                rep(l, 1, 3){
                    if(h > l and h > j){
                        dp[cur][l][h] = max(dp[cur][l][h], dp[prev][h][j] + a[i][l]);
                    }
                    if(h < l and h < j){
                        dp[cur][l][h] = max(dp[cur][l][h], dp[prev][h][j] + a[i][l]);
                    }

                }
            }
        }
        if(i != n + 1)swap(cur, prev);
    }
    rep(i,1,3){
        rep(j, 1, 3){
            ans1 = max(ans1, dp[cur][i][j]);
        }
    }



    cout<<min(ans1, ans)<<endl;

}
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
//    cin>>kase;
//    while (kase--)
    solve();
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

posted @ 2021-12-26 11:26  tiany7  阅读(44)  评论(0编辑  收藏  举报