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中通过配置文件的方式(Jedis驱动)使用Redis
在java中使用Redis,实际上是将Redis的一些命令封装到Jedis的实体类中,然后进行调用.
- hdu1174(3维射线与圆是否相交)
简单的题意,要注意z2 = h2*0.9-r2 #include <iostream> #include <cmath> #include <vector> #in ...
- 【BZOJ3998】[TJOI2015]弦论 后缀自动机
[BZOJ3998][TJOI2015]弦论 Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T ...
- 1307 绳子与重物(DFS)
1307 绳子与重物 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量 ...
- Xamarin.Forms学习之Page Navigation(二)
在上一篇的文章中,对页面常规的导航做一些分享,然而在实际的编程中,页面数据的保持,传值等等都有很多,这篇文章就对这些相关内容做一个分享和记录,有问题,希望大家留言指正.这一篇从实现业务逻辑来讲并没有什 ...
- resetForm(name1,name2)-我的JavaScript函数库-mazey.js
重置表单输入值为原始(空)状态. 参数:name1,name2,name3...NAME属性,可以多个. function resetForm(){ for(var i = 0; i < arg ...
- TimeStamp 毫秒和纳秒
毫秒 /** * Returns the time represented by this Timestamp object, as a long value * containing the num ...
- POCO c++ 使用例子
.定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using P ...
- 开发人员必备的 Chrome 扩展
Firebug:不用多介绍了吧 https://chrome.google.com/webstore/detail/bmagokdooijbeehmkpknfglimnifench ChromeSni ...
- python3 中引用 HTMLTestRunner.py 模块的注意事项
HTMLTestRunner.py支持python2中运行,如果在python3.6.2中引用HTMLTestRunner.py模块,需要做一下更改: 1.更改HTMLTestRunner.py模块中 ...