POJ 1160 Post Office(区间DP)】的更多相关文章

Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same positio…
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i][j]表示到第i个村庄为止建立j个邮局的最小距离和,dis[i][j]表示i~j之间建一个邮局的最小距离和,我们很容易得出状态转移方程:dp[i][j]=min{dp[k][j]+dis[k+1][i]}(k<i). 主要是dis[i][j]的预处理很巧妙,从别人的博客上看的“将邮局建在i~j中间即…
Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa…
POJ - 3280 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u id=16272" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block;…
Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular…
<题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区间DP的好题.因为本题字符串的长度最大为2e3,所以考虑$O(n^2)$直接枚举区间的两个端点,然后对枚举的区间进行状态转移,大体上有三种转移情况: $dp[l][r]$表示$[l,r]$为回文串的最小代价 对于区间$[l,r]$,当$str[l]==str[r]$时,$dp[l][r]=dp[l+1][r-…
Description 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 regul…
题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). 思路:区间DP,设dp[i][j]是把区间[l, r]内的字符压缩之后的最短长度,那么可以想到区间[l, r]可以通过两种方式转换而来: 1 :[i, j]整个区间本来就可以被压缩 2 :由2个子区间合并而来. 第二种转换是区间DP的常见操作,第一种直接暴力枚举可重叠串的长度即可. 代码: #inc…
题意: 给出一个字符串,其中仅仅含 “ ( ) [ ] ” 这4钟符号,问最长的合法符号序列有多长?(必须合法的配对,不能混搭) 思路: 区间DP的常规问题吧,还是枚举区间[i->j]再枚举其中第k个与第i个来配对,如果配对了就+2这样子. //#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <cmath>…
题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: //#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #define MAX 1000000000 #define FRE() freopen(&qu…