由题意我们能够知道,花费最多为n。

所以单次最多涂掉sqrt(n)种颜色。

dp[i]:涂到第i个位置。之前的花费最少为多少。

biao[i][j]:在第i个位置,往前涂j-1种颜色,涂到哪个位置。

vis[i]:i颜色最后出现的位置,不存在等于-1。

我们先离散化颜色。

然后非常显然转移方程:

dp[i]=min(dp[i],dp[biao[i][j]]+(j+1)*(j+1));

重点是biao[i][j]怎么求;

假如a[i]=x;

假设vis[x]=-1,那么非常显然biao[i][j]=biao[i-1][j-1];

否则vis[x]=y:

那么假设biao[i-1][j]>y。biao[i][j]=biao[i-1][j-1];

假设biao[i-1][j]<=y,biao[i][j]=biao[i-1][j];

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define maxn 55000
#define mod 10000007
#define LL __int64
int vis[maxn];
int dp[maxn];
int biao[2][301];
int a[maxn];
struct list
{
int x;
int id;
friend bool operator <(const list &a,const list &b)
{
return a.x<b.x;
}
}nn[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
nn[i].x=a[i];
nn[i].id=i;
}
sort(nn+1,nn+n+1);
for(int i=1; i<=n; i++)
{
if(nn[i].x!=nn[i-1].x)
{
a[nn[i].id]=i;
}
else
{
a[nn[i].id]=a[nn[i-1].id];
}
}
memset(vis,-1,sizeof(vis));
memset(dp,0,sizeof(dp));
memset(biao,-1,sizeof(biao));
int m=sqrt(1.0*n);
for(int i=1; i<=n; i++)
{
int x=a[i];
int k=i&1;
int ks=(!k);
if(x!=a[i-1])biao[k][0]=i-1;
else biao[k][0]=biao[ks][0];
dp[i]=dp[biao[k][0]]+1;
int p;
p=-1;
for(int j=1; j<=m; j++)
{
if(vis[x]==-1||vis[x]<biao[ks][j-1])biao[k][j]=biao[ks][j-1];
else
{
biao[k][j]=biao[ks][j];
}
if(biao[k][j]==-1)break;
dp[i]=min(dp[i],dp[biao[k][j]]+(j+1)*(j+1));
}
vis[x]=i;
}
printf("%d\n",dp[n]);
}
return 0;
}

hdu-5009-Paint Pearls-dp的更多相关文章

  1. HDU - 5009 Paint Pearls(dp+优化双向链表)

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  2. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

    转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...

  3. HDU 5009 Paint Pearls 双向链表优化DP

    Paint Pearls Problem Description   Lee has a string of n pearls. In the beginning, all the pearls ha ...

  4. HDU 5009 Paint Pearls (动态规划)

    Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  5. hdu 5009 Paint Pearls

    首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...

  6. hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题目要求对空序列染成目标颜色序列,对一段序列染色的成本是不同颜色数的平方. 这题我们显然会首先想到用DP去 ...

  7. HDOJ 5009 Paint Pearls

    Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...

  8. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

  9. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

  10. hdu5009 Paint Pearls[指针优化dp]

    Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

随机推荐

  1. PHPUnit测试

    今天单元测试测到一个有点坑的小问题: public function testUpdataStatusFailForNegative() { // // Remove the following li ...

  2. 机器视觉工具箱-Machine Vision Toolbox for Matlab

    发现了一个机器视觉的Matlab工具箱,分享一下. 机器视觉工具箱(MVT的)规定,在机器视觉和基于视觉的控制有益的多种功能.这是一个有点折衷收集反映作者在光度学,摄影测量,色度学 方面的个人利益.它 ...

  3. Quartz1.8.5例子(五)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  4. MFC窗口分割以及各窗口间的通讯

    一个偶然的机会又重新接触了MFC窗口的分割,自己结合资料重新写了一个窗口分割的程序,现将具体流程跟大家分享一下: 1.我们先创建一个MFC单文档类的程序,具体分割方式先将单文档整个客户区分成两行一列, ...

  5. vim文本编辑器的总结

    说起文本编辑器,在下就不得不说vim这个老古董了,说老吧,但也是老当益壮,至今还是有不少拥护者.的确,vim很实用,在下就先列出vi的优点吧. 1.如果精通了vim,就可以省去鼠标了. 2.vim是跨 ...

  6. MFC应用程序创建窗口的过程 good

    MFC应用程序中处理消息的顺序 1.AfxWndProc()      该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc 2.AfxCallWndProc()  该 ...

  7. div 背景色设置_DIV背景颜色设置

    DIV 背景色设置篇-div背景颜色设置篇 一.div标签内直接设置背景颜色   -   TOP <div style="background:#000; color:#FFF&quo ...

  8. lc面试准备:Number of 1 Bits

    1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  9. 修改uCOS_II以实现“优先级+时间片”联合调度

    本文在uCOS II上增加时间片任务调度的的原理: 对设置为同优先级的任务使用时间片调度,不同优先级任务仍然使用uCOS II的优先级调度策略.在同优先级任务的时间片调度中,所有任务暂时时间片长度固定 ...

  10. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...