区间DP

区间DP 入门 详解 P1880 [NOI1995]石子合并 要注意这是个环,是一个石子圈。 怕日后我自己又看不懂了就放个链接,还是很好理解的 时间复杂度O(8N3)O(8N^3)O(8N3) #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<... 阅读详情

   区间DP,每段区间的最优值都由几段更小区间的最优值得到。主要方法是先求小区间的DP,再求大区间的DP。大多数情况是三重循环。大概形式是下面这样,i是区间大小,j是区间起始位置,k从0到i找出dp[i][j+i]最优解。

for(i=1;i<N;i++)

{

   for(j=0;i+j<N;j++)

   {

        if(..) dp[i][j+i]=...

        for(k=0;k<i;k++)

        dp[i][j+i]=...max(min)(dp[j][j+i],dp[j][j+k]+dp[j+k+1][j+i])

   }

}

P - Brackets
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

Description

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • ifs is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • ifa and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of charactersa1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence ofs. That is, you wish to find the largest m such that for indicesi1, i2, …, im where 1 ≤i1 < i2 < … < im ≤ n, ai1ai2 …aim is a regular brackets sequence.

Given the initial sequence([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters(, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6


括号问题,刚开始以为

dp[j][j+i]=dp[j+1][j+i-1]+1;
之后就不用k循环了,事实上还是要的,比如 ()()() 这种情况。

代码:

#include<stdio.h>
#include<string.h>
char a[110];
int dp[110][110];
int max(int a,int b)
{
    return a>b?a:b;
}
int main()
{
    while(scanf("%s",a),strcmp(a,"end"))
    {
        int i,j,k;
        memset(dp,0,sizeof(dp));
        for(i=1; i<strlen(a); i++)
            for(j=0; j+i<strlen(a); j++)
            {
                if(a[j+i]==')'&&a[j]=='('||a[j+i]==']'&&a[j]=='[') dp[j][j+i]=dp[j+1][j+i-1]+1;
                for(k=0; k<i; k++) dp[j][i+j]=max(dp[j][i+j],dp[j][j+k]+dp[j+k+1][j+i]);
            }
        printf("%d\n",dp[0][strlen(a)-1]*2);
    }
    return 0;
}

Q - Multiplication Puzzle
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

Description

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000

If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

Input

The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.

Output

Output must contain a single integer - the minimal score.

Sample Input

6
10 1 50 50 20 5


这道题一开始不会做,其实是假设第k个是最后一个取,于是可以得到

dp[j][j+i]=min(dp[j][j+i],dp[j][j+k]+dp[j+k][j+i]+a[j]*a[j+k]*a[j+i])

代码:

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
int a[110];
int dp[110][110];
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        int i,j,k;
        memset(dp,INF,sizeof(dp));
        for(i=0; i<N; i++) scanf("%d",&a[i]);
        for(i=1; i<N; i++)
            for(j=0; j+i<N; j++)
            {
                if(i==1) dp[j][j+1]=0;
                else if(i==2) dp[j][j+i]=a[j]*a[j+1]*a[j+2];
                else for(k=1; k<i; k++) dp[j][j+i]=min(dp[j][j+i],dp[j][j+k]+dp[j+k][j+i]+a[j]*a[j+k]*a[j+i]);
            }
        printf("%d\n",dp[0][N-1]);
    }
    return 0;
}


R - You Are the One
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

Description

  The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D, because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?
 

Input

  The first line contains a single integer T, the number of test cases.  For each case, the first line is n (0 < n <= 100)
  The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)
 

Output

  For each test case, output the least summary of unhappiness .
 

Sample Input

    
2    5 1 2 3 4 5 5 5 4 3 2 2
 

Sample Output

  
Case #1: 20 Case #2: 24


题目意思是说可以调换顺序,但是先进的要后出,所以DP就应该考虑把队首元素换到后面的哪个地方k,在换到位置之前的区间DP加上它的值乘以k再加上后面的DP*(k+1),(因为后面的每个元素都往后移动了k+1个位置)这样找到最优解。不知道为什么我开始总想着从后往前,那样要满足先进后出的话整个队列的顺序都要反过来=。=不知道是怎么想的。。

代码:


