2014西安网络预选赛1003(DP+剪枝)hdu5009

Paint Pearls

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 575    Accepted Submission(s): 153


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 each operation, he selects some continuous pearls and all these pearls will be painted to  their target colors. When he paints a string which has k different target colors, Lee will cost k 2 points. 

Now, Lee wants to cost as few as possible to get his ideal string. You should tell him the minimal cost.
 

Input
There are multiple test cases. Please process till EOF.

For each test case, the first line contains an integer n(1 ≤ n ≤ 5×10 4), indicating the number of pearls. The second line contains a 1,a 2,...,a n (1 ≤ a i ≤ 10 9) indicating the target color of each pearl.
 

Output
For each test case, output the minimal cost in a line.
 

Sample Input
   
   
3 1 3 3 10 3 4 2 4 4 2 4 3 2 2
 

Sample Output
   
   
2 7

题意:每次可以给一个区间涂色,花费为该区间段不同颜色数量的平方,求涂完整个序列的最小花费

思路:DP[i]表示到从1涂到i的最小花费

            然后从前往后转移

            假设现在由i往后转移,那么转移的时候要枚举从i+1到j的不同颜色的个数x,然后dp[j]=min(dp[j],dp[i]+x*x)

            但是要加个剪枝,就是当dp[i]+x*x>=dp[n]时就停止

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          using namespace std; #define maxn 50010 int n; int a[maxn]; int vis[maxn]; struct node{ int v; int i; }; node b[maxn]; int stk[maxn]; bool cmp(node a,node b) { return a.v 
         
           =dp[cnt])break; if(tt 
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值