题目链接 BZOJ

洛谷

AC代码:

区间DP,f[i][j]表示消掉i~j需要的最少珠子数。

先把相邻的相同颜色的珠子合并起来。

枚举方法一样,处理一下端点可以碰撞消除的情况就行。

当然合并会出现问题,比如有多个同色珠子但是可以分配给两边分别匹配,比如:https://www.luogu.org/discuss/show/8416?page=1。

没办法 写不对。

注意颜色还可能是非正数。

  1. //1820kb 108ms
  2. #include <cstdio>
  3. #include <cctype>
  4. #include <cstring>
  5. #include <algorithm>
  6. #define gc() getchar()
  7. const int N=505;
  8. int n,f[N][N];
  9. struct Pair
  10. {
  11. int col,cnt;
  12. Pair() {}
  13. Pair(int c,int t):col(c),cnt(t) {}
  14. }A[N];
  15. inline int read()
  16. {
  17. int now=0,f=1;register char c=gc();
  18. for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
  19. for(;isdigit(c);now=now*10+c-'0',c=gc());
  20. return now*f;
  21. }
  22. int main()
  23. {
  24. n=read();
  25. for(int i=1; i<=n; ++i) A[i].col=read();
  26. // if(n==17&&A[1].col==0) {putchar('2'); return 0;}//洛谷某sxbk的数据。
  27. int cnt=1; A[1].cnt=1;
  28. for(int i=2; i<=n; ++i)
  29. if(A[i].col!=A[i-1].col) A[++cnt]=Pair(A[i].col,1);
  30. else ++A[cnt].cnt;
  31. n=cnt;
  32. memset(f,0x3f,sizeof f);
  33. for(int i=1; i<=n; ++i) f[i][i]=A[i].cnt>=2?1:2;
  34. for(int len=1; len<n; ++len)
  35. for(int i=1; i+len<=n; ++i)
  36. {
  37. int j=i+len;
  38. if(A[i].col==A[j].col)//消除后再碰撞消除 //长度怎么会是1,都合并了
  39. f[i][j]=f[i+1][j-1]+(A[i].cnt+A[j].cnt>=3?0:1);
  40. for(int k=i; k<j; ++k)
  41. f[i][j]=std::min(f[i][j],f[i][k]+f[k+1][j]);
  42. }
  43. printf("%d",f[1][n]);
  44. return 0;
  45. }/*
  46. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  47. 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 0
  48. */

不合并(瞎DP)代码(WA):

不合并同色的话,我只能过7个点了,但是某些数据能过。。比如:12 1 1 2 2 3 3 2 2 2 4 4 2 = 3.

  1. #include <cstdio>
  2. #include <cctype>
  3. #include <cstring>
  4. #include <algorithm>
  5. #define gc() getchar()
  6. const int N=505;
  7. int n,col[N],f[N][N];
  8. inline int read()
  9. {
  10. int now=0,f=1;register char c=gc();
  11. for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
  12. for(;isdigit(c);now=now*10+c-'0',c=gc());
  13. return now*f;
  14. }
  15. int main()
  16. {
  17. n=read();
  18. for(int i=1; i<=n; ++i) col[i]=read();
  19. memset(f,0x3f,sizeof f);
  20. for(int i=1; i<=n; ++i) f[i][i]=2;
  21. for(int i=2; i<=n; ++i)//处理i>j的f[i][j],方便下面特判时len=2的情况。
  22. for(int j=1; j<i; ++j) f[i][j]=1;
  23. for(int len=1; len<n; ++len)
  24. for(int i=1; i+len<=n; ++i)
  25. {
  26. int j=i+len;
  27. if(col[i]==col[j])//消除后再碰撞消除
  28. {
  29. if(len==1) f[i][j]=1;
  30. else if(col[i]==col[i+1] && col[j-1]==col[j]) f[i][j]=f[i+2][j-2];
  31. else if(col[i]==col[i+1]) f[i][j]=f[i+2][j-1];
  32. else if(col[j-1]==col[j]) f[i][j]=f[i+1][j-2];
  33. else f[i][j]=f[i+1][j-1]+1;
  34. }
  35. for(int k=i; k<j; ++k)
  36. f[i][j]=std::min(f[i][j],f[i][k]+f[k+1][j]);
  37. // printf("(%d,%d):%d\n",i,j,f[i][j]);
  38. }
  39. printf("%d",f[1][n]);
  40. return 0;
  41. }/*
  42. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  43. 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 0
  44. */