#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
int a[110],c[110];
int dp[110][110];
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int N,T,Case=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int i,j,k;
        c[0]=0;
        memset(dp,INF,sizeof(dp));
        for(i=0; i<N; i++)
        {
            scanf("%d",&a[i]);
            c[i]=(i==0?a[i]:c[i-1]+a[i]);
            dp[i][i]=dp[i+1][i]=0;
        }
        for(i=1; i<N; i++)
            for(j=0; i+j<N; j++)
            {
                dp[j][j+i]=min(dp[j][j+i],dp[j+1][j+i]+c[j+i]-c[j]);
                for(k=1; k<=i; k++)
                {
                    dp[j][j+i]=min(dp[j][j+i],dp[j+1][j+k]+a[j]*k+dp[j+k+1][j+i]+(c[j+i]-c[j+k])*(k+1));
                }
            }
        printf("Case #%d: %d\n",++Case,dp[0][N-1]);
    }
    return 0;
}

T - String painter
Time Limit:2000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

Description

There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?
 

Input

Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.
 

Output

A single line contains one integer representing the answer.
 

Sample Input

    
zzzzzfzzzzz abcdefedcba abababababab cdcdcdcdcdcd
 

Sample Output

  
6 7


问的是可以把A的任意区间改成别的字母,最少改几次变成B。直接变不好弄,可以先算出空白串变成B最少要几次,再算A到B。dp[i][j]包括位置i和j,所以i==j的话dp是1。首先dp[i][j]=min(dp[i-1][j],dp[i][j-1]),我一开始写的是dp[i][j]=dp[i][j-1]+1,结果WA。然后如果dp[i+j]==dp[i+k],就有dp[j][j+i]=min(dp[j][j+i],dp[j][j+k-1]+dp[j+k][j+i-1])。空白串变B处理完了,接着就是A变B,又是区间DP。。用ans[i]存到i-1个位置的答案,于是先把ans[i]赋值dp[0][i],再尝试减小ans[i]。最后ans[N-1]就是答案。

 for(i=0; i<N; i++)
    {
        ans[i]=dp[0][i];
        if(a[i]==b[i]) ans[i]=i==0?0:ans[i-1];
        for(j=0; j<i; j++) ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
    }

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#define INF 0x3f3f3f3f
char a[110],b[110];
int dp[110][110];
int ans[110];
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    while(scanf("%s",a)!=EOF)
    {
        scanf("%s",b);
        int i,j,k,N=strlen(b);
        for(i=0; i<N; i++)
            for(j=0; j<=i; j++)
            {
                if(i==j)dp[i][j]=1;
                else dp[i][j]==0;
            }
        for(i=1; i<N; i++)
            for(j=0; j+i<N; j++)
            {
                dp[j][j+i]=min(dp[j][j+i-1],dp[j+1][j+i])+1;
                for(k=0; k<i; k++) if(b[j+k]==b[j+i])
                        dp[j][j+i]=min(dp[j][j+i],dp[j][j+k-1]+dp[j+k][j+i-1]);
            }
        for(i=0; i<N; i++)
        {
            ans[i]=dp[0][i];
            if(a[i]==b[i]) ans[i]=i==0?0:ans[i-1];
            for(j=0; j<i; j++) ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
        }
        printf("%d\n",ans[N-1]);
    }
    return 0;
}


  额。。其实有些DP很难想到,还是要多做才有经验。。。

