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的游戏,名称为祖玛. 精致细腻的背景,外加神秘的印加音乐衬托,彷佛置身在古老的国度里面,进行一个神秘的游戏——这就是著名的祖玛游戏.祖玛游戏的主角是一只石青蛙,石青蛙会 ...
随机推荐
- C# IsAssignableFrom与IsSubClassOf 判断匿名类是否继承父类
public class Dog : Animal { public string name { get; set; } } public class Animal { public string i ...
- bootstrap_bootstrap中日历范围选择插件daterangepicker的使用
1.引入脚本 <link rel="stylesheet" type="text/css" href="assets/css/bootstrap ...
- 状压dp+floyed(C - Hie with the Pie POJ - 3311 )
题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...
- HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...
- Flask小demo---代码统计系统
功能要求: 管理员登录 # 第一天 班级管理 # 第一天 学生管理 # 第一天 学生登录 上传代码(zip文件和.py文件) 查看个人提交记录列表 highchar统计 学生列表上方使用柱状图展示现班 ...
- parseObject方法将json字符串转换成Map
String nwVal=recordDO.getWorkOrderNwVal(); HashMap<String,WxhcWorkOrderDO> nwMap=JSON.parseObj ...
- Android浮动窗口的实现
1.浮动窗口的实现原理 看到上图的那个小Android图标了吧,它不会被其他组建遮挡,也可以响应用户的点击和拖动事件,它的显示和消失由WindowManager直接管理,它就是Android浮动窗口. ...
- 数据库-mysql视图
视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用 一:创建视图 create view view ...
- dede图集内容页调用
{dede:productimagelist} <li> <img src="[field:imgsrc/]" width="92" heig ...
- javaweb作业二
作业:1.书写servlet的类架构及重要方法.(ServletConfig,Servlet)<---GenericServlet(getInitParameter(String str);in ...