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$ ...
随机推荐
- (转)WEB服务器_IIS配置优化指南
原文地址:https://www.cnblogs.com/heyuquan/p/deploy-iis-set-performance-guide.html 通常把站点发布到IIS上运行正常后,很少会去 ...
- PHP 的一款http请求封装类
<?php namespace hisi; class Http { protected static $userAgent = [ 'Mozilla/5.0 (Windows NT 6.1; ...
- springboot超级详细的日志配置(基于logback)
前言 java web 下有好几种日志框架,比如:logback,log4j,log4j2(slj4f 并不是一种日志框架,它相当于定义了规范,实现了这个规范的日志框架就能够用 slj4f 调用) ...
- 某位前辈的Image识图,,有点意思,先留存
import PIL from PIL import Image def get_bin_table(threshold=155): ''' 获取灰度转二值的映射table 0表示黑色,1表示白色 ' ...
- MySQL Replication--开启GTID模式下匿名事务异常
错误环境: OS: CentOS release 6.5 (Final) MySQL: MySQL 5.7.19 主从参数配置: master_info_repository = TABLE rela ...
- git学习记录--标签随笔
创建标签: 命令git tag <name>用于新建一个标签,默认为HEAD,也可以指定一个commit id: git tag -a <tagname> -m "b ...
- SpringCloud2.0 Turbine 断路器集群监控 基础教程(九)
1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...
- java基础(7)---IO流
一.FileWriter 导包:import java.io.FileWriter 覆盖写入: 追加写入: 写数据换行: write方法重载: 二.编码: 三.FileReader: read重载 ...
- Java精通并发-synchronized关键字原理详解
关于synchronized关键字原理其实在当时JVM的学习[https://www.cnblogs.com/webor2006/p/9595300.html]中已经剖析过了,这里从研究并发专题的角度 ...
- 【Python学习】Python3 基本数据类型
参考学习地址:https://www.runoob.com/python3/python3-data-type.html Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用 ...