POJ 1179 DP

// DP 

#include<iostream>

#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#define MAXN 55
#define MAXM 55
#define MOD 10000
#define inf 1e15
#define eps 1e-9
#define LL long long 
using namespace std;
int n, idx[MAXN], len;
char op[MAXN][5];
LL v[MAXN], dp[MAXN][MAXN][2];
void solve(int i, int s, int j, LL &minf, LL &maxf) {
int r = (i + s - 1) % n + 1;
LL a = dp[i][s][0], b = dp[i][s][1], c = dp[r][j - s][0], d = dp[r][j - s][1], tmp[4];
if (op[r][0] == 't') {
minf = a + c, maxf = b + d;
} else {
tmp[0] = a * c, tmp[1] = a * d, tmp[2] = b * c, tmp[3] = b * d;
sort(tmp, tmp + 4);
minf = tmp[0], maxf = tmp[3];
}
int a[3];
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%s %lld", op[i], &v[i]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j][0] = (LL)inf, dp[i][j][1] = -(LL)inf;
}
}
for (int i = 1; i <= n; i++) dp[i][1][0] = dp[i][1][1] = v[i]; 
LL minf, maxf;
bool flag = false;
for (int j = 2; j <= n; j++) { 
for (int i = 1; i <= n; i++) {
for (int s = 1; s < j; s++) {
solve(i, s, j, minf, maxf);
dp[i][j][0] = min(dp[i][j][0], minf);
dp[i][j][1] = max(dp[i][j][1], maxf);
}
}
}
LL ans = dp[1][n][1];
idx[len = 1] = 1;
for (int i = 2; i <= n; i++) {
if (ans == dp[i][n][1]) {
idx[++len] = i;
} else if (dp[i][n][1] > ans) {
ans = dp[i][n][1];
idx[len = 1] = i;
}
}
sort(idx + 1, idx + len + 1);
printf("%lld\n", ans);
for (int i = 1; i <= len; i++) {
if (i != 1) putchar(' ');
printf("%d", idx[i]);
}
puts("");
    return 0;
}

posted on 2012-11-26 22:57  Sure_Yi  阅读(270)  评论(0编辑  收藏  举报

导航