HDU 4960 Another OCD Patient 简单DP

思路:

  因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和。根据这个性质,我们可以预处理出这个数列的对称点对。然后最后一个对称段是从哪里开始的,做n^2的DP就可以了。

代码:

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 #include <string>
 8 #include <queue>
 9 #include <stack>
10 #include <vector>
11 #include <map>
12 #include <set>
13 #include <functional>
14 #include <cctype>
15 #include <time.h>
16
17 using namespace std;
18
19 typedef __int64 ll;
20
21 const int INF = 1<<30;
22 const int MAXN = (int) 5055;
23
24 inline void nextInt(int &x) {
25     char c = getchar();
26     x = 0;
27     while (isdigit(c)) {
28         x = x*10 + c-‘0‘;
29         c = getchar();
30     }
31 }
32
33 inline void nextLL(ll &x) {
34     char c = getchar();
35     x = 0;
36     while (isdigit(c)) {
37         x = x*10 + c-‘0‘;
38         c = getchar();
39     }
40 }
41
42 ll a[MAXN], V[MAXN], prefix[MAXN], suffix[MAXN];
43 ll dp[MAXN];
44 int sym[MAXN];
45 int n;
46
47 void solve() {
48     a[0] = 0;
49     prefix[0] = suffix[n+1] = 0;
50     for (int i = 1; i <= n; i++) prefix[i] = suffix[i] = V[i];
51     for (int i = 0; i < n; i++) prefix[i+1] += prefix[i]; //前缀和
52     for (int i = n; i > 0; i--) suffix[i] += suffix[i+1]; //后缀和
53
54     for (int i = 1, j = n; i <= n; i++) { //求对称点
55         sym[i] = -1;
56         while (j>0 && prefix[i]>suffix[j]) j--;
57         if (prefix[i]==suffix[j]) sym[i] = j;
58     }
59
60     memset(dp, -1, sizeof(dp));
61     for (int i = 1; i <= n; i++) if (sym[i]>0) { //这一点有对称点
62         if (sym[i] <= i) break; //枚举过界
63         dp[i] = a[i] + a[n-sym[i]+1]; //前面是一整段
64         for (int j = 1; j < i; j++) if (sym[j]>0) { //从j转移过来
65             dp[i] = min(dp[i], dp[j]+a[i-j]+a[sym[j]-sym[i]]);
66         }
67     }
68
69     ll ans = a[n];
70     for (int i = 1; i <= n; i++) if (dp[i]>=0)
71         ans = min(ans, dp[i]+a[sym[i]-i-1]); //中间合成一段
72     printf("%I64d\n", ans);
73 }
74
75 int main() {
76     #ifdef Phantom01
77         freopen("HDU4960.txt", "r", stdin);
78     #endif //Phantom01
79
80     while (1) {
81         nextInt(n);
82         if (n==0) break;
83         for (int i = 1; i <= n; i++)
84             nextLL(V[i]);
85         for (int i = 1; i <= n; i++)
86             nextLL(a[i]);
87         solve();
88     }
89
90     return 0;
91 }

HDU 4960 Another OCD Patient 简单DP,布布扣,bubuko.com

时间: 2024-11-07 00:03:21

HDU 4960 Another OCD Patient 简单DP的相关文章

HDU 4960 Another OCD Patient 区间dp

区间dp.. T^T一直感觉是n^3,看了题解看来是数据水了么.. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string.h> #define ll long long #define inf 1e8 inline int min(int a, int b){return a<b?a:b;} inline void rdl(ll

hdu 4960 Another OCD Patient(dp)2014多校训练第9场

Another OCD Patient                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) pat

HDU 4960 Another OCD Patient(区间dp记忆化搜索)

题目大意:给你一串数字让你判断经过若干次合并,使得这个数字串变成回文串的最小成本是多少.第一行是数字串,第二行是合并连续i个数字的成本是多少. 解题思路:区间dp,可以进行记忆化搜索,如果左边比右边和大那么右边一定是小了,右边比左边大那么左边一定小了.因为保证有解.具体不太好说,直接看代码吧. Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe

hdu 4960 Another OCD Patient(dp)

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 645    Accepted Submission(s): 238 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This mornin

HDU 4960 Another OCD Patient(记忆化搜索)

HDU 4960 Another OCD Patient 题目链接 记忆化搜索,由于每个碎片值都是正数,所以每个前缀和后缀都是递增的,就可以利用twopointer去找到每个相等的位置,然后下一个区间相当于一个子问题,用记忆化搜索即可,复杂度接近O(n^2) 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f

hdu 4960 Another OCD Patient(记忆化)

题目链接:hdu 4960 Another OCD Patient 题目大意:给定一个长度为n的序列,然后再给出n个数ai,表示合成i个数的代价.每次可以将连续的子序列和成一个数,即为序列中各个项的和.要求将给定长度n的序列变成一个回文串,一个数字只能被合成一次. 解题思路:dp[l][r]表示从l到r被和成回文串的最小代价,dp[l][r]=min(val(r?l+1),val(r?i+1)+val(j?l+1)+dp[j+1][i?1]),当i每减少1,对应的j一定变大,这一点可以减少大部分

hdu 4960 Another OCD Patient

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 87    Accepted Submission(s): 24 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the

HDU 5375 Gray code (简单dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 626    Accepted Submission(s): 369 Problem Description The reflected binary cod