DPT Permutation

题意

给定 \(S \in ['>', '<']\)。表示排列 \(P\) 两点之间的大小关系。

求排列 \(P\) 的方案数。

Sol

排列方案,考虑 \(f_{i, j}\) 表示第 \(i\) 位的数在排列中排名为 \(j\) 的方案数。

  • \(S_i = '>'\)\(f_{i, j} = \sum_{k = 1} ^ {j - 1} f_{i - 1, k}\)
  • \(S_i = '<'\)\(f_{i, j} = \sum_{k = j} ^ {i - 1} f_{i - 1, k}\)

预处理是 \(trivial\) 的。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
string read_() {
	string ans;
	char c = getchar();
	while (c != '<' && c != '>')
		c = getchar();
	while (c == '<' || c == '>') {
		ans += c;
		c = getchar();
	}
	return ans;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 3005, mod = 1e9 + 7;

array <array <int, N>, N> f;
array <int, N> g;

void Mod(int &x) {
	if (x >= mod) x -= mod;
	if (x < 0) x += mod;
}
int main() {
	int n = read();
	string s = " " + read_();
	f[1][1] = 1;
	for (int i = 1; i <= n; i++) g[i] = 1;
	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= i; j++) {
			if (s[i - 1] == '<') f[i][j] = g[j - 1];
			else f[i][j] = g[i - 1] - g[j - 1];
			Mod(f[i][j]);
		}
		for (int j = 1; j <= i; j++)
			g[j] = g[j - 1] + f[i][j], Mod(g[j]);
	}
	write(g[n]), puts("");
	return 0;
}
posted @ 2023-11-23 09:03  cxqghzj  阅读(4)  评论(0编辑  收藏  举报