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

Codeforces 626 D. Jerry’s Protest(概率DP) codeforces每日一练。 题意: 有n张卡片,卡片上的数字就是分数,比如说甲乙两人抽卡,三局两胜,一局得分高的胜,求在甲赢了两局的情况下乙赢了第三局且总分比甲高的概率。 思路: 数据1e3,很明显的On^2算法,所以考虑暴力。 把分数数组排序,然后用cnt每轮可能出现的情况个数,cnt1统计分数差出现次数,cnt2统计甲前两局领先乙的总分方案数。 很明显,排序后的a[n]为最大得分,所以可以遍历1~a[n],两层循环统计甲前两局领先乙的总分方案数,cnt2[i+j]=cnt1[i]*cnt1[j] 。 最后也是两层循环,枚举分差,每对可能出现的分差对答案的贡献为: cnt1[i] * c 立即下载



链接:戳这里


D. Zuma
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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.

Examples
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.


题意:

给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去

问最少需要多少次操作可以消除所有的宝石


思路:

很明显是要dp,而且n=500

那么设置状态的时候dp[i][j] 表示区间[i,j]的最优值

那么怎么转移呢?直接看代码吧!


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int dp[550][550]; /// 区间[i,j]的最小值
int a[550],n;
int main(){
    mst(dp,127);
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    for(int i=0;i<n;i++) dp[i][i]=1; /// 初始化 本身到本身为1
    for(int i=1;i<n;i++){ /// 枚举当前区间的长度
        for(int j=0;j+i<n;j++){ /// 当前区间[j,j+i]的最小值
            if(i==1){ /// 当前长度为1的时候直接判断 后面的区间里面至少长度>2
                if(a[j]==a[j+i]) dp[j][j+i]=1;
                else dp[j][j+i]=2;
            } else {
                if(a[j]==a[j+i]) dp[j][j+i]=dp[j+1][j+i-1]; ///首尾相等的话转移到[j+1,j+i-1]区间上去取最优值
                for(int k=j;k<j+i;k++){
                    dp[j][j+i]=min(dp[j][j+i],dp[j][k]+dp[k+1][i+j]);
                    /// 枚举这个k,有最优的值就更新区间最优值
                }
            }
        }
    }
    printf("%d\n",dp[0][n-1]);
    return 0;
}



