传说的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. MIS2000 Lab,我的IT人生与职场--从零开始的前十五年 与 我的微创业

    http://www.dotblogs.com.tw/mis2000lab/archive/2014/09/16/ithome_2014_ironman.aspx [IT邦幫忙]鐵人賽 -- MIS2 ...

  2. c# float和double的“坑”

    定义一个float类型的变量=0.7,结果在IL中却是0.69999999. 乘以10之后,获取整数值.得到的却是6.通过查看IL,竟然被转换成double类型再转换.就变成6了. Demo: IL: ...

  3. EF经验分享_jimmyzzc

    刚刚接触EF,总结了一些在实际工作中的心德经验,与大家分享一下. 一.Excression表达式树 表达式目录树在LINQ中用于表示分配给类型为Expression<TDelegate>的 ...

  4. Python脚本控制的WebDriver 常用操作 <十一> 操作测试对象

    下面将使用WebDriver来模拟键盘的输入操作,以及复习上节的层对象操作 测试用例场景 定位到具体的对象后,我们就可以对这个对象进行具体的操作,比如先前已经看到过的点击操作(click).一般来说, ...

  5. 每日一练--C语言--串

    目标 实现串的经典模式匹配算法与KMP算法. 简述 自定义串结构: 串采用定长顺序存储结构,串从下标1开始存储,0下标存储串的实际长度: 匹配成功返回匹配位置,匹配失败返回0. #include &l ...

  6. Linux中printf格式化输出

    printf使用文本或者由空格分隔的参数,我们可以在printf中使用格式化字符串.printf不会写像echo那样自动添加换行符,必须手动添加 =========================== ...

  7. Redis 五:配置主从复制功能

    redis的主从复制事实上是非常简单的一件事情,甚至比mysql的配置还简单,因为基本不需要在主服务器上做任何操作 我们在同一台服务器上开不同的端口进行测试操作(安装部分就不说啦,前面的文章有::) ...

  8. golang实现ping命令

    // Copyright 2009 The Go Authors.  All rights reserved.// Use of this source code is governed by a B ...

  9. iOS9之Bitcode

    Bitcode是被编译程序的一种中间形式的代码. 更新Xcode7后需要将”Build Settings”->”Enable Bitcode”设为NO,保证第三方库能真机运行项目.       ...

  10. 获取 UIWebView中用户所点击的图片URL

    在使用 UIWebView 的时候 (通常是阅读类的 App),会有点击图片放大的需求,那么可以通过设置 UIWebViewDelegate 来过滤请求,取出图片的 URL 这个方法的前提是 img ...