转自: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思路还是很混乱,有空还要把这题好好做做。。。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9. #include<string>
  10.  
  11. #define N 50005
  12. #define M 15
  13. #define mod 10000007
  14. #define p 10000007
  15. #define mod2 100000000
  16. #define ll long long
  17. #define LL long long
  18. #define maxi(a,b) (a)>(b)? (a) : (b)
  19. #define mini(a,b) (a)<(b)? (a) : (b)
  20.  
  21. using namespace std;
  22.  
  23. int n,k,s;
  24. int a[N];
  25. int b[N];
  26. map<int,int>c;
  27. int vis[N];
  28. int dp[N];
  29. int cou;
  30. vector<int>save;
  31.  
  32. void ini()
  33. {
  34. //memset(vis,0,sizeof(vis));
  35. memset(dp,0x3f3f3f3f,sizeof(dp));
  36. c.clear();
  37. k=;
  38. int i;
  39. scanf("%d",&a[]);
  40. k=;
  41. b[]=a[];
  42. for(i=;i<=n;i++){
  43. scanf("%d",&a[i]);
  44. if(a[i]!=a[i-]){
  45. k++;
  46. b[k]=a[i];
  47. }
  48. }
  49. s=;
  50. for(i=;i<=k;i++){
  51. if(c[ b[i] ]==){
  52. // vis[ b[i] ]=1;
  53. s++;
  54. c[ b[i] ]=s;
  55. }
  56. }
  57.  
  58. for(i=;i<=k;i++){
  59. b[i]=c[ b[i] ];
  60. // dp[i]=i;
  61. }
  62. // for(i=1;i<=k;i++){
  63. // printf(" i=%d b=%d\n",i,b[i]);
  64. //}
  65.  
  66. }
  67.  
  68. void solve()
  69. {
  70. int i,j;
  71. dp[]=;
  72. dp[k]=k;
  73. for(i=;i<k;i++){
  74. cou=;
  75. // vis[ b[i] ]=1;
  76. //save.push_back(b[i]);
  77. for(j=i+;j<=k;j++){
  78. // if(cou*cou>=k) break;
  79. if(vis[ b[j] ]== ){
  80. vis[ b[j] ]=;
  81. save.push_back(b[j]);
  82. cou++;
  83. }
  84. if (dp[i] + cou * cou >= dp[k]) break;
  85. // printf(" i=%d j=%d dpj=%d cou=%d dp=%d ",i,j,dp[j],cou,dp[i]+cou*cou);
  86. dp[j]=min(dp[j],dp[i]+cou*cou);
  87. // printf(" dpj=%d\n",dp[j]);
  88. }
  89. for(vector<int>::iterator it=save.begin();it!=save.end();it++){
  90. vis[*it]=;
  91. }
  92. save.clear();
  93. }
  94. }
  95.  
  96. void out()
  97. {
  98. //for(int i=1;i<=k;i++){
  99. // printf(" i=%d dp=%d\n",i,dp[i]);
  100. //}
  101. printf("%d\n",dp[k]);
  102. }
  103.  
  104. int main()
  105. {
  106. //freopen("data.in","r",stdin);
  107. //freopen("data.out","w",stdout);
  108. //scanf("%d",&T);
  109. //for(int cnt=1;cnt<=T;cnt++)
  110. // while(T--)
  111. while(scanf("%d",&n)!=EOF)
  112. {
  113. ini();
  114. solve();
  115. out();
  116. }
  117.  
  118. return ;
  119. }

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. HDOJ1195 双向BFS //单向也可以过 没想清

    #include<cstdio> #include<map> #include<vector> #include<stack> #include< ...

  2. Java IO file文件的写入和读取及下载

    一.FileWriter 和BufferedWriter 结合写入文件 FileWriter是字符流写入字符到文件.默认情况下,它会使用新的内容代替文件原有的所有内容,但是,当指定一个true值作为F ...

  3. vue 获取汉字的全拼、简拼、首拼

    1.封装公共方法,获取汉字的全拼.简拼.首拼 export const Pinyin = { _JMcode:{ "-":"", "—":& ...

  4. 企业自颁布服务器证书的有效性验证(C#为例)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/notjusttech/article/details/72779904 目前根据项目的需要,整理了一 ...

  5. Bootstrap历练实例:禁用的按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  6. qemu-img管理虚拟机

    qemu-img管理虚拟机 1. 查看正在运行的虚拟机 [root@idca-vm02 ~]# virsh list Id    名称                         状态 ----- ...

  7. js事件(事件冒泡与事件捕获)

    事件冒泡和事件捕获分别由微软和网景公司提出,这两个概念都是为了解决页面中事件流(事件发生顺序)的问题. <div id='aa' click='po'> <p id='bb' cli ...

  8. 洛谷 P2032 扫描

    https://www.luogu.org/problemnew/show/P2032 为啥不用STL,多方便. 定义一个大根堆,里边放一对数,这个数的大小和位置. 我们对于每次查询,判断首元素的位置 ...

  9. C语言实现链表及其操作

    #include <stdio.h> #include <stdlib.h> //定义节点 typedef struct Node { int data; struct Nod ...

  10. perl学习之:匹配修饰符/s /m

    m 是将字符串作为多行处理,s是将字符串作为单行处理,如果是s在字符串中出现的\n就相当于普通字符. 6.6. Matching Within Multiple Lines6.6.1. Problem ...