CodeForces – 1312E Array Shrinking(区间dp) 题目链接:点击查看 题目大意:给出 n 个数,现在可执行的操作是: 找到相邻且数值相等的两个数,即 abs( i – j ) == 1 && a[ i ] == a[ j ] 使得两个数合并为一个数,且数值变为 a[ i ] + 1 问如何操作,能使得最终数列的长度最短 题目分析:最优性问题,且是对区间操作的,而且数据范围满足 n^3 的时间复杂度,综上可以考虑区间dp,因为题目已经明确了需要求什么,所以我们不妨设 dp[ i ][ j ] 为区间 [ i , j ] 合并后的最短数列的长度,因为题目中的合并放到数组中不太好实现,我们等加成区间赋值就可以了,如果遇到需要将区间 [ i , k 立即下载

相关推荐

Zlib1.2.11源码编译(Win10+VS2019)

下载 下载网址: zlib Home Site 得到文件: 解压缩到“D:\SourceCodes\zlib-1.2.11” 使用CMake配置、生成并打开项目 将CMakeLists.txt拖入CMake,配置(Configure): 生成(Generate) 打开项目(Open Project),CMake调用VS2019打开项目 生成debug版本 构建项目ALL_BUILD。成功生成debug版本 在“D:\Sourc...

Alexabc3000的专栏 1900

codeforces-149D(区间dp + 递归)

题目: Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it. You are given string s. It represents a correct bracket sequence. ...

偷吃了老鼠的土豆 471

关于RT-Thread中PWM无法正常输出的学习

按照官方的PWM配置,并没有输出PWM,网上有比较多的办法,试了很多,有些还是不能实现输出,这里做个记录。记录自己完成PWM输出的过程,以及修改的地方。 关于STM32CubeMX配置就不多说了,网上有很多。 首先必定是按照流程来,一定一定按照流程来!!! 需要注意的是加上通道数!!!! 这是流程上没有的,也是RTT的一点小BUG吧 配上board.c中添加的代码,我用的是PA2定时2的通道3,需要使用的朋友修改对应定时器和GPIO! void HAL_TIM_Base_MspInit(TIM_Han

weixin_42566346的博客 3558

李超线段树(Li-Chao Segment Tree)

李超线段树 李超线段树是一种用于维护平面直角坐标系内线段关系的数据结构。它常被用来处理这样一种形式的问题:给定一个平面直角坐标系,支持动态插入一条线段,询问从某一个位置 (X,+∞)(X,+\infty)(X,+∞) 向下看能看到的最高的一条线段(也就是给一条竖线,问这条竖线与所有线段的最高的交点 如上图,有三条线段,两条红色竖线代表两个询问,则点 AAA 与点 BBB 就是询问到的答案。 李超线段树的核心是维护每个区间的“最优势线段”,即在每个区间的中点处最高的线段。询问时我们可以对所有包含横坐标为

weixin_45630722的博客 1109

setoolkit克隆网站获取用户信息

一、环境 kali、DVWA 二、步骤 首先打开kali,输入setoolkit 依次选择选项1-选项2-选项3-选项2 输入钓鱼网站的网址(默认为本地网址) 输入要克隆的网址URL 这里我的网址如图,将网址复制 然后模拟攻击场景,攻击者向受害者发送了一条钓鱼链接,此链接与DVWA的登陆页面完全一样,受害者点击之后并进行登陆信息 点击login之后,用户信息就被setool...

BeatRex的博客 3004

STM32 IIC电量计LTC2944

LTC2944是一款电池电量计,设计用于端电压为3.6V至60V的多节电池。它测量电池充电和放电、电池电压、电流和自身温度。精密模拟库仑计数器通过电池正极端子与负载或充电器之间的检测电阻器对电流进行积分。电池电压、电池电流和硅温度由内部ADC测量。

baidu_34971492的博客 3171

CodeForces - Antenna Coverage(区间DP)

题意: 给一段长度为m的线段,线段上有n个路灯, 每个路灯在位置xix_ixi​且能照亮范围[xi−si,xi+si][x_i-s_i,x_i+s_i][xi​−si​,xi​+si​] 我们可以花费一个硬币使得某个路灯的sis_isi​的增大1,可以操作任意多次 求能使区间[1,m]被路灯照亮的最小硬币花费 思路: dp[i]表示线段[1,i][1,i][1,i]被覆盖所需的最小代价 第一层循环dp从1到m,第二层循环遍历n个路灯,维护dp 时间复杂度O(n*m) 状态转移分析: x[j]<=.

不爱吃鱼的猫丶的博客 614

FPGA实现串口的任意字节数接收

基于FPGA的任意字节数(单字节、多字节)的串口(UART)接收,真的不来看看吗?

孤独的单刀 1万+

codeforces 1025D (区间dp)

                                                                                     Recovering BST                                                                          Time limit per test   1 se...

nka_kun的博客 803

微信小程序 - 真机调试请求异常 Provisional headers are shown,微信小程序开发者工具调试没问题就真机测试时出现请求报错(微信小程序请求接口提示Provisional h)

微信小程序开发 Request Headers: Provisional headers are shown,微信开发工具请求正常,真机调试和上线之后出现 Provisional headers are shown,微信小程序开发工具调试没问题,真机调试Provisional headers are shown,在开发工具调试,请求正常没问题,使用真机调试,请求异常,微信小程序真机调试时后台是本地服务器,显示“Provisional headers are shown”问题解决,微信小程序websocket

🏆 前端领域优质创作者 1万+

【100%通过率】华为OD机试真题 JavaScript 实现【对称美学】【2023 Q1 | 100分】

对称就是最大的美学,现有一道关于对称字符串的美学。 已知: 第1个字符串:R 第2个字符串:BR 第3个字符串:RBBR 第4个字符串:BRRBRBBR 第5个字符串:RBBRBRRBBRRBRBBR相信你已经发现规律了,没错! 就是第i个字符串=第i-1号字符串的取反+第i-1号字符串。取反即(R->B,B->R); 现在告诉你n和k,让你求得第n个字符串的第k个字符是多少。(k的编号从0开始)

MISAYAONE的博客 9855

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

华为OD机试真题 新系统 【社交网络相同爱好好友查询】

华为od机试真题 新系统【社交网络相同爱好好友查询】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为od机试真题【社交网络相同爱好好友查询】多语言题解实现。

qq_45776114的博客 1348

pscad IEEE标准系统搭建说明

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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

信仰.的博客 373

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】...

A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Saitama accidentally destroyed a...

weixin_30349597的博客 146

Codeforces Round #336 div2

Codeforces Round #336 div2 通过数:3(1题未来得及交) Standing: 418 题目链接: http://codeforces.com/contest/608 A: 到一个楼层的时候,如果有人,则更新ans = Max(ans, 此人等待时间)#include <cstdio> #include <cstring> #include <cma

beihai2013 387

Codeforces Round #336 (Div. 2) D. Zuma

D - Zuma 题意:给你一个数字组成的串, 每次你能消去一段连续的回文串,问你最少需要操作几次把所有数字删完。 思路:区间dp, dp[ i ][ j ]表示删 i 到 j 段最少需要几次。 我们只考虑最左边的那个点的删除情况。 首先dp[ i ] [ j ] <= dp[ i + 1] [ j ] + 1 然后我们枚举 i 到 j 里面的点, 找到a[ k ] ...

185

Codeforces Round #336 (Div. 2) D. Zuma

Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串;每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因为只需要考虑一边),当考虑最左边的数时,有多少中消去方法?每种消去方法对结果的贡献又是多少?同时结果的区间又是怎么变化?这就是dp式子; 1.当单独消去这个元素时,dp[l][r] = 1 + dp[l

hxer 390

专题---区间DP

专题—区间DP 区间dp做题套路: 预处理:长度为1的区间 例题1、Codeforces Round #336 (Div. 2) D. Zuma 题目链接:http://www.codeforces.com/contest/608/problem/D AC代码: #include<iostream> #include<cstdio> #include<set>...

wangkr ‘s blog 163

Codeforces Round #336 (Div. 2)

A. Saitama Destroys Hotel 【题意】一楼梯从最高层开始依次下降,每下一层花1s,如果该层有人,则等乘客到达后载客继续下降,乘客进电梯的时间忽略不计,给定有乘客的楼层数及乘客到达时间,求承载所有乘客下至第0层所花费的最小时间。 【代码】 #include<iostream> using namespace std; int ...

An55511的博客 156

【题解】codeforces Zuma Round #336 D.【区间DP】

如果 w[l] == w[r] 那么位置l 和 r可以在删除l,r之间的数的时候顺便删了,就可以得出f[l][r] = f[l + 1][r - 1]我们枚举区间中的一个位置k 就可以用f[l][r] = min{f[l][k] + f[k + 1][r]}就可以表示所有的转移,分析:看数据范围可以知道可以用区间DP。首先不妨设f[1][n]表示1-n个数删除的最小次数,那我。题意:给n个数,每次可以删除一个回文串,问最少删除的次数;

wdnxmd的博客 187

Codeforces Round #336 (Div. 2) 608D Zuma(dfs+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

GKHack's Blog 1179
上一篇: IX Samara Regional Intercollegiate Programming Contest F 三分
下一篇: Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A 模拟
CrossDolphin
博客等级 码龄12年 62粉丝 328原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值