转自:http://blog.csdn.net/accelerator_/article/details/39271751

吐血ac。。。

11668627 2014-09-16 22:15:24 Accepted 5009 1265MS 1980K 2290 B G++

czy

 

Paint Pearls

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1473    Accepted Submission(s): 466

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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5017 5016 5014 5013 5011 

转自:http://blog.csdn.net/accelerator_/article/details/39271751

题意:给定一个目标颜色,每次能选一个区间染色,染色的代价为这个区间不同颜色数的平方,问最小代价

思路:先预处理,把相同颜色的一段合并成一个点,然后把颜色离散化掉,然后进行dp,dp[i]表示染到第i个位置的代价,然后往后转移,转移的过程记录下不同个数,这样就可以转移了,注意加个剪枝,就是如果答案大于了dp[n]就不用往后继续转移了

哎,dp思路还是很混乱,有空还要把这题好好做做。。。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 50005
#define M 15
#define mod 10000007
#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,k,s;
int a[N];
int b[N];
map<int,int>c;
int vis[N];
int dp[N];
int cou;
vector<int>save; void ini()
{
//memset(vis,0,sizeof(vis));
memset(dp,0x3f3f3f3f,sizeof(dp));
c.clear();
k=;
int i;
scanf("%d",&a[]);
k=;
b[]=a[];
for(i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]!=a[i-]){
k++;
b[k]=a[i];
}
}
s=;
for(i=;i<=k;i++){
if(c[ b[i] ]==){
// vis[ b[i] ]=1;
s++;
c[ b[i] ]=s;
}
} for(i=;i<=k;i++){
b[i]=c[ b[i] ];
// dp[i]=i;
}
// for(i=1;i<=k;i++){
// printf(" i=%d b=%d\n",i,b[i]);
//} } void solve()
{
int i,j;
dp[]=;
dp[k]=k;
for(i=;i<k;i++){
cou=;
// vis[ b[i] ]=1;
//save.push_back(b[i]);
for(j=i+;j<=k;j++){
// if(cou*cou>=k) break;
if(vis[ b[j] ]== ){
vis[ b[j] ]=;
save.push_back(b[j]);
cou++;
}
if (dp[i] + cou * cou >= dp[k]) break;
// printf(" i=%d j=%d dpj=%d cou=%d dp=%d ",i,j,dp[j],cou,dp[i]+cou*cou);
dp[j]=min(dp[j],dp[i]+cou*cou);
// printf(" dpj=%d\n",dp[j]);
}
for(vector<int>::iterator it=save.begin();it!=save.end();it++){
vis[*it]=;
}
save.clear();
}
} void out()
{
//for(int i=1;i<=k;i++){
// printf(" i=%d dp=%d\n",i,dp[i]);
//}
printf("%d\n",dp[k]);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
// while(T--)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
} return ;
}

HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化的更多相关文章

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

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

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

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

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

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

  4. hdu 5009 Paint Pearls

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

  5. hdu 5017 Ellipsoid(西安网络赛 1011)

    Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. hdu 4035 2011成都赛区网络赛E 概率dp ****

    太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...

  7. 异或运算(2014西安网络赛H题)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 题意:给出范围N,给出0-N的一个排列a.让你求出另外一个排列b,使 t = a1 ^ b1 + a ...

  8. hdu 4044 2011北京赛区网络赛E 树形dp ****

    专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...

  9. hdu 4050 2011北京赛区网络赛K 概率dp ***

    题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...

随机推荐

  1. 联想e431笔记本更改硬盘模式bios设置的详细教程

    用硬盘安装系统,就要进入bios,将硬盘改为第一启动项即可重装系统.不同品牌的电脑,它的bios设置方法也就不同.那么,联想e431笔记本要如何更改硬盘模式呢?今天U大侠小编就和大家分享联想e431笔 ...

  2. 使用memcached缓存 替代solr中的LRUCache缓存

    前沿 在搜索引擎中,缓存被当做是不可缺少的部分,但是很多情况下,将缓存的实现过度依赖于分发服务器及webserver会很大程度上加重webserver 的负担,具体表现就是经常性的假死,拒绝服务,因此 ...

  3. block总结

    3.编译器中的block 3.1 block的数据结构定义 我们通过大师文章中的一张图来说明: 上图这个结构是在栈中的结构,我们来看看对应的结构体定义: 1 2 3 4 5 6 7 8 9 10 11 ...

  4. ThinPHP5.0 目录结构

    官网文档  https://www.kancloud.cn/manual/thinkphp5/118008 project 应用部署目录├─application 应用目录(可设置)│ ├─commo ...

  5. selenium-浏览器操作方法

    前戏 浏览器都有哪些方法呢?最大化,设置浏览器窗口的大小,刷新,前进,后退等等,让我们来一一介绍 获取网站titie from selenium import webdriver from time ...

  6. 清理数据库事务——SQL语句

    清除流程内部的所有相关数据 eg1: declare @procedureTemp table ( [ProcedureCode] varchar(10) ) declare @ProcedureCo ...

  7. PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20)

    PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20)    http://www.patest.cn/contests/pat-b-practise/10 ...

  8. python mysql备份脚本

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: pyinnobackup.py #@time: 2018/12/11 1 ...

  9. 纯 CSS 创作一个小球绕着圆环盘旋的动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKxyWo 可交互视频 ...

  10. python-小数据池,再谈编码,is和 == 的区别

    一 . 小数据池 # 小数据池针对的是: int, str, bool 在py文件中几乎所有的字符串都会缓存. # id() 查看变量的内存地址 s = 'attila' print(id(s)) 二 ...