1 /* 2 题意:一只兔子顺时针跳,另一只逆时针跳,跳石头权值相等而且不能越过起点 3 LPS:这道就是LPS的应用,把环倍增成链,套一下LPS,然而并不能理解dp[i][i+n-2] + 1,看别人的解题报告吧,以后来补(玩游戏) 4 详细解释 5 */ 6 /************************************************ 7 * Author :Running_Time 8 * Created Time :2015-8-8 16:57:23 9 * File Name :HDOJ_4747_LPS.cpp 10 ************************************************/ 11 12 #include <cstdio> 13 #include <algorithm> 14 #include <iostream> 15 #include <sstream> 16 #include <cstring> 17 #include <cmath> 18 #include <string> 19 #include <vector> 20 #include <queue> 21 #include <deque> 22 #include <stack> 23 #include <list> 24 #include <map> 25 #include <set> 26 #include <bitset> 27 #include <cstdlib> 28 #include <ctime> 29 using namespace std; 30 31 #define lson l, mid, rt << 1 32 #define rson mid + 1, r, rt << 1 | 1 33 typedef long long ll; 34 const int MAXN = 2e3 + 10; 35 const int INF = 0x3f3f3f3f; 36 const int MOD = 1e9 + 7; 37 int a[MAXN], dp[MAXN][MAXN]; 38 39 40 int main(void) { //HDOJ 4745 Two Rabbits 41 int n; 42 while (scanf ("%d", &n) == 1) { 43 if (!n) break; 44 for (int i=1; i<=n; ++i) { 45 scanf ("%d", &a[i]); 46 a[i+n] = a[i]; 47 } 48 memset (dp, 0, sizeof (dp)); 49 for (int i=1; i<=2*n; ++i) dp[i][i] = 1; 50 for (int i=2; i<2*n; ++i) { 51 for (int j=1; j+i-1<=2*n; ++j) { 52 if (a[j] == a[j+i-1]) dp[j][j+i-1] = dp[j+1][j+i-2] + 2; 53 else { 54 dp[j][j+i-1] = max (dp[j+1][j+i-1], dp[j][j+i-2]); 55 } 56 } 57 } 58 int ans = 0; 59 for (int i=1; i<=n; ++i) { 60 ans = max (ans, dp[i][i+n-1]); 61 ans = max (ans, dp[i][i+n-2] + 1); 62 } 63 printf ("%d\n", ans); 64 } 65 66 return 0; 67 }
时间: 2024-10-12 20:30:19