传说的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. 【推介】TMS的控件之“TMS Unicode Component Pack”和“TMS Advanced Toolbars & Menus”

    TMS Unicode Component Pack是一款支持多语言的界面控件包,提供超过60种控件来为你的Delphi和C++Builder应用程序添加Unicode支持. 介绍: TMS Unic ...

  2. programming ruby

    ri #rdoc reader attr_reader attr_writer @@xx 类变量都是私有的 def 类名.xx end 类方法 [1,3,5,7].inject(0){|sum,e| ...

  3. java基本概念

    什么是环境变量? 环境变量通常是指在操作系统当中,用来指定操作系统运行时需要的一些参数.通常为一系列的键值对. path环境变量的作用 path环境变量是操作系统外部命令搜索路径 什么是外部命令搜索路 ...

  4. SOCKET 地址

    地址格式: 函数bind和getsockname使用通用数据类型:struct sockaddr*来指向socket地址. #incude <sys/socket.h> struct so ...

  5. oracle 查看隐含参数脚本

    set linesize 132 column name format a30 column value format a25 select x.ksppinm name, y.ksppstvl va ...

  6. 深度神经网络DNN的多GPU数据并行框架 及其在语音识别的应用

    深度神经网络(Deep Neural Networks, 简称DNN)是近年来机器学习领域中的研究热点,产生了广泛的应用.DNN具有深层结构.数千万参数需要学习,导致训练非常耗时.GPU有强大的计算能 ...

  7. Libevent windows/linux下编译

    1.windows下: 编译环境: windows xp sp3 + vs2010 (1)    解压libevent-2.0.21-stable.tar.gz到D:\libevent-2.0.21- ...

  8. ext4.1动态生成多个checkboxgroup(或者radiogroup),并且有toolbar操作、

    转载自:http://blog.csdn.net/zhengyuechuan/article/details/9327291 前台controller代码: Ext.define('zyc.contr ...

  9. iOS8中如何将状态栏的字体颜色改为白色

    网上的一些方法在我这行不通, 比如: UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent ...

  10. 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据

    在请求方法的顶部添加        [ValidateInput(false)]就OK了 从客户端(Content=" sdfdddd ...")中检测到有潜在危险的 Reques ...