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. Azure 项目构建 – 构建直播教学系统之媒体服务篇

    本课程主要介绍如何在 Azure 平台上快速构建和部署基于 Azure 媒体服务的点播和直播教学系统, 实践讲解如何使用 Azure 门户创建媒体服务, 配置视频流进行传输,连接 CDN 加速等. 具 ...

  2. siege4安装和使用介绍

    使用文档参考地址:https://www.joedog.org/siege-manual/ siege4地址:http://download.joedog.org/siege/ cd /usr/loc ...

  3. 清空iptables

    /sbin/iptables -P INPUT ACCEPT /sbin/iptables -F iptables -L

  4. (转)SpringMVC学习(三)——SpringMVC的配置文件

    http://blog.csdn.net/yerenyuan_pku/article/details/72231527 读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后 ...

  5. 模拟水题之unique两行AC

    https://icpc.njust.edu.cn/Contest/749/A/ Description 小鱼喜欢吃糖果.他有两盒糖果,两盒糖果分别仅由小写字母组成的字符串s和字符串t构成.其中'a' ...

  6. Asp.Net Core 进阶(二) —— 集成Log4net

    Asp.Net Core 支持适用于各种内置日志记录API,同时也支持其他第三方日志记录.在我们新建项目后,在Program 文件入口调用了CreateDefaultBuilder,该操作默认将添加以 ...

  7. 一个典型的flex布局,兼容性比较好

    html 代码: <body class="flex-wrap col-flex"> <header class="midCenter flex-wra ...

  8. 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?(转)

    作者:Wang Namelos 链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎 解答这个问题并不困难:唯一的要求是你熟悉 ...

  9. Shift-Invariant论文笔记

    ICML 2019 Making Convolutional Networks Shift-Invariant Again ICML 2019 Making Convolutional Network ...

  10. Linux C++/C开发所必需的一系列工具

    系统平台下的开发工具.开发环境各有不同.Linux C++/C开发所必需的一系列工具: 1. vi(vim)文本编辑器一个UNIX世界标准的文本编辑器,简约而强大,不论作为开发人员还是系统管理员,熟练 ...