【UOJ #204】【APIO 2016】Boat

http://uoj.ac/problem/204
肯定要离散化的,先离散化出\(O(n)\)个取值区间。
\(f(i,j)\)表示第\(i\)所学校派出的划艇数量在\(j\)区间中。
\(f(i,j)=\sum\limits_{k=0}^{i-1}\left(\sum\limits_{t=1}^{j-1}f(k,t)\right)\times Cal(k+1,i,j)\)
\(Cal(l,r,j)\)表示\([l,r)\)中的每所学校要不然不派出划艇,要不然派出数量在\(j\)区间中的划艇,第\(r\)所学校一定要派出数量在\(j\)区间中的划艇,且满足划艇数递增的方案个数。
假设\([l,r)\)中只有\(m\)所学校能满足派出数量在\(j\)区间中的划艇,设\(j\)区间的大小为\(l\),那么\(Cal(l,r,j)=\sum\limits_{i=1}^{m+1}{l\choose i}\times{m\choose i-1}={l+m\choose m+1}\)
利用这个组合数,再记录一下dp的前缀和,时间复杂度\(O(n^3)\)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

const int N = 503;
const int p = 1000000007;

int l[N], a[N], b[N], H[N << 1], cnt = 0, n, f[N][N << 1], ni[N];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", a + i, b + i);
		H[++cnt] = a[i];
		H[++cnt] = ++b[i];
	}
	
	stable_sort(H + 1, H + cnt + 1);
	cnt = unique(H + 1, H + cnt + 1) - H;
	
	for (int i = 1; i <= n; ++i) {
		a[i] = lower_bound(H + 1, H + cnt, a[i]) - H;
		b[i] = lower_bound(H + 1, H + cnt, b[i]) - H;
	}
	
	cnt -= 2;
	for (int i = 1; i <= cnt; ++i)
		l[i] = H[i + 1] - H[i];
	
	ni[1] = 1;
	for (int i = 2; i <= n; ++i)
		ni[i] = 1ll * (p - p / i) * ni[p % i] % p;
	
	for (int i = 0; i <= cnt; ++i) f[0][i] = 1;
	for (int i = 1; i <= n; ++i) {
		for (int j = a[i], up = b[i]; j < up; ++j) {
			int C = l[j], r = l[j], c = 1;
			for (int k = i - 1; k >= 0; --k) {
				(f[i][j] += 1ll * f[k][j - 1] * C % p) %= p;
				if (a[k] <= j && j < b[k]) {
					++r; ++c;
					C = 1ll * C * r % p * ni[c] % p;
				}
			}
		}
		
		for (int j = 2; j <= cnt; ++j)
			(f[i][j] += f[i][j - 1]) %= p;
	}
	
	int ans = 0;
	for (int i = 1; i <= n; ++i) (ans += f[i][cnt]) %= p;
	printf("%d\n", ans);
	return 0;
}
posted @ 2017-04-21 09:18  abclzr  阅读(534)  评论(0编辑  收藏  举报