[CF Gym 100372B] Sergey and a pattern
2017-01-24 14:45
387 查看
题意
给出一个初始序列ai和一个最终序列bi,要求用一个栈执行两种操作1 从ai的前面取出一个数字压到栈中
2 弹栈,将栈顶放到bi的后面。给出的最终序列中有一些位置是位置的数字,询问有多少种方法构造出bi来。
长度至多16
题解
将ai中数字是否在栈中状压,状态表示f[压栈次数][弹栈次数][状态]然后dp
代码
#include <cstdio> #define maxn 16 #define maxs (1<<16) #define mod 1000000007LL int n, a[maxn+1], b[maxn+1], f[maxn+1][maxn+1][maxs] = {0}; /// f[in][out][exist] int main() { int i, j, s, k; scanf("%d", &n); for (i = 1; i <= n; i ++ ) scanf("%d", &a[i]); for (i = 1; i <= n; i ++ ) scanf("%d", &b[i]); /// build array b from array a ! f[0][0][0] = 1; for (i = 0; i <= n; i ++ ) for (j = 0; j <= i; j ++ ) for (s = (1<<i)-1; s >= 0; s -- ) { if (!f[i][j][s]) continue; if (i < n) (f[i+1][j][s|(1<<i)] += f[i][j][s]) %= mod; if (!s) continue; for (k = i; !((1<<k-1)&s); k -- ); if (b[j+1]!=-1 && a[k]!=b[j+1]) continue; (f[i][j+1][s^(1<<k-1)] += f[i][j][s]) %= mod; } printf("%d\n", f [0]); //while(1); return 0; }
相关文章推荐
- [CF Gym 100372C] Sergey and array
- [CF Gym 100372E] Sergey’s game
- CF 388A:Fox and Box Accumulation
- cf 442C. Artem and Array
- cf/Codeforces Gym 100548F Color (容斥原理)
- CF449 C. Jzzhu and Apples
- CF 449B - Jzzhu and Cities(最短路)
- Head First Design Pattern - 2 - A Subject and Observer spar over the right way to get state information to the Observer
- [转贴] Double-checked locking and the Singleton pattern
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
- CF 400C - Inna and Huge Candy Matrix
- CF 好题集锦 PART 0 & #388 B Fox and Minimal path 题解
- cf B. Wilbur and Array
- [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]
- CF-599B - Spongebob and Joke
- 《Pattern Recognition and Machine Learning》学习笔记 第一章(二)
- cf 341D lahub and xors
- CF10月25日比赛。(Dima and Text Messages)
- 【Pattern Recognition and Machine Learning】p7 preface
- CF 459C(Pashmak and Buses-分治)