ARC118E Avoid Permutations
题意
给定一个长度为 \(n\) 的排列 \(p\),在一个 \((n + 2)\times(n + 2)\) 的网格上,禁止通过 \((i, p_i)\) 这些点,每次只能向上或右走一格,从 \((0, 0)\) 走到 \((n + 1, n + 1)\) 的方案数,定义为排列的权值。给定一个不完整的排列,对于所有补全排列的方案,计算权值和。
数据范围:\(1\le n \le 200\)。
做法
这题并不需要容斥。
考虑计算对于某一条路径,有多少排列满足没有点在路径上。那么正常 DP 路径,在 \((i, j)\) 这个点向上转移到 \((i, j + 1)\) 的时候决策 \(y = j + 1\) 这一行左侧是否放置一个物品、放置在哪个位置,同时记录当前位置左下角区域总共放置了多少障碍。DP 状态设计为 \(f_{i, j, k, 0/1, 0/1}\) 表示在 \((i, j)\) 左下角放了 \(k\) 个障碍,这一行左边是不是已经放了东西,这一列下面是不是已经放了东西。转移需要一些讨论。时间复杂度 \(\Theta(n^3)\)。
代码
// Author: kyEEcccccc
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define F(i, l, r) for (int i = (l); i <= (r); ++i)
#define FF(i, r, l) for (int i = (r); i >= (l); --i)
#define MAX(a, b) ((a) = max(a, b))
#define MIN(a, b) ((a) = min(a, b))
#define SZ(a) ((int)((a).size()) - 1)
const int N = 205, MOD = 998244353;
int n, p[N], q[N];
int toti[N], totj[N];
LL f[N][N][N][2][2];
signed main(void)
{
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(nullptr);
cin >> n;
F(i, 1, n)
{
cin >> p[i];
if (p[i] != -1) q[p[i]] = i;
}
F(i, 1, n)
{
toti[i] = toti[i - 1];
totj[i] = totj[i - 1];
if (p[i] <= 0) toti[i] += 1;
if (q[i] <= 0) totj[i] += 1;
}
toti[n + 1] = toti[n];
totj[n + 1] = totj[n];
F(i, 0, n + 1) f[i][0][0][0][0] = f[0][i][0][0][0] = 1;
F(i, 1, n + 1) F(j, 1, n + 1) F(k, 0, min(toti[i], totj[j])) F(a, 0, 1) F(b, 0, 1)
{
LL &res = f[i][j][k][a][b];
if (((a == 1 && p[i] <= 0) || (b == 1 && q[j] <= 0)) && k == 0) continue;
if ((i == n + 1 && a == 1) || (j == n + 1 && b == 1)) continue;
if (p[i] > 0 && p[i] == j) continue;
if (p[i] > 0 && ((p[i] < j && a == 0) || (p[i] > j && a == 1))) continue;
if (q[j] > 0 && ((q[j] < i && b == 0) || (q[j] > i && b == 1))) continue;
if (a == 0)
{
res = (res + f[i - 1][j][k][0][b] + f[i - 1][j][k][1][b]) % MOD;
}
else
{
if (p[i] > 0)
{
res = (res + f[i - 1][j][k][0][b] + f[i - 1][j][k][1][b]) % MOD;
}
else if (q[j] <= 0)
{
res = (res + (f[i - 1][j][k - 1][0][b] + f[i - 1][j][k - 1][1][b])
* (totj[j - 1] - k + 1 + b)) % MOD;
}
else
{
res = (res + (f[i - 1][j][k - 1][0][b] + f[i - 1][j][k - 1][1][b])
* (totj[j - 1] - k + 1)) % MOD;
}
}
if (b == 0)
{
res = (res + f[i][j - 1][k][a][0] + f[i][j - 1][k][a][1]) % MOD;
}
else
{
if (q[j] > 0)
{
res = (res + f[i][j - 1][k][a][0] + f[i][j - 1][k][a][1]) % MOD;
}
else if (p[i] <= 0)
{
res = (res + (f[i][j - 1][k - 1][a][0] + f[i][j - 1][k - 1][a][1])
* (toti[i - 1] - k + 1 + a)) % MOD;
}
else
{
res = (res + (f[i][j - 1][k - 1][a][0] + f[i][j - 1][k - 1][a][1])
* (toti[i - 1] - k + 1)) % MOD;
}
}
}
cout << f[n + 1][n + 1][toti[n]][0][0] << endl;
return 0;
}
ARC118E Avoid Permutations的更多相关文章
- 47. Permutations II (JAVA)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- String源码中的"avoid getfield opcode"
引言: 之前一篇文章梳理了String的不变性原则,还提到了一段源码中注释"avoid getfield opcode",当时通过查阅资料发现,这是为了防止 getfield(获取 ...
- 10 Biggest Business Mistakes That Every Entrepreneur Should Avoid
原文链接:http://www.huffingtonpost.com/syed-balkhi/10-biggest-business-mista_b_7626978.html When I start ...
- Permutations
Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- rocketmq-spring : 实战与源码解析一网打尽
RocketMQ 是大家耳熟能详的消息队列,开源项目 rocketmq-spring 可以帮助开发者在 Spring Boot 项目中快速整合 RocketMQ. 这篇文章会介绍 Spring Boo ...
- lua快速入门~在js基础上,知道Lua 和 Js 的不同即可
lua 和 javaScript 差不多的,就是一些语法的细节不同,学过js,再注意一下下面的细节,就能上手了~ 快速入门,可以直接看一下菜鸟教程的lua:https://www.runoob.com ...
- python之pil图像库操作
Image模块Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内.如open.save.conver.show-等功能. open类Image.o ...
- [数据库/MYSQL]MYSQL开启Bin-Log
1 概述: MYSQL数据库的二进制日志----bin log 什么是二进制日志(binlog)? MySQL的二进制日志binlog,可以说是MySQL最重要的日志,它记录了所有的DDL和DML语句 ...
- 【Vue】Vuex
Vuex简介 概念: 专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中管理(读.写),也是一种适用于任意组件间的通信方式. 什么时候用Vuex ①多个 ...
- 如何确定 this 指向?改变 this 指向的方式有哪些?
this 指向: 1. 全局上下文(函数外) 无论是否为严格模式,均指向全局对象.注意:严格模式下全局对象为undifined 2. 函数上下文(函数内) 默认的,指向函数的调用对象,且是最直接的调用 ...
- React Native组件(二)
一.创建一个项目 1.1.找到目标目录cmd命令,请尽量不要有中文路径 npx react-native init reactnative03 进入文件 cd reactnative03 启动 npx ...
- 通过命令快速找到python的路径
查询Python 首先我们需要知道Python安装路径,可以在命令行中逐行执行下面代码 python3 import sys sys.executable
- Redis篇一之基础数据结构
文章目录 Redis的数据结构 String类型**** Hash类型 List类型 Set类型 SortedSet类型 BitMap类型 HyperLogLog 总结 Redis诞生于2009年全称 ...
- C++ | 类继承
1. 概述 C++有3种继承方式:公有继承(public).保护继承(protected).私有继承(private). 一个B类继承于A类,或称从类A派生类B.这样的话,类A称为基类(父类),类B称 ...