当前位置:网站首页>2021上海市赛-D-卡特兰数变种,dp
2021上海市赛-D-卡特兰数变种,dp
2022-07-25 15:23:00 【塔子哥来了】
题目大意:
给你 n n n个数,让你分成两排长度为n/2的数组 a i , b i a_i,b_i ai,bi.然后使得两个数组有序且 a i ≤ b i a_i\leq b_i ai≤bi.
n ≤ 5 e 3 n \leq 5e3 n≤5e3
题目思路:
如果数两两不同的话,就是一个卡特兰数.数有重复,需要dp计算。
先考虑如何 d p dp dp的求两两不同的情况:
将其转化为括号序列。第一排看成左括号的下标,第二排看出右括号的下标。
任意一个前缀状态中左括号的数量不得少于右括号的数量.
这样 d p ( i , j ) dp(i,j) dp(i,j)为前i个数,左括号比右括号多j个的方案数.
考虑相同的情况:只需要按数的种类从小到大枚举。再多枚举一个 <上面放几个该数>。转移的时候再乘上一个阶乘即可。
因为这么放,两种放法一定对应不同的该数的分布,所以分步乘法,乘上阶乘即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
const int maxn = 5e3 + 5;
const int mod = 998244353;
int a[maxn];
ll dp[maxn][maxn] , f[maxn];
unordered_map<int,int> s;
int main()
{
f[0] = 1;
for (int i = 1 ; i < maxn ; i++)
f[i] = f[i - 1] * i % mod;
ios::sync_with_stdio(false);
int n; cin >> n;
for (int i = 1 ; i <= n ; i++){
cin >> a[i];
s[a[i]]++;
}
sort(a + 1 , a + 1 + n);
int m = unique(a + 1 , a + 1 + n) - a - 1;
dp[0][0] = 1;
for (int i = 1 ; i <= m ; i++){
for (int j = 0 ; j <= n ; j++){
int num = s[a[i]];
for (int k = 0 ; k <= num ; k++){
int l = j - 2 * k + num;
if (l < 0) continue;
dp[i][j] = (dp[i][j] + dp[i - 1][l] * f[num] % mod) % mod;
}
}
}
cout << dp[m][0] << endl;
return 0;
}
边栏推荐
- Graph theory and concept
- ML - 自然语言处理 - 基础知识
- Recommend 10 learning websites that can be called artifact
- 在网页上实现任意格式的音视频快速播放功能的开发总结。
- Outline and box shadow to achieve the highlight effect of contour fillet
- 如何更新更新数据库中的json值?
- JVM garbage collector details
- Spark judges that DF is empty
- 记一次Spark foreachPartition导致OOM
- Submarine cable detector tss350 (I)
猜你喜欢

Reflection - Notes

Image cropper example

Yan required executor memory is above the max threshold (8192mb) of this cluster!

理解“平均负载”

MySql的安装配置超详细教程与简单的建库建表方法

获取键盘按下的键位对应ask码

Use the command to check the WiFi connection password under win10 system

ML - Speech - advanced speech model

ML - natural language processing - Key Technologies

Idea remotely submits spark tasks to the yarn cluster
随机推荐
spark分区算子partitionBy、coalesce、repartition
记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
Xcode添加mobileprovision证书文件报错:Xcode encountered an error
ML - 自然语言处理 - 关键技术
Outline and box shadow to achieve the highlight effect of contour fillet
获取键盘按下的键位对应ask码
Spark sql 常用时间函数
Idea护眼色设置
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
Es5 thinking of writing inheritance
Example of password strength verification
Spark获取DataFrame中列的方式--col,$,column,apply
MATLAB读取显示图像时数据格式转换原因
redis淘汰策列
Hbck 修复问题
BPSK调制系统MATLAB仿真实现(1)
Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
ios 面试题
记一次Spark foreachPartition导致OOM
HBCK fix problem