Codeforces Round #336 (Div. 2) 608D Zuma(dfs+dp)

使用 pip 安装 pygame 遇到的问题 今天尝试学习使用 pygame 时,按照我的习惯一发 pip install 下去,本以为很简单的事情,没想到遇到了不少问题,顺手记录一下。 0x01. 无法找到 “SDL.h” 文件 pygame 是使用 SDL 库写就的游戏框架,自然强依赖于 SDL,我的 python@3 是使用 macOS 的 brew 管理器直接安装的,可能由于不是编译安装的问题,缺少了这个库,继续使用 brew 安... 阅读详情

D. Zuma
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

Print a single integer — the minimum number of seconds needed to destroy the entire line.

Sample test(s)
input
3
1 2 1
output
1
input
3
1 2 3
output
3
input
7
1 4 4 2 3 2 1
output
2
Note

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.



题目链接:点击打开链接

给出一个序列, 每次可以移走一个回文子串, 问最少移多少次.

dp数组记录l至r的回文数量, dfs递归更新dp数组, dp[0][n - 1]即为答案.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 505;
int n, a[MAXN], dp[MAXN][MAXN];
int Dfs(int l, int r)
{
	if(l >= r) return 1;
	if(dp[l][r]) return dp[l][r];
	int x = r - l + 1;
	if(a[l] == a[r]) x = min(x, Dfs(l + 1, r - 1));
	for(int i = l; i < r; ++i)
		x = min(x, Dfs(l, i) + Dfs(i + 1, r));
	return dp[l][r] = x;
}
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	printf("%d\n", Dfs(0, n - 1));
	return 0;
}


3D 韩系风格漫剧人设 | GPT-Image2 人设生成【附完整提示词】 宁姚同款清冷剑系美人,极致小巧鹅蛋脸,下颌线紧致纤细流畅,下巴小巧精致收窄,小脸衬得五官格外舒展,韩式水光肌,面部精细水光高光,鼻梁高光、鼻尖高光、面中苹果肌高光、唇珠高光,红棕色长卷发偏分大波浪,垂在一侧肩前,发量蓬松。发量丰厚蓬松有体积感,不贴头皮,几缕碎发自然垂落脸侧修饰脸型,长度过胸及背,发丝柔顺丝滑卷度自然,发尾自然垂落,身穿香槟色缎面挂脖长裙,挂脖设计无袖露肩露背,上半身围裹式荡领胸口垂褶,腰部堆褶收腰,修身直筒包臀裙长度到脚踝,缎面光泽感强垂坠感好,极简剪裁。纯白色背景,3D漫剧写实厚涂风。 阅读详情

相关推荐

影刀rpa的使用一--读取excel表格数据并循环出每一项

1、全部展示2、运行结果展示之后可以按照各自的需求对excel中取出的数据进行处理。

qq_69268437的博客 1万+

Codeforces 607B:Zuma DP

B. Zuma time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Genos recently installed the game Zuma on his phone

697

软件工程案例教程·韩万江【课后习题】

!!!!

weixin_57858263的博客 6428

CodeForces 607B Zuma

区间dp

SHOJYS的博客 338

Fluent温度超限调试:从能量方程到网格优化的实战指南

本文深入解析Fluent中温度超限问题的诊断与解决方法,从能量方程机制到网格优化策略,提供实战指南。通过分析温度与焓的换算关系,探讨典型场景如能量方程发散、局部网格质量差等,并给出五步调试法及高级命令应用,帮助工程师有效解决温度计算异常问题。

weixin_29045585的博客 537

C# 进程间 双内存共享 高帧率图像传输

演示了两个进程之间通过 双内存共享 的方式, 来实时处理图像,并以高帧率(40~60FPS)传输的基本示例。 进程间内存共享的优点:高性能、低延迟、资源效率高、简单可靠。 适用场景:实时图像处理、大数据传输、多进程协作、实时监控系统,等等。

qianqiansd 2000

区间dp--洛谷Zuma

能很快的挑选出这些有颜色的宝石中的一个回文的、连续的子串,并将这个子串移除。每当一个子串被删除后,剩余的宝石将连接在一起,形成一个新的行列。初始化中我们必须初始化长度为2和1的两种取法,并将所有非法状态初始化为正无穷,防止非法转移。仅存在两种转移方式,即当第一个字母是否与最后一个字母相等,很容易得出状态转移方程。这个游戏的目标是尽快的消灭一行中所有的宝石。你的任务是:求出把整个宝石串都移除的最短时间。输出一个整数,把这行珠子移除的最短时间。,表示宝石串的长度。个被空格分开的整数,第。

2301_76623326的博客 1749

深入解析Linux Suspend/Resume机制——从用户态到内核态的完整流程

本文深入解析Linux Suspend/Resume机制,从用户态触发到内核态执行的完整流程。详细介绍了三种挂起模式(Freeze、Standby、Suspend to RAM)的实现原理,包括系统调用入口、状态验证、设备挂起流程及平台相关操作。同时提供实战调试技巧,帮助开发者解决电源管理中的常见问题。

weixin_29313239的博客 448

「Codeforces」607B (区间dp)

题意:原题在这 Genos最近在他的手机上下载了祖玛游戏。在祖玛游戏里,存在n个一行的宝石,第i个宝石的颜色是Ci 。这个游戏的目标是尽快的消灭一行中所有的宝石。 在一秒钟,Genos能很快的挑选出这些有颜色的宝石中的一个回文的,连续的子串,并将这个子串移除。每当一个子串被删除后,剩余的宝石将连接在一起,形成一个新的行列。你的任务是:求出把整个宝石串都移除的最短时间。 让我们给你一个提...

