自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

Neutralzz的博客

我有自己的梦想和追求!

  • 博客(227)
  • 收藏
  • 关注

原创 Light OJ 1246 Colorful Board (棋盘黑白染色+数学)

解析:将矩形黑白染色,则根据题意,黑格和白格不能有同色出现。所以K种颜色有了三种划分,一是染了某个黑格,二是染了某个白格,三是没有使用。如果有i种颜色染黑格,则方案数等价于 n个不同的物品放入m个不同的箱子,箱子非空的方案数(Strling(n,m)*m!)。[code]:#include#include#includeusing namespace std;typedef

2016-02-15 10:54:30 691

原创 Light OJ 1231 1232 1233 Coin Change (DP)

解析:第一个第二个都是简单的背包,不解释。第三个设dp[i][j]为考虑前i个硬币,凑成j的方案数。状态转移方程:dp[i][j] = sigma(dp[i-1][j-k*A[i]]) (0整理后,得dp[i][j] = dp[i-1][j]+dp[i][j-A[i]]-dp[i-1][j-(C[i]+1)*A[i]][1231's code]:#inclu

2016-02-14 16:52:02 306

原创 Light OJ 1226 One Unit Machine (大组合数计算+DP)

解析:设dp[i]为考虑前i个任务的方案数,s[i]为前i 个任务的总时间。则dp[0] = 1,s[0] = a[0]。状态转移方程: dp[i] = dp[i-1]*C(s[i-1]+a[i]-1,a[i]-1)。[code]:#include#include#include#include#includeusing namespace std;typedef

2016-02-14 15:53:16 355

原创 Light OJ 1217 Neighbor House (II) (环形DP)

解析:枚举每一个位置,将该位置和其影响的位置删去,从而把环化为链。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;int n,a[1005],dp[1005][2];int main(){ int i,j,cas,T; sca

2016-02-14 15:21:43 321

原创 Light OJ 1205 Palindromic Numbers (数位DP)

题意:问区间[l,r]内有多少个数是回文,不考虑前导0。解析:其实是个搜索,i位到s位有多少个数字,s是中间位置。找出回文数字其实只要确定出中间位置,然后找出有多少个数。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;int bit[20],

2016-02-14 14:28:41 192

原创 Light OJ 1201 A Perfecr Murder (简单树形DP)

解析:设dp[i][0]为在i的子树中,不谋杀i的最大人数。dp[i][1]为在i的子树中,谋杀i的最大人数。然后转移状态就好。。水题一个。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;const int maxn = 1005;st

2016-02-14 12:57:43 201

原创 Light OJ 1200 Theif (多重背包)

解析:先把必须要装入的总重量减去就是了。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;int n,w,price[105],weight[105],dp[10005];int main(){ int i,j,cas,T; s

2016-02-14 11:18:33 289

原创 Light OJ 1191 Bar Codes (DP)

解析:实质上和Light oj 1145是一个题 可以参考http://blog.csdn.net/qq_26572969/article/details/50654796[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;int n,k,m;LL

2016-02-13 22:50:39 241

原创 Codeforces 479E Riding in a Lift (DP)

题意:有n层楼,你现在在a层,禁地在b层,每次的移动如下:从x层移动到y层,必须满足|x-y|解析:设dp[i][j]为移动了i次位于j层的方案数。则对于dp[i-1][j],dp[i][每个从j层可以移动到的位置] += dp[i-1][j]。采用类似BIT区间增减的方法标记对应的端点,进行状态转移。[code]:#include#include#include#inc

2016-02-13 22:22:41 236

原创 Light OJ 1193 Dice (II) (DP)

解析:设dp[i][j]为考虑前i个骰子和为j的score的和。dp[0][0] = 1;dp[i][j] = sigma(k*dp[i-1][j-k])  (1我现在仍然想用类似BIT区间增减的方法对两端进行标记,然而对于dp[i-1][j]来说,dp[i][j+1] += 1*dp[i-1][j].....dp[i][j+k] += k*dp[i-1][j]对于

2016-02-13 22:04:16 261

原创 Light OJ 1170 Counting Perfect BST (DP+数学)

解析:如果知道了区间[a,b]内perfct power的个数,就可以dp求对应的BST的个数。设dp[i]为有i个perfct power对应的BST的个数(卡特兰数)。dp[0] = 1;dp[i] = sigma(dp[j]*dp[i-1-j]) (0至于求区间[a,b]内perfct power的个数,有两种方法。方法一:定义一个数是基,当且仅当这个数不是另一个数的幂次

2016-02-13 13:26:19 350

原创 Light OJ 1169 - Monkeys on Twin Tower (简单DP)

解析:a[0][j]表示在第一个塔的第j层用时,同理a[1][j];b[0][j]表示从第一个塔的第j层跳到第二个塔的第j+1层用时,同理b[1][j];则令dp[0][j]为到达第一个塔的第j层的最少总用时,同理dp[1][j];状态转移方程:dp[j][i] = min(dp[j][i-1],dp[(j+1)%2][i-1]+b[(j+1)%2][i-1])+a[j][i];

2016-02-12 22:32:44 234

原创 Light OJ 1159 Batman (简单DP)

仿着两个字符串的LCS写就是了。[code]:#include#include#includeusing namespace std;char s[3][55];int dp[55][55][55],len[3];int main(){ int i,j,k,cas,T; scanf("%d",&cas); for(T = 1;T <= cas;T+

2016-02-12 22:03:58 260

原创 Light OJ 1158 Anagram Division(状压+数位DP)

题意:给你一个字符串s,s中只包含数字,问s的所有排列所形成的数字中有多少能被d整除。解析:数位和状压还是容易想到的,问题是怎么压缩状态,这里的状态不能用多进制记录出现次数,但因字符串的长度[code]:#include#include#includeusing namespace std;int n,d,dp[1<<10][1001];char s[15];int

2016-02-12 21:31:44 255

原创 Light OJ 1145 Dice (I) (DP)

解析:设dp[i][j]为考虑前i个骰子,和为j的方案数。状态转移时,dp[i][j+1....j+k+1]都要加上dp[i][j]。直接转移的话复杂度为n*k*m。借助树状数组区间增减的思想,对需要更改的区间的两端进行标记,这样复杂度为n*m。[code]:#include#include#include#include#includeusing namespac

2016-02-12 16:34:51 281

原创 Light OJ 1140 How Many Zeroes? (数位DP)

解析:注意前导0和边界0,细节有点烦。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;const int maxn = 1e5+5;const int inf = 0x3f3f3f3f;int bit[20],top;LL m,n,dp[2

2016-02-12 15:32:21 223

原创 Light OJ 1134 Be Efficient(水题)

解析:记录前缀和mod M的值。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;const int maxn = 1e5+5;const int inf = 0x3f3f3f3f;int n,m;LL s[maxn];int main(

2016-02-12 13:27:07 310

原创 Light OJ 1126 Building Twin Towers (DP)

题意:给出n个砖块,砌出两个相同高度的塔,问塔的最大高度为多少。解析:设dp[i][j]为考虑前i个砖块,高度差为j的最高时两塔较小的高度。状态转移:dp[i][j] = max(dp[i-1][j],dp[i-1][j+a[i]]+a[i],(a[i]>=j?dp[i-1][a[i]-j]+a[i]-j:dp[i-1][j-a[i]]));[code]:#include#inc

2016-02-12 12:48:24 204

原创 Light OJ 1125 Divisible Group Sums (DP)

题意:从n个数字中取出m个数字形成一个集合,问有多少个集合,集合中元素和为D的倍数。解析:dp[i][j][k] 考虑前i个数字从中取出j个 mod D = k 的个数。状态转移方程:dp[i][j][k] = dp[i-1][j][k]+dp[i-1][j-1][((k-a[i])%d+d)%d];[code]:#include#include#include#incl

2016-02-11 21:40:43 221

原创 Light OJ 1122 Digit Count (简单数位DP)

题意:求长度为n的每一位都属于集合S且相邻位差的绝对值解析:套模板记忆化搜索。[code]:#include#include#include#include#includeusing namespace std;int n,m,a[10],dp[15][15];int dfs(int i,int s,bool z){//printf("%d %d %d\n",i,

2016-02-11 17:44:57 216

原创 Light OJ 1119 Pimp My Ride (简单状压DP)

解析:用二进制表示各工作的完成状态S。dp[S] = min(dp[S^(1>i&1&&S>>j&1)[code]:#include#include#include#include#includeusing namespace std;int n,a[14][14],dp[1<<14];int main(){ int i,j,cas,T; sca

2016-02-11 17:19:36 329

原创 Light OJ 1110 An Easy LCS (DP+路径记录)

解析:要输出LCS,只需要记录一下dp的过程即可。[code]:#include#include#include#include#includeusing namespace std;typedef pair PII;typedef long long LL;char s[2][105],tmp[2][105];int n,m,dp[105][105];PII pr

2016-02-11 16:53:09 221

原创 Light OJ 1105 Fi Binary Number(二分+数位DP)

解析:二分x, 用数位dp查询1到x有多少个符合要求的数。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;const int maxn = 1005;const LL MOD = 1e9+7;const LL INF = 1e18;in

2016-02-11 11:57:41 266

原创 Light OJ 1095 Arrange the Numbers (容斥)

解析:先从前M个中取出K个,剩下的所有的排列个数为(n-k)!;然后剩下的M-K中取出1个放在其初始位置,剩下的排列个数为(n-k-1)!;............最后容斥原理求解便是。[code]:#include#include#include#include#includeusing namespace std;typedef long long LL;

2016-02-11 11:07:24 238

原创 Light OJ 1086 Jogging Trails (Floyd+状压DP)

题意:给出一个图,求从任意一点出发遍历所有的边后回到起点的最短路径长度。解析:对于每一个节点,进和出的次数都是相同的,所以如果该节点度数是偶数,则每条边都会经过一次,如果是奇数,应走一条最短路到另一个度数为奇数的点。对节点度数进行状压。[code]:#include#include#include#include#include#define lson l,mid,rt

2016-02-10 17:48:10 256

原创 Light OJ 1084 Winter (线段树+DP)

题意:有n个点在一条线上,每个点最多移动k距离,问通过移动这n个点,最少能形成多少个组,使得每个组至少三个点,且每个点都属于一个组。解析:先将点按位置排序,设dp[i]为前i个点的最少组数 ,如果不能形成,则dp[i] = INF。则状态转移方程为dp[i] = max(dp[i],dp[i-k]+1)     (i-k+1...i这k个点构成一组) 用线段树维护序列就能AC[

2016-02-10 15:02:53 212

原创 Light OJ 1079 Just another Robbery (概率+背包)

解析:dp[i][j]为考虑前i个银行,获得j元,不被逮捕的最大概率。状态转移方程:dp[i][j] = max(dp[i-1][j],dp[i-1][j-a[i]]*(1-p[i]));[code]:#include#include#include#include#includeusing namespace std;const double eps = 1e-7;

2016-02-10 13:39:29 443

原创 Light OJ 1071 Baker Vai (DP)

题意 :n*m的方格,每个方格有一个权值,问从(1,1)到(n,m)的不相交的两条路径所经过的方格的最大权值和。解析:很简单,斜着按步数DP。dp[i][j][k]是走i步后两路径的横坐标分别为j和k的最大权值和。[code]:#include#include#includeusing namespace std;int n,m,dp[205][105][105],a[

2016-02-10 13:01:36 268

原创 Light OJ 1068 Investigation (数位DP)

题意:找出区间[A,B]内能被K整除且各位的和也能被K整除的数的个数。解析:数位DP 显然 K>90时必然为0对于K[code]:#include#include#includeusing namespace std;int dp[12][100][100];int a,b,k,bit[12],top;int bug = 0;int dfs(int i,i

2016-02-10 11:04:02 216

原创 Light OJ 1064 Throwing Dice(简单DP)

题意:投掷n个骰子,问和至少为x的概率。[code]:#include#include#includeusing namespace std;typedef long long LL;LL n,m,dp[30][200];LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b);}int main(){ int i,j,

2016-02-09 19:59:00 300

原创 Light OJ 1060 nth Permutation (状压DP)

题意:给出一个n个字符的字符串(n解析:先将字符串排序状态S记录第i位的字符是否有效。类比全排列的过程。dp[S]记录状态S中的有效字符组成的序列的排列个数。根据这个就可以确定第k个排列所在的位置。(具体看代码)[code]:#include#include#include#include#include#define th(x) this->x=xusing

2016-02-04 20:57:24 184

原创 LightOJ 1057 Collecting Gold(简单状压DP)

把题目转化成图的形式,然后就是个经典的TSP。[code]:#include#include#include#include#include#define th(x) this->x=xusing namespace std;const int inf = 0x3f3f3f3f;struct Node{ int x,y; void init(int x,

2016-02-04 17:25:48 271

原创 Codeforces 478D Red-Green Towers (DP)

题意:有r个红砖块和b个绿砖块,用这些砖块建一个塔,假设塔有h层,则从下往上第i层有h-i+1个同种颜色的砖块。问有多少种方案使塔的高度最高。解析:塔的最大高度很容易求得。h最大不过700。设h是塔的最大高度,dp[i][j]为建i层塔用j个红砖块的方案数。则 dp[i][j] = sigma(dp[k][j])+dp[i-1][j-i] (1因空间会爆,所以用滚动数组。[co

2016-02-04 16:46:25 551

原创 Light OJ 1050 Marbles (概率DP)

题意:A和B玩一种游戏,箱子里有两种球,红球和蓝球,球的总数是奇数,A先手,轮到A时,A从箱子里随机取一球,轮到B时,B从中拿出一个蓝球,如果轮到B时,已经没有蓝球,则B胜出。如果取出的最后一个球是蓝球(无论是A取还是B取),则A胜出,反之B胜出。求A获胜的概率。解析:显然 如果剩下奇数个球,这一轮肯定是A,反之是B。dp[i][j]为有i个红球j个蓝球A获胜的概率。则递推关系:

2016-02-03 17:39:27 205

原创 Light OJ 1047 Neighbor House(简单DP)

题意:房子排成一排,每个房子可以涂三种颜色,每个房子涂一种颜色对应一个花费,相邻房子不能同色。问最少花费。解析不写了。[code]:#include#include#includeusing namespace std;typedef long long LL;int n,dp[50][3],a[50][3];int main(){ int i,j,k,cas

2016-02-03 16:21:28 265

原创 Light OJ 1044 Palindrome Partitioning (hash+DP)

题意:给出一个字符串,求其最少由多少个连续的回文子串构成。解析:hash用来判一个子串是否是回文串,正反预处理保存各个位置上的hash值。dp[i]:下标1~i对应的子串最少由多少个连续的回文子串构成。dp[i] = min(dp[j-1]+1)  (j[code]:#include#include#includeusing namespace std;typedef

2016-02-03 15:45:31 202

原创 Light OJ 1038 Race to 1 Again (期望DP)

题意: He selects a number N. And he calls this D.In each turn he randomly chooses a divisor of D (1 to D). Then he divides D by the number to obtain new D. He repeats this procedure until D becomes 1. W

2016-02-02 19:51:08 188

原创 Light OJ 1037 Agent 47 (状压DP)

题目:你有一个伤害为1的手枪,有n个人你需要去杀害,每个人有血量值h[i],杀害了第i个人你可以获得他的手枪,并用来杀害目标。s[i][j]为第i个人的手枪对第j个人的伤害值。求最少需要打几枪杀完所有人。解析:状压DPS的第i个位表示第i个目标是否已杀害。状态转移见代码。[code]:#include#include#include#include#include

2016-02-02 19:22:27 238

原创 Light OJ 1036A Refining Company (DP)

题目:http://lightoj.com/volume_showproblem.php?problem=1036解析:dp[i][j]为在1~i,1~j的矩形区域内的最大sum。dp[i][j] = max(dp[i-1][j]+a[i][j],dp[i][j-1]+b[i][j]),a,b是前缀和。[code]:#include#include#includeusing

2016-02-02 18:10:29 664

原创 Light OJ 1033 Generating Palindromes (最长回文子串 区间DP)

题意:给你一个字符串,问最少插入多少个字符能使其成为回文串。解析:字符串长度n - 最长回文子串的长度。[code]:#include#include#includeusing namespace std;char s[105];int n,dp[105][105];int main(){ int i,j,cas,T; scanf("%d",&cas)

2016-02-02 16:42:04 271

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除