https://leetcode.com/problems/k-inverse-pairs-array/description/

【题意】

给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0<=k<=1000, 1<=n<=1000,答案模1e9+7

【思路】

dp[i][j]表示正好有j个逆序对的长度为i的序列的方案数,最终我们要求的就是dp[n][k]

考虑dp[i+1][j]和dp[i][j]的关系,长度为i+1的序列相当于在长度为i的序列中插入一个数,那么有

xxxx#

xxx#x

xx#xx

x#xxx

#xxxx

则dp[i+1][j]=dp[i-1][j]+dp[i-1][j-1]+dp[i-1][j-2]+...+dp[i-1][j-(i-1)]

这样写的转移复杂度比较大,会T

可以优化一下,考虑dp[i][j+1]和dp[i][j]的关系,可以得到dp[i][j]=dp[i][j-1]+dp[i][j-1]-dp[i-1][j-i]

【Accepted】

 class Solution {
public:
int kInversePairs(int n, int k) {
if(k==) return ;
const int maxn=1e3+;
const int mod=1e9+;
int dp[maxn][maxn];
memset(dp,,sizeof(dp));
for(int i=;i<maxn;i++) dp[i][]=;
for(int i=;i<=n;i++){
for(int j=;j<=min(k,i*(i-)/);j++){
dp[i][j]=(dp[i][j]+dp[i][j-])%mod;
dp[i][j]=(dp[i][j]+dp[i-][j])%mod;
if(j>=i)
dp[i][j]=(dp[i][j]-dp[i-][j-i]+mod)%mod;
}
}
return dp[n][k]%mod;
}
};

【leetcode dp】629. K Inverse Pairs Array的更多相关文章

  1. 629. K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  2. [LeetCode] K Inverse Pairs Array K个翻转对数组

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  3. [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  4. 【树形DP】codeforces K. Send the Fool Further! (medium)

    http://codeforces.com/contest/802/problem/K [题意] 给定一棵树,Heidi从根结点0出发沿着边走,每个结点最多经过k次,求这棵树的最大花费是多少(同一条边 ...

  5. 【leetcode dp】Dungeon Game

    https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...

  6. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  7. 【LeetCode 60】第k个排列

    题目链接 [题解] 逆康托展开. 考虑康托展开的过程. K = ∑v[i]*(n-i)! 其中v[i]表示在a[i+1..n]中比a[i]小的数字的个数 (也即未出现的数字中它排名第几(从0开始)) ...

  8. 【LeetCode 23】合并K个排序链表

    题目链接 [题解] 会归并排序吧? 就把这K个链表当成是K个数字就好. 然后做归并排序. 因为归并排序的时候本来就会有这么一个过程. [l..mid]和[mid+1..r]这两段区间都是有序的了已经. ...

  9. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

随机推荐

  1. HDU 4291 A Short problem 短问题 (递推,找规律)

    题意: 给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路: 要求的g( g( g(n ...

  2. UVA 427 The Tower of Babylon 巴比伦塔(dp)

    据说是DAG的dp,可用spfa来做,松弛操作改成变长.注意状态的表示. 影响决策的只有顶部的尺寸,因为尺寸可能很大,所以用立方体的编号和高的编号来表示,然后向尺寸更小的转移就行了. #include ...

  3. js 两个数组进行去重处理,返回去重后的数组

    1.去重的方法为: array_diff(a, b) { for (var i = 0; i < b.length; i++) { for (var j = 0; j < a.length ...

  4. maven项目创建(eclipse配置

    Eclipse相关配置: eclipse 设置默认编码为Utf-8 需要设置的几处地方为: Window --> Preferences --> General --> Conten ...

  5. numpy中tile函数

    tile函数位于python模块numpy.lib.shape_base中,他的功能是重复某个数组. 函数的形式是tile(A,reps) 函数参数说明中提到A和reps都是array_like的,什 ...

  6. CS193p Lecture 8 - Protocols, Blocks and Animation

    一.协议(Protocols) 1. 声明协议 @protocol Foo <Xyzzy, NSObject> // ... @optinal // @required //... @en ...

  7. 响应式web设计视图工具及插件总结----20150113

    响应式web设计可以说火不火是迟早的,下面就对于最开始的视口调试的方法汇总,希望有好的方法大家一起交流. 1.火狐:从Firefox升级到29.0之后就不直接支持Firesizer了. 先安装Add- ...

  8. centos7内核优化

    #sysctl -p 参数: net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1net.ipv4.icmp ...

  9. kvm虚拟迁移

    1. 虚拟迁移 迁移: 系统的迁移是指把源主机上的操作系统和应用程序移动到目的主机,并且能够在目的主机上正常运行.在没有虚拟机的时代,物理机之间的迁移依靠的是系统备份和恢复技术.在源主机上实时备份操作 ...

  10. vue-music:歌词的其他功能

    由于歌词的播放需要歌曲播放,切换歌曲,歌曲的播放模式等等有关联,因此,需要在这几处处理相关问题 1.循环播放回不到开始位置 loop() { this.$refs.audio.currentTime ...