DAC8568 Controller DAC8568 ControllerDAC8568 简介串行接口三级目录 DAC8568 简介 PIN Description Name Description LDAC‾\overline{LDAC}LDAC load DACs SYNC‾\overline{SYNC}SYNC 低电平有效,电平触发控制, 该输入为输入数据的帧同步信号. 当SYNC为低,使能DAC内输入移位寄存器,并在随后的所有时钟沿采样数据。 在第32个时钟之后,DAC更新输出。 若不到32个时钟SYNC被置高(即 阅读详情

相关推荐

ClearML中文文档实战:从安装到第一个实验的保姆级教程

本文提供ClearML从零开始的实战指南,涵盖核心组件理解、Python SDK安装、服务器配置及首个自动化实验的完整流程。通过改造PyTorch训练脚本的实例,详细演示了如何利用ClearML自动记录实验参数、指标与模型,实现机器学习实验的可复现性与高效管理,并简要介绍了进阶的自动化工作流。

weixin_29216049的博客 402

线性区间 dp 与环形区间 dp

区间dp就是在区间上进行动态规划,求解一段区间上的最优解;该问题的求解思路主要是通过合并小区间的最优解进而得出整个大区间上最优解的dp算法。(实际上还是通过子问题的解来求出原问题的解)

oscar1224的博客 2329

Surface Go 2 变身 Linux 开发本:保姆级 Ubuntu 20.04 双系统安装与驱动配置全攻略

本文详细介绍了如何将 Surface Go 2 变身为高效的 Linux 开发本,涵盖 Ubuntu 20.04 双系统安装、专用内核配置、驱动优化等全流程。通过 linux-surface 内核安装和硬件驱动优化,确保 Surface Go 2 的触控、笔输入、摄像头等功能完美运行,适合开发者打造便携生产力工具。

weixin_30596165的博客 621

区间DP详解

本文讲解了 区间DP 是什么,怎么转移,以及五道经典的区间DP例题和六道作业题,可快速带新手了解 区间DP 这一个属于动态规划的算法。

doooge_awa的博客 1199

详细解读文字识别工具———Tesseract-OCR

   在尝试破解12306验证码的时候,提前了解了一下应对各种验证码的解决方案,这里主要是学习到了面对文字图片验证码,例如亚马逊、豆瓣等平台的登录时会遇到的情况,效果还不错。 一、工具介绍 ​    Tesseract-OCR 是一款由HP实验室开发由Google维护的开源OCR(Optical Character Recognition...

别看我只是一只羊 2万+

工业相机TEC散热技术:从原理到高精度成像的实战解析

本文深入解析了工业相机TEC散热技术的核心原理与实战应用。通过阐述热电制冷器如何利用帕尔帖效应实现精准温控,有效抑制图像传感器暗电流噪声,从而保障高精度成像的稳定性。文章结合工业场景痛点,提供了从TEC选型、系统集成到控制逻辑的完整设计指南,并分享了不同应用场景下的实战案例,是提升工业视觉系统成像质量的关键技术解析。

weixin_29269441的博客 426

区间dp 总结&详解

区间 dp

seanli1008的博客 1442

Proteus仿真AT89C51驱动DHT11:温湿度数据采集与LCD1602显示的5个调试坑点总结

本文总结了在Proteus仿真环境下使用AT89C51驱动DHT11温湿度传感器并通过LCD1602显示的5个常见调试坑点,包括DHT11时序问题、LCD1602初始化错误、数据校验失败、仿真特有差异及系统稳定性问题,提供了详细的解决方案和优化代码示例,帮助开发者高效完成温湿度采集项目。

weixin_30335575的博客 364

区间 dp 介绍

区间 dp 就是在区间上进行动态规划,求解一段区间上的最优解。

doudpyj的博客 1035

[PCIe5.0] 5.3.1.4.2 D3Cold State

主电源(Vcc)完全断开,仅保留极少量辅助电源(Vaux)用于维持基本唤醒功能设备功能完全关闭,所有寄存器状态丢失(除非有特殊存储)需要完全重新初始化才能恢复工作比D3Hot状态功耗更低,但唤醒延迟更长。

系统固件工程师 463

芒果YOLOv5改进05:损失函数IoU 篇之Repulsion Loss:原创改进损失函数 Repulsion:解决目标遮挡场景下检测,为解决密集人群检测中遮挡设计的损失函数,打造高效检测器涨点

YOLOv5改进代码实践|结合Repulsion 解决目标遮挡场景下检测🚀

包括YOLOv5、YOLOv7、YOLOv8等模型改进 4577

区间DP总结

区间dp基础:石子合并; 环形区间dp:环形石子合并、能量项链、凸多边形的划分(数组高精度) 区间dp求方案:加分二叉树 二维区间dp:棋盘分割

weixin_68738523的博客 1226

[ vulhub漏洞复现篇 ] ActiveMQ反序列化漏洞复现(CVE-2015-5254)

Apache ActiveMQ是美国阿帕奇(Apache)软件基金会所研发的一套开源的消息中间件,它支持Java消息服务、集群、Spring Framework等。由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。Apache ActiveMQ 5.13.0之前5.x版本中存在安全漏洞,该漏洞源于程序没有限制可在代理中序列化的类。......

qq_51577576的博客 4458

区间dp问题

区间dp问题区间DP概念思路+模板例题1.石子合并直线版NOI1995 石子合并 参考: 区间DP题型总结 全网最最最最最详细的区间dp题目总结!! https://blog.csdn.net/noiau/article/details/72514812 区间DP概念 区间DP是线性DP的扩展,分阶段地划分问题,与阶段中元素出现的顺序和由前一阶段的哪些元素合并而来有很大的关系。 状态转移方程: dp[i][j]=min{dp[i][k]+dp[k+1][j]}+cost[i][j] 或者 dp[i][j]=

selfsongs 1307

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

算法:区间dp

区间dp就是在区间上进行动态规划,求解一段区间上的最优解;该问题的求解思路主要是通过合并小区间的最优解进而得出整个大区间上最优解的dp算法。

m0_74310050的博客 726

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

区间DP题型总结

一. 概念灌输 区间DP是线性DP的扩展,分阶段地划分问题,与阶段中元素出现的顺序和由前一阶段的哪些元素合并而来有很大的关系。 状态转移方程: 区间DP的特点: (1)合并:即将两个或多个部分进行整合,当然也可以反过来。 (2)特征:能将问题分解为能两两合并的形式。 (3)求解:将整个问题舍最优值,枚举合并点,将问题分解为左右两个部分,最后合并的两个部分的最优值得到原问题的最优值。 ...

fighting_yifeng的博客 1033

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

入门普通区间DP和环形区间DP

具体的更新方式为:f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j] + v[i] * v[k + 1] * v[j + 1]),其中 f[i][k] 表示区间 [i,k] 的最优解,f[k + 1][j] 表示区间 [k+1,j] 的最优解。例如f [i] [j]为区间[l,r]的最大价值,则f [i] [j] = max(f[i] [k] + f[k + 1] [r] + cost | l<=k < r)对于每一个区间,枚举所有可能出现的两个子区间的组合。

