CodeForces-607B:Zuma (基础区间DP)】的更多相关文章

题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除. 自然想到dp[i][j]表示序列i...j全部消除的最少操作数 有几种消除的方式都能通过枚举k(i<=k<j)从min(dp[i][k],dp[k+1][j])转移 还有一种先消除中间的,剩余两部分组成回文串再消除,这种消除方式转移不会..想到的时间复杂度太高.. 看了tourist的代码,发现神的转移好简洁,这种方式就是从dp[i+1][j-1](c[i]=c[j])转移的 应该可以这么理解,如果c[i…
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…
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情况,不上色,上红色,上蓝色. 每对括号必须只能给其中的一个上色,且必须给一个上色 相邻的两个不能上同色,可以都不上色 求满足条件的括号序列染色的方法数 假设不染色为0,另外两种色为1,2 那对于一个匹配的括号对来说,只允许(1,0),(2,0),(0,1),(0,2) 定义\(f[l][r][i][…
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…
题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i|=1\)). 容易想到区间DP.\(f[i][j]\)表示把区间\([i,j]\)全部删除的最大收益,还需要\(g[i][j]\)表示将区间\([i,j]\)删成连续上升的一段(\(a_i\sim a_j\))的最大收益,\(h[i][j]\)表示将区间\([i,j]\)删成连续下降的一段(\(a_…
LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2]⊕b[3],-,b[m−1]⊕b[m])\) 然后q次询问每次问一个区间的子区间里面最大的连续段的f函数值是多少 思路 首先考虑怎么快速计算f的函数值 因为我们发现一个\([l,r]\)的数组,考虑他最后状态的前一个状态 这个时候只有两个元素,而这两个元素实际上就是\([l,r-1]\)和\([l…
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has…
题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例如:[1, 2, 2, 3, 2]需要2步:[1, 2, 2, 3, 2] -> [1, 2, 2, 2, 2] -> [2, 2, 2, 2, 2]. 思路:我们先把颜色块压缩(即把本来颜色相同并且相邻的颜色块合并).容易发现,如果出现了形如[1, 2, 3, 1]这种情况, 那么显然先合并两个…
转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内,然后让你求最小值 基本思路: 记sum[i]表示0 - i 的和对 p 取模的值. 1.如果k * p > n,那么与C2的做法一致,O(k*p*n)复杂度低于1e8. 2.如果k * p <= n 那么根据抽屉原理,必有至少k个sum[i]相同, 那么任意取k - 1个相同的 sum[i],记它…
题目链接 给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完. 代码很清楚 #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <…
题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最少需要多少次操作可以消除所有的宝石.(每次操作消除一个回文串,最少操作次数清楚字符串) 题解:dp[l][r]表示区间(l,r)的最少操作次数,对于一个区间(l,r),有两种转移:①若存在c_l = c_i(l <= i <= r),可以由dp[l][i] + dp[i + 1][r]转移:②若c…
http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内有多少种上色方案 思路:dp[l][r][i][j]表示从l到r的长度且左右括号分别是i j的方案数 那么转移方程: 1:l+1=r时, dp[l][r][0][1]=1;       dp[l][…
题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红色,涂蓝色,不涂色. 2,每对括号有且仅有其中一个被涂色. 3,相邻的括号不能涂相同的颜色,但是相邻的括号可以同时不涂色. 解题思路: 这个题目的确是一个好题,无奈我太蠢,读错题意.以为(())这样的括号序列在涂色的时候,第一个括号与第三个括号也可以看做是一对.这样的话还要统计合法的括号匹配数目,还…
XOR-pyramid time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output For an array bb of length mm we define the function ff as f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,f(b)={b[…
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…
http://acm.hdu.edu.cn/showproblem.php?pid=6212 题意:有一行的祖玛,只由1和0组成,每次出现连续三个及以上的就会消去,问你最少需要发射多少个球才能消完. 思路:区间最优值问题.先处理一下,把连续相同的放在一起. 对于区间$(i,j)$来说,只有3种情况: ①:把$(i,j)$分成两部分,$d[i][j]=min(d[i][j],d[i][k]+d[k+1][j])$. ②:如果i和j是相同的,那么可以把中间的处理了,然后在合并两端的,$d[i][j]…
题目链接 \(Description\) 给定一个n边凸多边形(保证没有三点共线),求一条经过每个点最多一次的不会相交的路径,使得其长度最大.输出这个长度. \(Solution\) 最长路径应该是尽可能多走点.因为路径不相交,如果当前在\(x\),下次应是向\(x+1\)或\(x-1\)连边,区间也是如此,即当前区间一定是连续的. 画画图可以发现,每个状态应该是左边可以再连出边或右边可以再连出边. 那么令\(f[i][j][0/1]\)表示在\([i,j]\)时左边/右边可以再连出边时的最长路…
Uva 10739 题意:给定字符串,可以增加.删除.修改任意字符,问最少经过多少次操作使字符串回文. 题解:定义dp[l][r]表示把从l到r的子串Sl...Sr变成回文串需要操作的最少次数.字符可以增删改,有的博客说增删是一样的,有的说增比删开销大,我倾向于后者,但前者是对的.因为显然s[l]==s[r]时,dp[l][r]=dp[l+1][r-1];当两者不相等时,可以删去s[l]或者s[r],状态转移到dp[l+1][r]+1或dp[l][r-1]+1,但是增加怎么加?一样的,在状态dp…
因为昨天在Codeforces上设计的区间dp错了(错过了上紫的机会),觉得很难受.看看学长好像也有学,就不用看别的神犇的了. 区间dp处理环的时候可以把序列延长一倍. 下面是 $O(n^3)$ 的朴素区间dp: ; len<=n; len++) { //枚举长度 ; i+len<=n+; i++) { //枚举起点 ; for(int k = i; k<j; k++) { //枚举分割点,更新小区间最优解 dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+][…
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description Bill is trying to compactly represent sequences of capital alphabetic characters from 'A' to 'Z' by folding repeating subsequences insid…
D - Fox And Jumping Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 512B Description Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integer…
Palindromic characteristics of string s with length |s| is a sequence of |s|integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as fo…
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同时消去,问最少需要消去多少次 题解 定义dp[l][r]为区间[l,r]剩下一个字符所需要的最小次数 dp[l][r]=min(dp[l][i]+dp[i+1][r]+x) x为消去剩下两个字符所需要的次数,假如两个字符相同需要x=-1 代码 #include<bits/stdc++.h> #de…
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Genos needs your help. He was asked to solve the following programming problem by Saitama: The length of some string…
任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a line of nn colored squares in a row, numbered from 11 to nn f…
题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b,a)有4个 题解:如果遇到区间而且数又不大n*n能存下来的可以考虑一下用区间dp,然后区间dp一般都是可以通过 预处理来减少for的层数,这里可以预处理一下Is[i][j]表示i,j区间是否是回文串然后dp转移方程就显而易见了. dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1…
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular brackets…
题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l][r-1]-dp[l+1][r-1]+flag[l][r], 如果是回文串flag[l][r]为1. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #incl…
题意:给出一个字符串S,|S|<=500.每次操作可以删除一段连续的相同字母的子串.问,最少操作多少次可以把这个字符串变成空串. 分析:刚开始的思路是,把连续的串给删除掉,然后再....贪心.完全没想到这个题目会是用区间dp来做,惭愧... 虽然后来知道是区间dp,但是想了很久也没想到如何dp.最后看了别人的博客,dp确实有点神奇. $m\times 10^{k_{1}-1}$ ac代码: #include<bits/stdc++.h> using namespace std; cons…
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a regular polygon with nn vertices labeled from 11 to nn in counter-clockwise order. The triangulatio…