https://zybuluo.com/ysner/note/1232641

题面

给出\(b_1,b_2,...,b_n\in\{−1,1\}\),求满足\((p_i−i)*b_i<0\)的\(\{1,2,...,n\}\)的排列\(p_i\)的数量。

  • \(n\leq20\)

解析

一开始并不知道这状态怎么设

设\(f[i][j]\)表示当前填到了第\(i\)个位置,有\(j\)个\(b_i>0\)(即\(+\)号)不满足。

则可顺利写出决策:

  • 当前位置是\(+\):

    1、用当前的\(i\)填掉前面的一个空,有\(j\)种选择。

    2、该位不填,留到后面用。
  • 当前位置是\(-\):(必须用掉)

    1、用当前的\(i\)填掉前面的一个空,有\(j\)种选择。

    2、替代前面一个\(+\)号,再从前面的\(+\)号中选一个填在这里,均有\(j\)种选择。

这种“延时\(DP\)”我似乎只会用它来计数。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define re register
#define il inline
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
char s[25];
ll n,f[25][25];
il ll gi()
{
re ll x=0,t=1;
re char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
int main()
{
while(scanf("%s",s+1)!=EOF)
{
n=strlen(s+1);memset(f,0,sizeof(f));
f[0][0]=1;
fp(i,1,n)
{
if(s[i]=='+')
fp(j,1,i)
f[i][j]+=f[i-1][j-1]+f[i-1][j]*j;
else
fp(j,0,i)
{
f[i][j]+=f[i-1][j]*j+f[i-1][j+1]*(j+1)*(j+1);
}
}
printf("%lld\n",f[n][0]);
}
return 0;
}

[HDU4689]Derangement的更多相关文章

  1. 【题解】HDU4689 Derangement(有技巧的计数DP)

    [题解]HDU4689 Derangement(有技巧的计数DP) 传送门 呵呵没告诉我多测组数,然后\(n\le 20,7000\mathrm{ms}\)我写了个状压上去T了 题目大意: 要你求错排 ...

  2. HDU-4689 Derangement DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4689 题意:初始序列1,2...n,求所有满足与初始序列规定大小的错排数目.. 这道题目感觉很不错~ ...

  3. HDU-4689 Derangement

    太洗脑了: 题目意思:初始队列是1,2, 3.......n :在打乱这个队列切保证每个数字都不在原来的位置上的情况下给出一个具有+,- 的队列: 被打乱的队列 和 原来队列 对应位置的大小的关系是那 ...

  4. 【HDU4689】Derangement(动态规划)

    [HDU4689]Derangement(动态规划) 题面 Vjudge 给定一个\(+-\)组成的字符串,长度为\(n\). 如果第\(i\)位是\(+\),那么\(p_i>i\),否则\(p ...

  5. [LeetCode] Find the Derangement of An Array 找数组的错排

    In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no ...

  6. [leetcode-634-Find the Derangement of An Array]

    In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no ...

  7. HDOJ 4689 Derangement DP

    DP具体解释见: http://blog.csdn.net/liguan1/article/details/10468139 Derangement Time Limit: 7000/7000 MS ...

  8. 暑假集训 div1 B Derangement 交换数字 思维死角

    B. Derangement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  9. AtCoder Regular Contest 082 D Derangement

    AtCoder Regular Contest 082 D Derangement 与下标相同与下个交换就好了.... Define a sequence of ’o’ and ’x’ of lengt ...

随机推荐

  1. Centos6.7 安装Naigos教程

    Centos6.7 安装Naigos教程参考文档:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickst ...

  2. day10-函数基础知识

    函数 什么是函数 把工具事先准备好,然后下次使用的时候,直接使用就行了.我们的函数就是工具 为何用函数 1.遇到重复的功能只能重复编写实现代码,代码冗余 2.功能需要扩展时,需要找出所有实现该功能的地 ...

  3. CodeCombat代码全记录(Python学习利器)--Kithgard地牢代码1

    Kithgard地牢注意:在调用函数时,要在函数的后面加上括号内容,否则在python中,将不会认为你在调用这个函数内容,而你的英雄将像木头一样站在原地不会执行上左下右的移动!!! hero.move ...

  4. cvpr2016论文

    http://openaccess.thecvf.com/ICCV2017.py http://openaccess.thecvf.com/CVPR2017.py http://www.cv-foun ...

  5. 后台取前台input标签值方法

    直接在实体类中增加一个get set方法就可以实现取到value值

  6. 浏览器 <html>相关

    若页面需默认用极速核,增加标签:<meta name="renderer" content="webkit">  https://blog.csdn ...

  7. Python - 面对对象(基础)

    目录 Python - 面对对象(基础) 一. 概述 二. 创建类和对象 三. 面向对象三大特征 封装 继承 多态 Python - 面对对象(基础) 一. 概述 面向过程:根据业务逻辑从上到下写垒代 ...

  8. 解决WP程序 重复打开出现 “正在加载...” 字样 解决方案

    在开发winphone程序时候 我们经常遇到调试.在调试的时候 可能会重复打开 debug一下.可是有时候 经常遇到 "正在加载...."字样.而且很慢.效率很低. 测试发现 在 ...

  9. GeoTrust 企业(OV)型 多域名(SAN/UC)版 SSL证书

     GeoTrust 企业(OV)型 多域名(SAN/UC)版 SSL证书(GeoTrust True BusinessID With Multi-Domain(SAN/UC) ),支持多域名,属于企业 ...

  10. Linux imooc learning

    https://www.imooc.com/video/3529 Windows Vs Linux Linux: (other linux overall   https://onedrive.liv ...