原文链接www.cnblogs.com/zhouzhendong/p/AGC030F.html

草率题解

对于每两个相邻位置,把他们拿出来。

如果这两个相邻位置都有确定的值,那么不管他。

然后把所有的这些数拿出来,分为两类,一类是没有被填入的,一类是被填入的。

然后大力DP即可。由于没有被填入的可以任意排列,所以最后还要乘上一个阶乘。

代码

  1. #include <bits/stdc++.h>
  2. #define clr(x) memset(x,0,sizeof x)
  3. #define For(i,a,b) for (int i=(a);i<=(b);i++)
  4. #define Fod(i,b,a) for (int i=(b);i>=(a);i--)
  5. #define fi first
  6. #define se second
  7. #define pb(x) push_back(x)
  8. #define mp(x,y) make_pair(x,y)
  9. #define outval(x) cerr<<#x" = "<<x<<endl
  10. #define outtag(x) cerr<<"---------------"#x"---------------"<<endl
  11. #define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
  12. For(_x,L,R)cerr<<a[_x]<<" ";cerr<<endl;
  13. using namespace std;
  14. typedef long long LL;
  15. typedef vector <int> vi;
  16. LL read(){
  17. LL x=0,f=0;
  18. char ch=getchar();
  19. while (!isdigit(ch))
  20. f|=ch=='-',ch=getchar();
  21. while (isdigit(ch))
  22. x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
  23. return f?-x:x;
  24. }
  25. const int N=305*2,mod=1e9+7;
  26. int Pow(int x,int y){
  27. int ans=1;
  28. for (;y;y>>=1,x=(LL)x*x%mod)
  29. if (y&1)
  30. ans=(LL)ans*x%mod;
  31. return ans;
  32. }
  33. void Add(int &x,int y){
  34. if ((x+=y)>=mod)
  35. x-=mod;
  36. }
  37. void Del(int &x,int y){
  38. if ((x-=y)<0)
  39. x+=mod;
  40. }
  41. int Add(int x){
  42. return x>=mod?x-mod:x;
  43. }
  44. int Del(int x){
  45. return x<0?x+mod:x;
  46. }
  47. int n;
  48. int a[N],b[N];
  49. int cnt=0,tot=0;
  50. int dp[N][N][N];
  51. vector <int> v;
  52. int main(){
  53. n=read();
  54. For(i,1,n*2){
  55. a[i]=read();
  56. if (a[i]!=-1)
  57. b[a[i]]=1;
  58. }
  59. For(i,1,n){
  60. if (a[i*2-1]==-1&&a[i*2]==-1)
  61. cnt++,tot+=2;
  62. else if (a[i*2-1]==-1)
  63. tot+=2,v.pb(a[i*2]);
  64. else if (a[i*2]==-1)
  65. tot+=2,v.pb(a[i*2-1]);
  66. }
  67. For(i,1,n*2)
  68. if (!b[i])
  69. v.pb(i);
  70. sort(v.begin(),v.end());
  71. v.pb(0);
  72. reverse(v.begin(),v.end());
  73. dp[0][0][0]=1;
  74. For(i,1,tot)
  75. For(j,0,tot)
  76. For(k,0,tot){
  77. int val=dp[i-1][j][k];
  78. if (!val)
  79. continue;
  80. if (!b[v[i]]){
  81. Add(dp[i][j+1][k],val);
  82. if (j>0)
  83. Add(dp[i][j-1][k],val);
  84. if (k>0)
  85. Add(dp[i][j][k-1],(LL)val*k%mod);
  86. }
  87. else {
  88. Add(dp[i][j][k+1],val);
  89. if (j>0)
  90. Add(dp[i][j-1][k],val);
  91. }
  92. }
  93. int ans=dp[tot][0][0];
  94. For(i,1,cnt)
  95. ans=(LL)ans*i%mod;
  96. cout<<ans<<endl;
  97. return 0;
  98. }

AtCoder Grand Contest 030 (AGC030) F - Permutation and Minimum 动态规划的更多相关文章

  1. AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC030C.html 题解 才发现当时是被题意杀了. 当时理解的题意是“对于任意的 (i,j) ,颜色 i 和 ...

  2. Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划

    原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a ...

  3. AtCoder Grand Contest 030题解

    第一次套刷AtCoder 体验良好 传送门 Poisonous Cookies cout<<b+min(c,a+b+); Tree Burning 难度跨度有点大啊 可以证明当第一次转向之 ...

  4. Atcoder Grand Contest 030 F - Permutation and Minimum(DP)

    洛谷题面传送门 & Atcoder 题面传送门 12 天以前做的题了,到现在才补/yun 做了一晚上+一早上终于 AC 了,写篇题解纪念一下 首先考虑如果全是 \(-1\)​ 怎么处理.由于我 ...

  5. AtCoder Grand Contest 030 自闭记

    A:阅读. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...

  6. AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...

  7. AtCoder Grand Contest 030 Solution

    A - Poisonous Cookies 签到. #include <bits/stdc++.h> using namespace std; #define ll long long l ...

  8. Coloring Torus(Atcoder Grand Contest 030 C)

    怎么外国都喜欢考脑筋急转弯…… 题意 输入 $k$,要求构造一个 $n\times n$ 的矩阵($n$ 自选),使得恰好用 $k$ 中颜色把每个点都染色,并且同一种颜色的格子周围 相邻的每种颜色数量 ...

  9. AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...

随机推荐

  1. windows下安装hexo和生成博客

    首先在电脑上安装node和git,这个只要在相关官网的下载然后一步安装即可. 然后在你的电脑上新建一个文件夹,用来存放你的博客文件,比如创建hexo 进入该文件,右键打开git bash 安装hexo ...

  2. adb server is out of date. killing... ADB server didn't ACK * failed to start daemon *……

    问题 使用 adb 命令的时候报错如下: adb server is out of date. killing... ADB server didn't ACK * failed to start d ...

  3. CSS中常见的布局

    一.css中常见的布局有哪些? (1)两列布局 (2)三列布局 (3)弹性布局 (4)圣杯布局 (5)双飞翼布局 二.具体实现  (1)两列布局 https://www.cnblogs.com/qin ...

  4. excel2016打开为空白界面解决办法

    前言 excel2016打开文件为空白的界面,明显不正常. 解决方法 https://blog.csdn.net/b2345012/article/details/94134401 以上.

  5. day34-python之进程调用

    1.信号量 import threading,time class myThread(threading.Thread): def run(self): if semaphore.acquire(): ...

  6. [ipsec][crypto] ike/ipsec与tls的认证机制比较

    前言 接上篇:[ipsec][crypto] 有点不同的数字证书到底是什么 本篇内容主要是上一篇内容的延伸.抽象的从概念上理解了证书是什么之后,我们接下来 从实践的角度出发,以IKEv2和TLS两个协 ...

  7. sqlite3入门之sqlite3_open,sqlite3_exec,slite3_close

    sqlite3_open sqlite3_open函数原型: int sqlite3_open( const char *filename, /* Database filename (UTF-8) ...

  8. git修改提交历史中的author信息

    当次提交 当次的提交显示指定提交者信息: git commit -m "Initial commit" --author="mn <mn@furzoom.com&g ...

  9. 隐藏按钮button

    <td> <input id="del" type="button" value="删除" onclick="u ...

  10. 剑指offer:二维数组的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...