AtCoder Grand Contest 030 (AGC030) F - Permutation and Minimum 动态规划
原文链接www.cnblogs.com/zhouzhendong/p/AGC030F.html
草率题解
对于每两个相邻位置,把他们拿出来。
如果这两个相邻位置都有确定的值,那么不管他。
然后把所有的这些数拿出来,分为两类,一类是没有被填入的,一类是被填入的。
然后大力DP即可。由于没有被填入的可以任意排列,所以最后还要乘上一个阶乘。
代码
#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"---------------"#x"---------------"<<endl
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
For(_x,L,R)cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef vector <int> vi;
LL read(){
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f|=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=305*2,mod=1e9+7;
int Pow(int x,int y){
int ans=1;
for (;y;y>>=1,x=(LL)x*x%mod)
if (y&1)
ans=(LL)ans*x%mod;
return ans;
}
void Add(int &x,int y){
if ((x+=y)>=mod)
x-=mod;
}
void Del(int &x,int y){
if ((x-=y)<0)
x+=mod;
}
int Add(int x){
return x>=mod?x-mod:x;
}
int Del(int x){
return x<0?x+mod:x;
}
int n;
int a[N],b[N];
int cnt=0,tot=0;
int dp[N][N][N];
vector <int> v;
int main(){
n=read();
For(i,1,n*2){
a[i]=read();
if (a[i]!=-1)
b[a[i]]=1;
}
For(i,1,n){
if (a[i*2-1]==-1&&a[i*2]==-1)
cnt++,tot+=2;
else if (a[i*2-1]==-1)
tot+=2,v.pb(a[i*2]);
else if (a[i*2]==-1)
tot+=2,v.pb(a[i*2-1]);
}
For(i,1,n*2)
if (!b[i])
v.pb(i);
sort(v.begin(),v.end());
v.pb(0);
reverse(v.begin(),v.end());
dp[0][0][0]=1;
For(i,1,tot)
For(j,0,tot)
For(k,0,tot){
int val=dp[i-1][j][k];
if (!val)
continue;
if (!b[v[i]]){
Add(dp[i][j+1][k],val);
if (j>0)
Add(dp[i][j-1][k],val);
if (k>0)
Add(dp[i][j][k-1],(LL)val*k%mod);
}
else {
Add(dp[i][j][k+1],val);
if (j>0)
Add(dp[i][j-1][k],val);
}
}
int ans=dp[tot][0][0];
For(i,1,cnt)
ans=(LL)ans*i%mod;
cout<<ans<<endl;
return 0;
}
AtCoder Grand Contest 030 (AGC030) F - Permutation and Minimum 动态规划的更多相关文章
- AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC030C.html 题解 才发现当时是被题意杀了. 当时理解的题意是“对于任意的 (i,j) ,颜色 i 和 ...
- Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划
原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a ...
- AtCoder Grand Contest 030题解
第一次套刷AtCoder 体验良好 传送门 Poisonous Cookies cout<<b+min(c,a+b+); Tree Burning 难度跨度有点大啊 可以证明当第一次转向之 ...
- Atcoder Grand Contest 030 F - Permutation and Minimum(DP)
洛谷题面传送门 & Atcoder 题面传送门 12 天以前做的题了,到现在才补/yun 做了一晚上+一早上终于 AC 了,写篇题解纪念一下 首先考虑如果全是 \(-1\) 怎么处理.由于我 ...
- AtCoder Grand Contest 030 自闭记
A:阅读. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...
- AtCoder Grand Contest 030 Solution
A - Poisonous Cookies 签到. #include <bits/stdc++.h> using namespace std; #define ll long long l ...
- Coloring Torus(Atcoder Grand Contest 030 C)
怎么外国都喜欢考脑筋急转弯…… 题意 输入 $k$,要求构造一个 $n\times n$ 的矩阵($n$ 自选),使得恰好用 $k$ 中颜色把每个点都染色,并且同一种颜色的格子周围 相邻的每种颜色数量 ...
- AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...
随机推荐
- windows下安装hexo和生成博客
首先在电脑上安装node和git,这个只要在相关官网的下载然后一步安装即可. 然后在你的电脑上新建一个文件夹,用来存放你的博客文件,比如创建hexo 进入该文件,右键打开git bash 安装hexo ...
- 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 ...
- CSS中常见的布局
一.css中常见的布局有哪些? (1)两列布局 (2)三列布局 (3)弹性布局 (4)圣杯布局 (5)双飞翼布局 二.具体实现 (1)两列布局 https://www.cnblogs.com/qin ...
- excel2016打开为空白界面解决办法
前言 excel2016打开文件为空白的界面,明显不正常. 解决方法 https://blog.csdn.net/b2345012/article/details/94134401 以上.
- day34-python之进程调用
1.信号量 import threading,time class myThread(threading.Thread): def run(self): if semaphore.acquire(): ...
- [ipsec][crypto] ike/ipsec与tls的认证机制比较
前言 接上篇:[ipsec][crypto] 有点不同的数字证书到底是什么 本篇内容主要是上一篇内容的延伸.抽象的从概念上理解了证书是什么之后,我们接下来 从实践的角度出发,以IKEv2和TLS两个协 ...
- sqlite3入门之sqlite3_open,sqlite3_exec,slite3_close
sqlite3_open sqlite3_open函数原型: int sqlite3_open( const char *filename, /* Database filename (UTF-8) ...
- git修改提交历史中的author信息
当次提交 当次的提交显示指定提交者信息: git commit -m "Initial commit" --author="mn <mn@furzoom.com&g ...
- 隐藏按钮button
<td> <input id="del" type="button" value="删除" onclick="u ...
- 剑指offer:二维数组的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...