Educational Codeforces Round 61 (Rated for Div. 2)F(区间DP,思维,枚举)

#include<bits/stdc++.h>
typedef long long ll;
const int inf=0x3f3f3f3f;
using namespace std;
char b[507];
int dp[507][507];
int main(){
    memset(dp,0x3f,sizeof(dp));
    int n;
    scanf("%d",&n);
    scanf("%s",b+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(i==j)
                dp[i][j]=1;
            else
                dp[i][j]=inf;
    for(int len=1;len<=n;len++)//中间段长度
        for(int l=1,r;(r=l+len)<=n;l++)//枚举起点,枚举终点
            if(b[l]==b[r])
                if(len==1)
                    dp[l][r]=1;//初始为1
                else
                    dp[l][r]=min(min(dp[l+1][r],dp[l][r-1]),dp[l+1][r-1]+1);//更新最小值
            else
                for(int k=l;k<r;k++)
                    dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);//区间更新
    printf("%d",dp[1][n]);
}

//类似cf#538D

原文地址:https://www.cnblogs.com/ldudxy/p/10486816.html

时间: 2024-08-29 19:24:21

Educational Codeforces Round 61 (Rated for Div. 2)F(区间DP,思维,枚举)的相关文章

Educational Codeforces Round 59 (Rated for Div. 2) E 区间dp + 状态定义

https://codeforces.com/contest/1107/problem/E 题意 给出01字符串s(n<=100),相邻且相同的字符可以同时消去,一次性消去i个字符的分数是\(a[i]\),问消去s最多能得到多少分数 题解 实质是安排消去次序使得分数最大,第一步采取的行动是递归边界 因为只有01串,所以s被分成了一段一段,考虑段处理 预处理出一次性消去i个字符的最大分数\(f[i]\) 定义\(dp[l][r][cnt]\)为消去第l到第r段加上cnt个字符和第l段相同得到的最大

Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)

#include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];int mx[4000007];int sum[4000007];int head[1000007],to[2000007],nex[2000007];int n,k;int a[10000077];int dfn;int tot;void pushup(int rt){    mx[rt]=max(mx[rt

Educational Codeforces Round 81 (Rated for Div. 2)F(线段树)

预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ll long long 5 const int N=2e5+7; 6 struct Tree{ 7 ll minn,la

Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并

题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度复杂度O(nlogn) //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("

Educational Codeforces Round 80 (Rated for Div. 2)C(DP)

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 const long long mod = 1e9+7; 5 long long pre[1007][1007],temp[1007][1007]; 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(NULL); 9 cout.tie(NULL); 10 int n,m;

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.