BZOJ.1032.[JSOI2007]祖码(区间DP)的更多相关文章

  1. BZOJ 1032 [JSOI2007]祖码Zuma

    1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 637  Solved: 318[Submit][Stat ...

  2. BZOJ 1032 JSOI2007 祖码Zuma 动态规划

    题目大意:给定一个祖玛序列,任选颜色射♂出珠子,问最少射♂出多少珠子 输入法近期越来越奇怪了0.0 首先我们把连续同样的珠子都缩在一起 令f[i][j]表示从i開始的j个珠子的最小消除次数 初值 f[ ...

  3. LG2145 「JSOI2007」祖码 区间DP

    问题描述 LG2145 题解 把颜色相同的一段看做一个点. 然后类似于合唱队区间DP即可. 但是这题好像出过一些情况,导致我包括题解区所有人需要特判最后一个点. \(\mathrm{Code}\) # ...

  4. 1032: [JSOI2007]祖码Zuma

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1032 Description 这是一个流行在Jsoi的游戏,名称为祖玛.精致细腻的背景,外加神 ...

  5. [BZOJ1032][JSOI2007]祖码Zuma 区间dp

    1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1105  Solved: 576 [Submit][S ...

  6. bzoj千题计划120:bzoj1032[JSOI2007]祖码Zuma

    http://www.lydsy.com/JudgeOnline/problem.php?id=1032 https://www.luogu.org/discuss/show?postid=8416 ...

  7. bzoj1032 [JSOI2007]祖码Zuma

    1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 672  Solved: 335[Submit][Stat ...

  8. Bzoj 1055: [HAOI2008]玩具取名 (区间DP)

    Bzoj 1055: [HAOI2008]玩具取名 (区间DP) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间动态规划和可 ...

  9. [JSOI2007]祖码Zuma

    题目描述 这是一个流行在Jsoi的游戏,名称为祖玛. 精致细腻的背景,外加神秘的印加音乐衬托,彷佛置身在古老的国度里面,进行一个神秘的游戏——这就是著名的祖玛游戏.祖玛游戏的主角是一只石青蛙,石青蛙会 ...

随机推荐

  1. SQL SERVER 视图优化经历

    系统中要求对HIS数据进行效益统计,因为HIS数据是需要第三方提供接口导入的,不清楚数据量大小,所以视图以业务为主未对其做性能优化(当时编写试图时就是几条简单的测试数据) 如今在项目接口实施完成后查看 ...

  2. 文件操作fstream

    c++文件操作详解 2009-04-16 20:46:35|  分类: C/C++|举报|字号 订阅 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ost ...

  3. java创建并配置多module的maven项目

    1 使用idea创建(推荐) 这篇博客写的特别好,很详细: https://blog.csdn.net/sinat_30160727/article/details/78109769 2 使用ecli ...

  4. Android Framebuffer介绍及使用【转】

    转自:https://www.jianshu.com/p/df1213e5a0ed 来自: Android技术特工队 作者: Aaron 主页: http://www.wxtlife.com/ 原文连 ...

  5. 如何用Percona XtraBackup进行MySQL从库的单表备份和恢复【转】

    前提 应该确定采用的是单表一个表空间,否则不支持单表的备份与恢复. 在配置文件里边的mysqld段加上 innodb_file_per_table = 1 环境说明: 主库:192.168.0.1 从 ...

  6. HDU 6057 Kanade's convolution

    题目链接:HDU-6057 题意: 思路:先按照官方题解推导出下面的式子: 现在唯一的问题就是怎么解决[bit(x)-bit(y)=bit(k)]的问题. 我们定义\( F(A,k)_{i}=\lef ...

  7. .net 下的集合

    集合的操作在编码的时候很常见.但是由于经常使用几种集合.而忽略了一些不常用的集合.在这里我整理下. 首先先了解下接口: 1.IEnumerable,返回一个循环访问集合的枚举器. 2.IEnumera ...

  8. Python爬取微信好友

    前言 今天看到一篇好玩的文章,可以实现微信的内容爬取和聊天机器人的制作,所以尝试着实现一遍,本文记录了实现过程和一些探索的内容 来源: 痴海 链接: https://mp.weixin.qq.com/ ...

  9. 牛客红包OI赛 C 小可爱表白

    据说是个公式题. Code #include<cstdio> #include<cstring> #include<algorithm> using namespa ...

  10. json字符串与java对象的相互转换(jackson)

    1.java对象转换为json字符串 package com.chichung.json; import com.fasterxml.jackson.core.JsonProcessingExcept ...