Sort It (树状数组+dp)

主页讨论版问题名次状态统计

请参赛队员将NickName修改为以下格式:队员1_队员2_队员3

问题 F: Sort It

时间限制: 6 Sec   内存限制: 128 MB
提交: 30   解决: 8
[ 提交][ 状态][ 讨论版]

题目描述

You are given a permutation of length n:p1,p2,...pn.Consider arrays of length n with possibly equal number from 1 to n.

If after rearrange elements of the array in the order of “all numbers equal to p1,all numbers equal to p2...all numbers equal to pn”,

the array become non-decreasing,then we call the array is sortable by p.

Calculate the total number of arrays that are sortable by p.

As the answer can be very large,output it after module 1e9+7.

输入

Several test cases.

The first line contains a single integer n(1<=n<=2000),the length of permutation.

The second line contains n distinct integers p1,p2,..pn(1<=pi<=n).

输出

Output a singe number -- the answer to the problem module 1e9+7.

样例输入

22 132 1 3

样例输出

215

提示

[ 提交][ 状态][ 讨论版]

/*
给一个排列p,问有多少个长度为n的序列(都是1到n的数)可以用p排序,p排序定义为先将序列中p1的取出来,再取p2,一直取到pn,
可以发现一个序列能否被p排序,只和序列出现了那些数有关,具体的来说就是序列中出现的数必须能在排列中找到这个LIS,
f[i]表示用i个数构成n个数的排列有多少个,g[i]表示长度为i的LIS有多少种,则答案是f[i]*g[i]的和,代码如下
 
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3 + 10;
const int mod = 1e9 + 7;
int f[maxn][maxn],n;
int p[maxn],g[maxn][maxn];
int dp[maxn][maxn];
void init(int n)
{
    memset(f,0,sizeof f);
    f[0][0] = 1;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= i; ++j) {
            f[i][j] = (f[i-1][j-1]+f[i-1][j])*(j+0LL) % mod;
        }
    }
}
int lowbit(int x)
{
    return x & -x;
}
void add(int *c,int x,int d,int n)
{
    for(; x <= n; x += lowbit(x)) c[x] = (c[x] + d) % mod;
}
int query(int *c,int x)
{
    int retVal = 0;
    for(;x > 0; x -= lowbit(x)) retVal = (retVal + c[x]) % mod;
    return retVal;
}

int main()
{
    init(2000);
    int n;
    while(scanf("%d",&n)==1) {
        for(int i = 1; i <= n; ++i) scanf("%d",p+i);
        memset(g,0,sizeof g);
        for(int i = 1; i <= n; ++i) {
            int x = p[i];
            for(int j = 1; j <= i; ++j) {
                int ret = (j == 1 ? 1 : query(g[j-1],x-1));
                add(g[j],x,ret,n);
                g[0][j] += ret;
                g[0][j] %= mod;
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; ++i) {
            int add = g[0][i]*(f[n][i]+0LL) % mod;
            ans = (ans + add) % mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值