[IOI 1998] Polygon
[题目链接]
http://poj.org/problem?id=1179
[算法]
区间DP
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 55 const long long INF = 1e15; int i,j,k,l,r,n; long long num[MAXN<<1]; long long mn[MAXN<<1][MAXN<<1],mx[MAXN<<1][MAXN<<1]; long long ans; char op; char opt[MAXN<<1]; int main() { scanf("%d",&n); getchar(); for (i = 1; i <= n; i++) { if (i > 1) scanf(" %c",&op); else scanf("%c",&op); scanf("%lld",&num[i]); opt[i] = opt[i + n] = (op == 't'); num[i+n] = num[i]; } for (i = 1; i <= 2 * n; i++) { for (j = i + 1; j <= 2 * n; j++) { mx[i][j] = -INF; mn[i][j] = INF; } } for (i = 1; i <= 2 * n; i++) mx[i][i] = mn[i][i] = num[i]; for (i = 2; i <= n; i++) { for (l = 1; l <= 2 * n - i + 1; l++) { r = l + i - 1; for (k = l; k < r; k++) { if (opt[k+1]) { mn[l][r] = min(mn[l][r],mn[l][k] + mn[k+1][r]); mx[l][r] = max(mx[l][r],mx[l][k] + mx[k+1][r]); } else { mn[l][r] = min(mn[l][r],min(mx[l][k] * mx[k+1][r],(mx[l][k] * mn[k+1][r],mn[l][k] * mx[k+1][r]))); mx[l][r] = max(mx[l][r],max(mx[l][k] * mx[k+1][r],mn[l][k] * mn[k+1][r])); } } } } for (i = 1; i <= n; i++) ans = max(ans,mx[i][i+n-1]); printf("%lld\n",ans); for (i = 1; i <= n; i++) { if (mx[i][i+n-1] == ans) printf("%d ",i); } printf("\n"); return 0; }