BZOJ.1032.[JSOI2007]祖码(区间DP)
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)的更多相关文章
- BZOJ 1032 [JSOI2007]祖码Zuma
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 637 Solved: 318[Submit][Stat ...
- BZOJ 1032 JSOI2007 祖码Zuma 动态规划
题目大意:给定一个祖玛序列,任选颜色射♂出珠子,问最少射♂出多少珠子 输入法近期越来越奇怪了0.0 首先我们把连续同样的珠子都缩在一起 令f[i][j]表示从i開始的j个珠子的最小消除次数 初值 f[ ...
- LG2145 「JSOI2007」祖码 区间DP
问题描述 LG2145 题解 把颜色相同的一段看做一个点. 然后类似于合唱队区间DP即可. 但是这题好像出过一些情况,导致我包括题解区所有人需要特判最后一个点. \(\mathrm{Code}\) # ...
- 1032: [JSOI2007]祖码Zuma
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1032 Description 这是一个流行在Jsoi的游戏,名称为祖玛.精致细腻的背景,外加神 ...
- [BZOJ1032][JSOI2007]祖码Zuma 区间dp
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1105 Solved: 576 [Submit][S ...
- bzoj千题计划120:bzoj1032[JSOI2007]祖码Zuma
http://www.lydsy.com/JudgeOnline/problem.php?id=1032 https://www.luogu.org/discuss/show?postid=8416 ...
- bzoj1032 [JSOI2007]祖码Zuma
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 672 Solved: 335[Submit][Stat ...
- Bzoj 1055: [HAOI2008]玩具取名 (区间DP)
Bzoj 1055: [HAOI2008]玩具取名 (区间DP) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间动态规划和可 ...
- [JSOI2007]祖码Zuma
题目描述 这是一个流行在Jsoi的游戏,名称为祖玛. 精致细腻的背景,外加神秘的印加音乐衬托,彷佛置身在古老的国度里面,进行一个神秘的游戏——这就是著名的祖玛游戏.祖玛游戏的主角是一只石青蛙,石青蛙会 ...
随机推荐
- Mysql注入root权限直接写一句话马
首先我们的找到一个有注入的站:这里我用自己搭建的环境表示:大家不要乱来 http://localhost/pentest/sql/sql_injection_get.php?id=1 发现是root权 ...
- python并发爬虫利器tomorrow(一)
tomorrow是我最近在用的一个爬虫利器,该模块属于第三方的一个模块,使用起来非常的方便,只需要用其中的threads方法作为装饰器去修饰一个普通的函数,既可以达到并发的效果,本篇将用实例来展示to ...
- Linux学习笔记-文件处理和权限命令
目录 文件处理命令 touch cat tac more less head tail 链接命令 ln 权限命令 chmod 权限管理命令 chown chgrp umask 文件处理命令 touch ...
- 在JAVA中记录日志的十个小建议
JAVA日志管理既是一门科学,又是一门艺术.科学的部分是指了解写日志的工具以及其API,而选择日志的格式,消息的格式,日志记录的内容,哪种消息对应于哪一种日志级别,则完全是基于经验.从过去的实践证明, ...
- tf.summary.merge_all()
1.自动管理模式 summary_writer = tf.summary.FileWriter('E:/data/tensorflow-master/1.Cnn_Captcha/result/', f ...
- C# LINQ语句
1.select 和 selectMany SelectMany() 将中间数组序列串联为一个最终结果值,其中包含每个中间数组中的每个值 2.join语句 from x in xx join d in ...
- js权威指南---学习笔记02
1.JS只有函数作用域,没有块级作用域这个概念: 它有一个特性——声明提前:在同一个函数中不同位置声明的变量,都被提前在函数开始的时候,执行声明操作:在原先位置执行赋值操作: 2.声明的全局变量,相当 ...
- 网络路径查询traceroute
Traceroute用法 网友:适兕 发布于: 2006.08.24 08:14 (共有条评论) 查看评论 | 我要评论 一.什么是Traceroute? In ...
- IntelliJ IDEA + Tomcat ;On Upate Action 与 On Frame Deactivation
On Upate Action 与 On Frame Deactivation 这两个选项的设置,依赖于 项目的部署方式 是war包 还是 exploded ,看下面的gif: 这里实在是太灵活了, ...
- 【TensorFlow】Python解析xml文件
最近在项目中使用TensorFlow训练目标检测模型,在制作自己的数据集时使用了labelimg软件对图片进行标注,产生了VOC格式的数据,但标注生成的xml文件标签值难免会产生个别错误造成程序无法跑 ...