arjxt6474的博客 257

【Apollo学习笔记】——规划模块TASK之SPEED_HEURISTIC_OPTIMIZER

在Apollo星火计划学习笔记——Apollo路径规划算法原理与实践与【Apollo学习笔记】——Planning模块讲到……Stage::Process的函数会依次调用task_list中的TASK,本文将会继续以LaneFollow为例依次介绍其中的TASK部分究竟做了哪些工作。由于个人能力所限,文章可能有纰漏的地方,还请批评斧正。在配置文件中,我们可以看到LaneFollow所需要执行的所有task。本文将继续介绍LaneFollow的第10个TASK——动态规划规划目标加速度尽可能小。

sinat_52032317的博客 3429

IP白名单校验

支持格式: 1、127.0.0.1 // 指定固定IP 2、127.0.0.1-127.0.0.128 // 可使用“-”来表示一个IP区间,支持跨IP段(一般不会出现这种情况,跨IP段则配置多个规则) 3、127.0.* // 2~4位IP段可...

2205

CF607B Zuma

题目描述 Genos recently installed the game Zuma on his phone. In Zuma there exists a line ofnngemstones, theii-th of which has colorc_{i}ci​. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Genos ...

wan1314mum的专栏 265

欺诈文本分类微调(一):基座模型选型

通义千问Qwen2具有0.5B、1.5B、7B、72B等一系列参数大小不等的模型,我们需要做的是从大到小依次测试每个模型的能力,找到满足自己需要的最小参数模型。依1.5B和1.5B-Instruct的主要区别:按照官方文档,前者是预训练模型,后者是经过指令微调的模型。

golfxiao的专栏 2657

Codeforces Round #336 (Div. 2) D. Zuma(区间dp)

D. Zuma time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Genos recently installed the game Zuma on his phone

u013268685的专栏(有一种幸福叫AC,有一种期待叫AK) 1150

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color c...

weixin_30256505的博客 190

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Zuma_Codeforces 607B

题目   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the li...

baidu_41907100的博客 329

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Codeforces Round #336 (Div. 1)-B. Zuma(区间dp)

原题链接 B. Zuma time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Genos recently installed the game Zuma on

L. 512

Codeforces_607B:Zuma(区间DP)

题目大意给定长度为n的一个序列ai,每次操作可以消除一个回文串,问至少需要几次操作可以将整个序列消除. 解法为区间dp,设f[i][j]表示消除第i至j项所需的最小操作次数,转移的话f[i][j]可以拆分成f[i][k]和f[k+1][j]两段,即相当于分成两段各自消除,而当a[i]==a[j]时,则f[i][j]除了前面的那种消法外,还可以看作在消除了第i+1至j-1个元素后顺带消除了外层的第

OMRailgun 547

CodeForces 608D Zuma(DP)

题意:给你一个串,你每次可以消去一个回文串,问你最少消去多少次,可以使得这个串清空 思路:记忆化搜索一下就可以了 #include using namespace std; #define maxn 805 int dp[maxn][maxn]; int vis[maxn][maxn]; int a[maxn]; int n; int dfs(int l,int r) { i

围巾的ACM博客 948

区间 dp

1、Codeforces 607B Zuma 解题思路: dp[i][j] 表示消去区间 [i, j] 需要的时间 然后就是记忆化搜索了 #include #include #include #include #include #include #include #include #include #include #include #include #in

追风蹑影 371

区间dp——Zuma

也就是本道题原问题为在区间1-n中删除回文数的最小花费现今转化为在区间i-j删除回文数的最小花费。对于本道题,当w[i]==w[j]区间往内缩,但对于区间dp,我们往往要预处理,对于本道题,我们要对长度为1的和长度为2的进行预处理,为什么要对长度为2预处理(一般的都只是对长度为1的预处理)?因为本道题要求的是对回文串删除,又由于回文串有奇数又有偶数的,所以要对长度为1和2进行预处理。我认为,其实这种区间删数问题就是一种类似区间合并操作的。因此当枚举到的子集合是回文串的情况,区间得往内缩。

m0_60738889的博客 1094

【Codeforces 607 B. Zuma】

CF607B 容易看出是区间dp dp[l][r]代表 l 到 r 长度的最小代价 就是需要特判长度为 2 不然如果arr[l] == arr[r] dp[l][r] 可以直接由dp[l+1][r-1]转移过来 然后再枚举左右两部分枚举最小就行 /* if you can't see the repay Why not just work step by step rubb...

我是谁 --- 卡比兽 262

Zuma CodeForces - 608D(记忆化搜索)

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color c i. The goal of the game is to destroy all the gemstones in the line as qu...

starlet_kiss的博客 581

codeforces 607B(记忆化dp,区间dp)

链接: http://codeforces.com/problemset/problem/607/B 题意: 现在给你一个序列,你可以拿走一个连续的子串,如果这个子串是回文串,花费为1, 剩下的接起来,直到拿完,求最小花费。 思路: 区间dp呀。 用记忆化更好想一点。dp[ l ][ r ] 表示l到r 的最小花费。那么边界条件? 如果当前的l r 是一个回文串,那么就可以直接返回了,花费为1...

yjt9299的博客 323

Codeforces #337(Div.2)D. Zuma【记忆化搜索】

D. Zuma time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Genos recently installed the game Zuma on his phone. In Zuma

mengxiang000000 673

Codeforces Round #336 (Div. 2)D. Zuma【区间dp】

D. Zuma time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Genos recently installed the game Zuma on his phone

走一步再走一步 565
上一篇: Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)
下一篇: Codeforces Round #336 (Div. 2) 608B Hamming Distance Sum(dp)
GKHack
博客等级 码龄12年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值