UVA 580 Critical Mass (两次dp)
题意:一个字符串有n个位置每个位置只可能是L或者U,问你在所有可能出现的字符串中最少出现一次三个U连在一起的字符串的个数
题解:首先从左向右枚举每个位置i,保证i,i+1,i+2是U,并且i+2(不包含)前面没有连续三个U连在一起的情况,这样i+2(不包含)后面随便怎么放都可以
接着只需要保证i-1是L并且[1,i-2]没有超过三个U在一起就好了,这儿又使用dp
开二维dp[2][n],第一维表示当前最后一个数是L或者U,第二维表示前j个位置
注意当前最后一个位置i是U时,方程式等于i-1位置是L加上i-2位置是L
不能加上i-1是U再减去i-2是U,因为i-1位置是U没有完全包含i-2位置是U的情况,减多了
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const ll INF=1LL<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
int dp[][];
void DP(int n)
{
dp[][]=1LL;
dp[][]=0LL;
dp[][]=dp[][]=1LL;
dp[][]=dp[][]=2LL;
for(int i=;i<n;++i)
{
dp[][i]=dp[][i-]+dp[][i-];
dp[][i]=dp[][i-]+dp[][i-];
}
return;
}
int Solve(int n)
{
DP(n);
if(n<)
return ;
int ans=(<<(n-));
for(int i=;i<=n;++i)
{
ans+=(<<(n-i))*(dp[][i-]+dp[][i-]);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
printf("%d\n",Solve(n));
}
return ;
}
UVA 580 Critical Mass (两次dp)的更多相关文章
- UVa 580 - Critical Mass(递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 580 Critical Mass
https://vjudge.net/problem/UVA-580 题意:一堆U和L,用n个排成一排,问至少有3个U放在一起的方案数 f[i] 表示 至少有3个U放在一起的方案数 g[i] 表示没有 ...
- UVA 10163 Storage Keepers(两次DP)
UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...
- uva 580 危险的组合(排列组合)
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Descript ...
- UVa 1586 Molar mass --- 水题
UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...
- UVA - 825Walking on the Safe Side(dp)
id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...
- 括号序列问题 uva 1626 poj 1141【区间dp】
首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
随机推荐
- 生产者——消费者模型的java代码实现
生产者 import java.util.Random; public class Producer extends Thread { private Storage<Product> s ...
- hfut 1287
http://acm.hfut.edu.cn/OnlineJudge/ 中文题. 区间线段树,需要剪枝.n的大小有问题. #include <iostream> #include < ...
- rest规范 ; restful 风格; gradel介绍 ; idea安装 ;
[说明]上午整理了一下心情:下午继续开始任务,了解了restful,知道了那个牛人的博士论文,下载了管理工具gradle,并且部署了环境:晚上安装了idea继承环境并且建了一个简单的gradle项目( ...
- css,查询相应标签,div等
1.类名 .类别 例子: 查询类名为“useradd” .useradd{ margin-top:50px; margin-left:200px;} 2.属性找 例子:查询类为useradd下的inp ...
- standard pbr(三)-BRDF
// Default BRDF to use: #if !defined (UNITY_BRDF_PBS) // allow to explicitly override BRDF in custom ...
- shell方式切割tomcat日志
#!/bin/bash while true do cd /usr/local/tomcat/logs d=`date +%Y%m%d` d15=`date -d'15 day ago' +%Y%m% ...
- jquery ajax属性async(同步异步)示例
在jquery的ajax中如果我们希望实现同步或者异步我们可以直接设置async发生为真或假即可true false,下面举几个jquery ajax同步和异步实例 例1.jquery+ajax/&q ...
- Ubuntu 安装VMware Tools
安装步骤: 首先,点击VMware菜单的-VM-Install VMware Tools (虚拟机-装载VMwareTool 工具) 这时,在Ubuntu下会自动加载Linux版的VMware Too ...
- springboot-vue项目前台1
- [转]springmvc中的常用的返回
package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...