zoukankan      html  css  js  c++  java
  • bzoj2431

    dp

    设dp[i][j]为当前到了第i个位置,一共出现了j个逆序对,然后考虑转移,因为前面只有i-1个数,所以这位只能产生[0,i-1]对逆序对,那么dp[i][j]=sigma(dp[i-1][k]),k∈[i-j+1,j]。

    但是这样直接搞是O(n^3)的,那么我们用前缀和优化一下,就是O(n^2)的了。

    碰见这种排列的题不能直接把现在填的数设为状态,我们可以通过调整之前填的数使当前要放进去的数是合法的,所以之前的数就是不确定的,应该考虑用其他条件去设状态

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1010; 
    namespace IO 
    {
        const int Maxlen = N * 50;
        char buf[Maxlen], *C = buf;
        int Len;
        inline void read_in()
        {
            Len = fread(C, 1, Maxlen, stdin);
            buf[Len] = '';
        }
        inline void fread(int &x) 
        {
            x = 0;
            int f = 1;
            while (*C < '0' || '9' < *C) { if(*C == '-') f = -1; ++C; }
            while ('0' <= *C && *C <= '9') x = (x << 1) + (x << 3) + *C - '0', ++C;
            x *= f;
        }
        inline void read(int &x)
        {
            x = 0;
            int f = 1; char c = getchar();
            while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
            while(c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); }
            x *= f;
        }
        inline void read(long long &x)
        {
            x = 0;
            long long f = 1; char c = getchar();
            while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
            while(c >= '0' && c <= '9') { x = (x << 1ll) + (x << 3ll) + c - '0'; c = getchar(); }
            x *= f;
        } 
    } using namespace IO;
    int n, k;
    int dp[N][N];
    int main()
    {
        read(n);
        read(k);
        for(int i = 0; i <= k; ++i) dp[0][i] = 1;
        for(int i = 1; i <= n; ++i)
            for(int j = 0; j <= k; ++j)
            {
                dp[i][j] = dp[i - 1][j];
                if(j >= i) dp[i][j] = dp[i][j] - dp[i - 1][j - i];
                if(j) dp[i][j] = dp[i][j] + dp[i][j - 1];
                if(dp[i][j] >= 10000) dp[i][j] -= 10000;
                if(dp[i][j] < 0) dp[i][j] += 10000;
            }
        if(k == 0) puts("1");
        else printf("%d
    ", ((dp[n][k] - dp[n][k - 1]) % 10000 + 10000) % 10000);   
        return 0;
    } 
    View Code
  • 相关阅读:
    Python 操作 MySQL 的5种方式
    ffiddler抓取手机(app)https包
    Git远程操作详解(clone、remote、fetch、pull、push)
    Mysql学生课程表SQL面试集合
    Python获取list中指定元素的索引
    python找出字典中value最大值的几种方法
    python sort、sorted高级排序技巧
    JMeter之BeanShell常用内置对象
    Docker从容器拷贝文件到宿主机或从宿主机拷贝文件到容器
    编程必备基础知识|计算机组成原理篇(10):输入输出设备
  • 原文地址:https://www.cnblogs.com/19992147orz/p/7552463.html
Copyright © 2011-2022 走看看