【leetcode dp】629. K Inverse Pairs Array
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- 【树形DP】codeforces K. Send the Fool Further! (medium)
http://codeforces.com/contest/802/problem/K [题意] 给定一棵树,Heidi从根结点0出发沿着边走,每个结点最多经过k次,求这棵树的最大花费是多少(同一条边 ...
- 【leetcode dp】Dungeon Game
https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 【LeetCode 60】第k个排列
题目链接 [题解] 逆康托展开. 考虑康托展开的过程. K = ∑v[i]*(n-i)! 其中v[i]表示在a[i+1..n]中比a[i]小的数字的个数 (也即未出现的数字中它排名第几(从0开始)) ...
- 【LeetCode 23】合并K个排序链表
题目链接 [题解] 会归并排序吧? 就把这K个链表当成是K个数字就好. 然后做归并排序. 因为归并排序的时候本来就会有这么一个过程. [l..mid]和[mid+1..r]这两段区间都是有序的了已经. ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
随机推荐
- Azure 项目构建 – 构建直播教学系统之媒体服务篇
本课程主要介绍如何在 Azure 平台上快速构建和部署基于 Azure 媒体服务的点播和直播教学系统, 实践讲解如何使用 Azure 门户创建媒体服务, 配置视频流进行传输,连接 CDN 加速等. 具 ...
- jmeter并发定时器
jmeter并发定时器
- 博客高亮代码及使用OpenLiveWriter修改之前博客
简述: 最近查阅前辈资料的时候,看到写的博客很有条理,回头看下自己的乱做麻花,然后来时研究: 他们的代码看起来很漂亮然后我就查资料,在网页版上一直没法出来像他们的格式,后查资料看来的使用客户端工具才 ...
- Socket通讯简易学习
Socket打开通信通道,告诉本地机器,愿意在该通道上接受客户请求——监听,等待客户请求——接受请求,创建专用链接进行读写——处理完毕,关闭专用链接——关闭通信通道(当然其中监听到关闭专用链接可以重复 ...
- CS193p Lecture 9 - Animation, Autolayout
Animation(动画) Demo Dropit续 Autolayout(自动布局) 三种添加自动布局的方法: 使用蓝色辅助虚线,右键选择建议约束(Reset to Suggested Constr ...
- ios 序列化
1到底这个序列化有啥作用? 面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中(来源于某教材的一 ...
- git 的.gitignore
# vs code .vscode # Logs *.log #npm node_modules
- 51nod 1265 四点共面——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1265 以其中某一点向其它三点连向量,若四点共面,这三个向量定义的平行六面体 ...
- Shell中各种括号的作用
一.小括号,圆括号() 1.单小括号 () ① 命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有分号 ...
- fshc之请求仲裁机制(from mcu and cache)
1.arbiter模块本身放在sclk时钟域,但是输入都是来之HCLK时钟域. 2.当MCU/CACHE访问FSHC时,FSHC不接受其他请求,FSHC只可以同时处理一个请求的操作. 3.如果原子操作 ...