Paint Pearls

Paint Pearls

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009

dp+双向链表优化

看到题目,很自然地可以定义状态:dp[i]表示涂好a[0...i]的字符串,花费的最小代价.

状态转移方程:dp[i]=min(dp[i],dp[j]+num2),其中num=从a[j]到a[i]不同的数字个数.

时间复杂度为O(n2),对于n=50000的数据,明显会T.

于是,我们需要进行优化。注意到状态数无法化简,考虑优化转移复杂度.

当区间[j+1,i]中包含元素a[j]时,无需再经过这个点,直接跳到a[j-1];

即a[j+1]前面略过a[j],直接为a[j-1],使得现序列中各个不同元素只出现一次.

而这种结构可以用双向链表维护.//之前用的是set,T了后查了下clear()是O(n)的,尴尬...

但是当序列为1,2,3,4,5,6,7这种互不相同的元素时,复杂度仍会退化为O(n2),

这时,则需要用到剪枝的技巧:当num2>i时,肯定不会比一个一个涂色方法更优,

由此,复杂度变为O(n3/2

代码如下:

 1 #include<cstdio>
 2 #include<map>
 3 #define Min(x,y) (x<y?x:y)
 4 #define N 50005
 5 using namespace std;
 6 const int INF=N;
 7 struct List{
 8     int pre,nxt;
 9     List(int _pre=-1,int _nxt=-1){//è?L[0].pre???ò-1
10         pre=_pre;
11         nxt=_nxt;
12     }
13 }L[N];
14 int n,a[N],idx,num,dp[N];
15 map<int,int>mp;
16 int main(void){
17     while(~scanf("%d",&n)){
18         mp.clear();
19         for(int i=1;i<=n;++i){
20             scanf("%d",&a[i]);
21             L[i]=List(i-1,i+1);
22         }
23         for(int i=1;i<=n;++i){
24             dp[i]=INF;
25             if(mp.find(a[i])==mp.end()){
26                 mp[a[i]]=i;
27             }else{
28                 idx=mp[a[i]];
29                 L[L[idx].pre].nxt=L[idx].nxt;
30                 L[L[idx].nxt].pre=L[idx].pre;
31                 mp[a[i]]=i;
32             }
33             for(num=1,idx=L[i].pre;idx>=0;idx=L[idx].pre,num++){
34                 dp[i]=Min(dp[i],dp[idx]+num*num);
35                 if(num*num>i)break;
36             }
37         }
38         printf("%d\n",dp[n]);
39     }
40 }
时间: 2024-11-05 06:10:40

Paint Pearls的相关文章

HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 Accepted 5009 1265MS 1980K 2290 B G++ czy   Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm

HDU 5009 Paint Pearls (动态规划)

Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In eac

HDOJ 5009 Paint Pearls

DP + 优化 ,因为花费是n^2的,所以num×num 大于 DP[i]的时候就可以跳出了.... Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1245    Accepted Submission(s): 395 Problem Description Lee has a string of n pea

HDU 5009 Paint Pearls _(:зゝ∠)_2014 ACM/ICPC Asia Regional Xi&#39;an Online

呵呵 #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> typedef long long ll; using namespace std; const int N = 5 * 10000 + 5; int xval[N], dep; int n, a[N], pre[N]; ll d[N]; int pos[300], dd; void work()

hdu5009 Paint Pearls (DP+模拟链表)

http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1951    Accepted Submission(s): 631 Problem Description Lee has a str

HDU 5009 Paint Pearls(西安网络赛C题)

HDU 5009 Paint Pearls 题目链接 题意:给定一个目标颜色,每次能选一个区间染色,染色的代价为这个区间不同颜色数的平方,问最小代价 思路:先预处理,把相同颜色的一段合并成一个点,然后把颜色离散化掉,然后进行dp,dp[i]表示染到第i个位置的代价,然后往后转移,转移的过程记录下不同个数,这样就可以转移了,注意加个剪枝,就是如果答案大于了dp[n]就不用往后继续转移了 代码: #include <cstdio> #include <cstring> #include

AC日记——Paint Pearls hdu 5009

Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 50005 int n,ai[maxn],dp[maxn],ls[

HDU 5009 Paint Pearls 双向链表优化DP

Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In eac

hdu 5009 Paint Pearls (动态规划)

Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2359    Accepted Submission(s): 761 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color