码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 2431 [HAOI2009]逆序对数列

时间:2018-02-20 18:23:56      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:return   题解   scanf   ace   end   ios   pre   bzoj   逆序对数   

题解:DP

f[i][j]表示1~i形成逆序对j对的方案数

转移用前缀和优化

O(nk)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1009;
const int mm=10000;

int n,m;

int f[maxn][maxn];
int sum[maxn][maxn];

int main(){
	scanf("%d%d",&n,&m);
	
	f[1][0]=1;
	for(int i=2;i<=n;++i){
		for(int j=m;j>=0;--j)sum[i-1][j]=(sum[i-1][j+1]+f[i-1][j])%mm;
		for(int j=0;j<=m;++j){
			int u=j;
			int d=max(j-i+1,0);
			f[i][j]=(sum[i-1][d]-sum[i-1][u+1]+mm)%mm;
		}
	}
	
	cout<<f[n][m]<<endl;
	return 0;
}

  

BZOJ 2431 [HAOI2009]逆序对数列

标签:return   题解   scanf   ace   end   ios   pre   bzoj   逆序对数   

原文地址:https://www.cnblogs.com/zzyer/p/8455478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!