题目链接 BZOJ

洛谷

AC代码:

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

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

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

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

没办法 写不对。

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

//1820kb	108ms
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=505; int n,f[N][N];
struct Pair
{
int col,cnt;
Pair() {}
Pair(int c,int t):col(c),cnt(t) {}
}A[N]; inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
} int main()
{
n=read();
for(int i=1; i<=n; ++i) A[i].col=read();
// if(n==17&&A[1].col==0) {putchar('2'); return 0;}//洛谷某sxbk的数据。
int cnt=1; A[1].cnt=1;
for(int i=2; i<=n; ++i)
if(A[i].col!=A[i-1].col) A[++cnt]=Pair(A[i].col,1);
else ++A[cnt].cnt;
n=cnt;
memset(f,0x3f,sizeof f);
for(int i=1; i<=n; ++i) f[i][i]=A[i].cnt>=2?1:2;
for(int len=1; len<n; ++len)
for(int i=1; i+len<=n; ++i)
{
int j=i+len;
if(A[i].col==A[j].col)//消除后再碰撞消除 //长度怎么会是1,都合并了
f[i][j]=f[i+1][j-1]+(A[i].cnt+A[j].cnt>=3?0:1);
for(int k=i; k<j; ++k)
f[i][j]=std::min(f[i][j],f[i][k]+f[k+1][j]);
}
printf("%d",f[1][n]); return 0;
}/*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 0
*/

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

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

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=505; int n,col[N],f[N][N]; inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
} int main()
{
n=read();
for(int i=1; i<=n; ++i) col[i]=read();
memset(f,0x3f,sizeof f);
for(int i=1; i<=n; ++i) f[i][i]=2;
for(int i=2; i<=n; ++i)//处理i>j的f[i][j],方便下面特判时len=2的情况。
for(int j=1; j<i; ++j) f[i][j]=1;
for(int len=1; len<n; ++len)
for(int i=1; i+len<=n; ++i)
{
int j=i+len;
if(col[i]==col[j])//消除后再碰撞消除
{
if(len==1) f[i][j]=1;
else if(col[i]==col[i+1] && col[j-1]==col[j]) f[i][j]=f[i+2][j-2];
else if(col[i]==col[i+1]) f[i][j]=f[i+2][j-1];
else if(col[j-1]==col[j]) f[i][j]=f[i+1][j-2];
else f[i][j]=f[i+1][j-1]+1;
}
for(int k=i; k<j; ++k)
f[i][j]=std::min(f[i][j],f[i][k]+f[k+1][j]);
// printf("(%d,%d):%d\n",i,j,f[i][j]);
}
printf("%d",f[1][n]); return 0;
}/*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 0
*/

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. 2016.07.13-map的使用(以leetcode1-Two Sum为例)

    map的使用 1.unordered_map和map的区别 2.如何用 3.for (int a : nums1) 4.to_string() 5.map的应用 1.unordered_map和map ...

  2. vue写出放大镜的效果

    用vue写出放大镜查看图片的效果. 安装 npm install vue2.0-zoom 引入 import imgZoom from 'vue2.0-zoom' 组件 components: { i ...

  3. 【codeforces】【比赛题解】#960 CF Round #474 (Div. 1 + Div. 2, combined)

    终于打了一场CF,不知道为什么我会去打00:05的CF比赛…… 不管怎么样,这次打的很好!拿到了Div. 2选手中的第一名,成功上紫! 以后还要再接再厉! [A]Check the string 题意 ...

  4. JUnit基本介绍

    一.什么是单元测试 单元测试(Unit  Testing)是指在计算机编程中,针对程序模块来进行正确性检验的测试工作.单元测试的特点如下: ※ 程序单元是应用最小的可测试部件,通常采用基于类或者类的方 ...

  5. idea git revert 究竟做了啥

    git里面实现撤销commit 这个据我目前所知,有至少4个途径可以做到 1.git reset 2.git revert 3.git rm –cached 4.git checkout 这个可以参考 ...

  6. maven2 up to maven3的'version' contains an expression but should be a constant

    在Maven2时,为了保障版本一致,一般之前我们的做法时: Parent Pom中 <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  7. 使用IDEA进行打包

    使用IDEA打jar包: 1.

  8. python3 安装

    Centos7 安装python3 #安装sqlite-devel yum -y install sqlite-devel #安装依赖 yum -y install make zlib zlib-de ...

  9. MySQL缓存命中率概述及如何提高缓存命中率

    MySQL缓存命中率概述 工作原理: 查询缓存的工作原理,基本上可以概括为: 缓存SELECT操作或预处理查询(注释:5.1.17开始支持)的结果集和SQL语句: 新的SELECT语句或预处理查询语句 ...

  10. epoll测试实例

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...