欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 1179 (DP)

程序员文章站 2022-06-30 20:44:15
...

关键就是要想到最大值可能是负负得正得来的,所以我们还需要维护区间的最小值,别的就不难了,很单纯的区间dp
lyd的书上的详解:
POJ 1179 (DP)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
ll mx[120][120],mi[120][120],num[120];
const ll INF=999999999999999ll;
string fh[120];
int main()
{
   // freopen("1.in","r",stdin);
   // freopen("zj.out","w",stdout);
    int n,i,j,k,len;
    cin>>n;
    //memset(mi,0x3f,sizeof(mi));
    for(i=1;i<=n*2;i++)
        for(j=1;j<=n*2;j++)mx[i][j]=-INF,mi[i][j]=INF;
    for(i=1;i<=n;i++){
        cin>>fh[i]>>num[i];
        fh[i+n]=fh[i];num[i+n]=num[i];
        mx[i][i]=mx[i+n][i+n]=mi[i][i]=mi[i+n][i+n]=num[i];
    }
    for(len=1;len<n;len++)
        for(i=1;i+len<=n*2-1;i++)
            for(j=i+1;j<=i+len;j++){
        if(fh[j]=="t") {
            mx[i][i + len] = max(mx[i][i + len], mx[i][j - 1] + mx[j][i + len]);
            mi[i][i + len] = min(mi[i][i + len], mi[i][j - 1] + mi[j][i + len]);
        }
        else {
            mx[i][i + len] = max(mx[i][i + len], mx[i][j - 1] * mx[j][i + len]);
            mx[i][i + len] = max(mx[i][i + len], mi[i][j - 1] * mi[j][i + len]);
            mi[i][i + len] = min(mi[i][i + len], mi[i][j - 1] * mi[j][i + len]);
            mi[i][i + len] = min(mi[i][i + len], mx[i][j - 1] * mi[j][i + len]);
            mi[i][i + len] = min(mi[i][i + len], mi[i][j - 1] * mx[j][i + len]);
        }
    }
    ll ans=-(1LL<<62);
    vector<int>ansedge;
    for(i=1;i<=n;i++) {
        //ans = max(ans, mx[i][i + n - 1]);
        if(mx[i][i+n-1]>ans){
            ans=mx[i][i+n-1];//id=i;
            ansedge.clear();
            ansedge.push_back(i);
        }
        else if(mx[i][i+n-1]==ans)
            ansedge.push_back(i);
    }
    cout<<ans<<endl;
    //cout<<(id-1==0?n:id-1)<<' '<<id<<endl;
    sort(ansedge.begin(),ansedge.end());
    for(i=0;i<ansedge.size();i++)
        printf("%d%c",ansedge[i],i==ansedge.size()-1?'\n':' ');

    return 0;
}