传说的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. Python初学者笔记:打印出斐波那契数列的前10项

    问题:斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.- ...

  2. 简答的理解C语言中的各种类型函数

    1.变参函数 变长参数的函数即参数个数可变.参数类型不定 的函数.最常见的例子是printf函数.scanf函数和高级语言的Format函数.在C/C++中,为了通知编译器函数的参数个数和类型可变(即 ...

  3. C/C++ 对常见字符串库函数的实现

    在c中的string.h头文件中存在很多对字符串进行操作的函数,利用这些函数可以方便的对字符串进行操作.下面将对常见的字符串函数进行解释和实现. strcpy 函数原型:char* _strcpy(c ...

  4. kettle删除资源库中的转换或者作业

    在资源库中新建转换,作业都很简单,那么加入现在不需要其中某个转换或者作业该怎么办呢? 下图是已经存在的转换跟作业 现在需要删除aa这个转换 操作步骤如下: 1.工具----资源库----探索资源库 出 ...

  5. core java 10~12(多线程 & I/O & Network网络编程)

    MODULE 10 Threads 多线程-------------------------------- 进程: 计算机在运行过程中的任务单元,CPU在一个时间点上只能执行一个进程,但在一个时间段上 ...

  6. asp.net实现手机号码归属地查询,代码如下

    protected void Button1_Click(object sender, EventArgs e)        {            if (Regex.IsMatch(TextB ...

  7. microsoft azure 速度测试网址

    http://www.azurespeed.com/ 选择你附近的区域.可以使用 azurespeed.com 查找延迟最低的数据中心.

  8. metaq

    MetaQ(全称Metamorphosis)是一个高性能.高可用.可扩展的分布式消息中间件,思路起源于LinkedIn的Kafka,但并不是Kafka的一个Copy.MetaQ具有消息存储顺序写.吞吐 ...

  9. Go中的指针与函数接收器

    Go中使用*号表示指针,但是没有指针算数,不能对其进行加减.同时内存管理都由Go来负责,不需要拖动释放内存. Go中的函数接收者,可以为值类型,也可以是引用类型. 看代码: package main ...

  10. Layout Support 获取上下bar的长度

    Layout Support This protocol . You can use layout guides as layout items in the NSLayoutConstraint f ...