estiny_123123的博客 732

D4——区间DP

f[i][j]=第i堆到第j堆石子和并的最高分k]+f[k+1范围k>=i;k<j。

XuyuxuanAa的博客 946

区间 DP 详解

每次只能合并相邻的两堆,合并的代价为这两堆石子的质量之和,合并后与这两堆石子相邻的石子将和新堆相邻。试找出一种合理的方法,使总的代价最小,并输出最小代价。分割型,指把一个区间内的几项分开拆成一份一份的,再全部合起来就是当前答案,可以理解为合并型的另一种(合并型详见下面),它的时间复杂度一般为。合并型,一般指把这一个区间内的相邻两项合在一起,每次代价为这两项的和,求最小代价。呗),但这种类型很少见,我基本翻遍了全网才找到了这一道题,其余的基本都是第一种,所以各位同学终点记第一种就行,第二种做一个拓展。

cwplh的博客 1561

区间dp的总结

1、区间dp含义 区间dp 常用dp[i][j] 来表示,含义是在区间[i,j]之间最大的…或是否可以成功。这里提供以下三个题来熟悉区间dp。 2、经典区间dp问题,合并果子  现在设有n堆的果子,每堆果子根据果子数量的不同需要耗费不同的体力,现在想把这些果子有次序的合并为一堆,消耗的体力等于两堆果子的重量之和。现在求耗费的最小的体力数多少。 比如 5 8 9 3 5+8=13 消费13体力 13 9 3 9+3=12 消费12体力 13 12 13+12=25 消费25体力 总体力消耗为 13

weixin_43688483的博客 957

区间dp

一、定义 区间DP,顾名思义是在区间上DP,它的主要思想就是先在小区间进行DP得到最优解,然后再利用小区间的最优解合并求大区间的最优解。 二,思路 区间动态规划问题一般都是考虑,对于每段区间,他们的最优值都是由几段更小区间的最优值得到,是分治思想的一种应用,将一个区间问题不断划分为更小的区间直至一个元素组成的区间,枚举他们的组合 ,求合并后的最优值。 设F[i,j](1<=i<=...

邵光亮的博客 686
上一篇: 买东西这一类的DP
下一篇: 快速排序
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址