题目链接 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. 信息安全学习笔记--CSRF

      一.CSRF简介   CSRF(Cross-site request forgery)跨站请求伪造,也被称为“one click attack”或者“session riding”,通常缩写为CS ...

  2. [转]std::set、自定义类型与比较函数

    转自:http://www.189works.com/article-42025-1.html 怎样在set中放入自定义类型?这个问题通过谷歌就可以得到不少答案:1.定义一个函数对象并在定义set的时 ...

  3. 推荐一本springBoot学习书籍---深入浅出springBoot2.x

    花了几周时间读完了这本书,确实是一本特别详细全面的书,而且不单单只是springBoot, 书中还介绍了许多工作中常用的技术与springBoot的整合使用,当然,也有一些小bug, 因为在代码实践过 ...

  4. go 函数介绍

    1. 定义:有输入.有输出,用来执行一个指定任务的代码块 func functionname([parametername type]) [returntype] { //function body ...

  5. linux中core dump开启使用教程【转】

    转自:http://www.111cn.net/sys/linux/67291.htm 一.什么是coredump 我们经常听到大家说到程序core掉了,需要定位解决,这里说的大部分是指对应程序由于各 ...

  6. 使用pandas把mysql的数据导入MongoDB。

    使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...

  7. 【hihocoder1251】Today is a rainy day

    #include<bits/stdc++.h> ; ; const int inf=0x3f3f3f3f; using namespace std; char s1[N],s2[N]; ] ...

  8. window.print打印方法实现

    vue中使用window.print打印效果 项目要求 打印每页有10行表格,如果接口数据没有十个显示10行 效果图 第一页 第二页 子组件 <template> <div> ...

  9. 可怕的线程上下文类装载器(TCCL)

    在明天的 OSGi 2012 社区活动上,我将以"如何使你的类库在不依赖 OSGi 的情况下进行友好地 OSGi"为主题进行演讲.在演讲中我将会提及 Java 的线程上下文类加载器 ...

  10. SQL语句添加删除修改字段

    用SQL语句添加删除修改字段1.增加字段     alter table docdsp    add dspcodechar(200)2.删除字段     ALTER TABLE table_NAME ...