传说的SB DP:

题目

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 k2 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×104), indicating the number of pearls. The second line contains a1,a2,...,an (1 ≤ ai ≤ 109) 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
 
这道题很容易想到方程,但是TLE居多,下面讲讲我的优化!
首先DP方程:DP[J]=MIN(DP[J],DP[I]+SX[I+1,J]^2); I<=J<=N; SX[I,J];表示I-J中有多少个颜色不一样的
我们知道dp[j]最大为J;
所以有假如我们算到DP[I]>=DP[N];那么就可BREAK;
有了这个数据题目就作对了一般,但是遇到全为一样数的数组时,效率不够高
我们需要离散一下。
注意:这份代码在C++ 是TLE ,在G++ 是1S;
对于编译器我无力:

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
#define N 55555
int b[N],a[N];
int c[N];
int num[N];
int tmp[N];
int dp[N];
vector<int> q;
int vis[N];
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
memset(dp,0x3f3f3f,sizeof(dp));
for (int i=;i<=n;i++)
scanf("%d",&a[i]);
int t=;
for (int i=;i<=n;i++)
if (b[t]!=a[i]) b[++t]=a[i];
for (int i=;i<=t;i++) c[i]=b[i];
sort(c+,c+t+);
int tt=;
for (int i=;i<=t;i++)
if (c[i]!=c[i-]) a[++tt]=c[i];
for (int i=;i<=t;i++) c[i]=lower_bound(a+,a+tt+,b[i])-a;//离散过程这里用STL处理,前面一段都是把数组离散一下 dp[t]=t;dp[]=;
memset(vis,,sizeof(vis)); for (int i=;i<t;i++)
{ for (int j=i+;j<=t;j++)
{
if (!vis[c[j]]) vis[c[j]]=,q.push_back(c[j]);
int k=q.size();
if (k*k+dp[i]>=dp[t]) break;
dp[j]=min(dp[j],dp[i]+k*k);//没有用long long ,因为超了LONG LONG 就break
} for (int j=;j<q.size();j++)
vis[q[j]]=;
q.clear();
}
printf("%d\n",dp[t]);
}
return ;
}
 
 

2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls的更多相关文章

  1. hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)

    Mart Master II Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. 2014 ACM/ICPC Asia Regional Xi'an Online

    03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为 ...

  3. HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)

    思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’   'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...

  4. 2014 ACM/ICPC Asia Regional Xi'an Online(HDU 5007 ~ HDU 5017)

    题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Son ...

  5. HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

    Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submiss ...

  6. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  7. HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...

  8. 2014 ACM/ICPC Asia Regional Shanghai Online

    Tree http://acm.hdu.edu.cn/showproblem.php?pid=5044 树链剖分,区间更新的时候要用on的左++右--的标记方法,要手动扩栈,用c++交,综合以上的条件 ...

  9. 2014 ACM/ICPC Asia Regional Guangzhou Online

    Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...

随机推荐

  1. How to display SSRS report based on customer/Vendor specific language [AX2012]

    Common requirement is to show the reports in customer’s language. [example : Quotations, sales confi ...

  2. Jquery + echarts 使用

    常规用法,就不细说了,按照官网一步步来. 本文主要解决问题(已参考网上其他文章): 1.把echarts给扩展到JQuery上,做到更方便调用. 2.多图共存 3.常见的X轴格式化,钻取时传业务实体I ...

  3. EmguCV学习——简单算法 差分与高斯

    公司项目需要检测运动物体,我对opencv也没啥研究,google了好久看了好多方法,最简单的就是差分与高斯背景建模了. 旁边搞c++的同事正在搞更nb的算法,等出来了 我再转成C#版的分享. 先看差 ...

  4. static 与 final 修饰符

    A) 用static(静态)修饰属性:一个类生成了N个对象,这些对象会共同使用一份静态的成员变量.一个对象对这个成员变量进行修改,其他对象的该成员变量的值也会随之变化. B) 我们可以通过 类名.成员 ...

  5. Map和HashMap

    通过查询JDK帮助文档,我们可以得知Map的说明.方法等 import java.util.Map; import java.util.HashMap; class Test{ public stat ...

  6. Redis客户端之Spring整合Jedis

      1.下载相关jar包,并引入工程: jedis-2.4.2.jar commons-pool2-2.0.jar 2.将以下XML配置引入spring <bean id="shard ...

  7. expr命令

    expr命令的兩大作用:1)四则运算:2)字符串的操作: 1.四则运算 [tough@localhost ~]$ + + [tough@localhost ~]$ + [tough@localhost ...

  8. 记录一下mvc发布

    让别人也可以访问你电脑上的ASP.NET MVC创建的网站 http://www.cnblogs.com/laoqi/p/4169184.html

  9. vnextcn

    Flag 标题 通过 提交 AC% 难度   E1 编写Hello World网站 9 17 52% 1   E2 编写Hello World控制台程序 11 13 84% 1   E3 控制器基础练 ...

  10. [转]STUN和TURN技术浅析

    [转]STUN和TURN技术浅析 http://www.h3c.com.cn/MiniSite/Technology_Circle/Net_Reptile/The_Five/Home/Catalog/ ...