【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的序列中插入一个数,那么有 xxx…
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. We define an inverse pair as following: For ith and jth element in the array, if i < j and a[i] > a[j] then it's a…
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. We define an inverse pair as following: For ith and jth element in the array, if i < j and a[i] > a[j] then it's a…
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. We define an inverse pair as following: For ith and jthelement in the array, if i < j and a[i] > a[j] then it's an…
http://codeforces.com/contest/802/problem/K [题意] 给定一棵树,Heidi从根结点0出发沿着边走,每个结点最多经过k次,求这棵树的最大花费是多少(同一条边走n次花费只算一次) [思路] 对于结点v: 如果在v的某棵子树停下,那么可以“遍历”k棵子树(有的话) 如果还要沿着v返回v的父节点p,那么只能“遍历”k-1棵子树(有的话). 用dp[v][1]表示第一种情况,dp[v][0]表示第二种情况:最后要求的就是dp[0][0]. 1. 对于dp[v]…
https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步只能往右或往下走一格.每个格子是一个整数,负数代表生命值减掉相应分数,正数表示生命值增加相应分值,要保证王子在走的过程中不挂掉,即经过每个格子后的生命值大于等于1,问王子最初的生命值最少是多少. [思路] 倒着推dp,计算(i,j)->(m,n)的可生存血量. dp[i][j]=max(1,min(…
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个子串都是回文串 [思路] 求一个字符串的所有子串是否是回文串,O(n^2) dp从后往前推 vector<vector<bool> > p(len,vector<bool>(len)); ;i<len-;i++){ ;j<len-;j++){ if(j!=i)…
题目链接 [题解] 逆康托展开. 考虑康托展开的过程. K = ∑v[i]*(n-i)! 其中v[i]表示在a[i+1..n]中比a[i]小的数字的个数 (也即未出现的数字中它排名第几(从0开始)) 那么我们在逆康托展开的时候,就可以通过直接除(n-i)!得到每个数字的v[i]的值. 然后通过给已经出现的数字打tag. 剩下的问题就转化为找未出现的第v[i]个数字了. 注意康托展开的值是比当前序列小的序列的个数. 所以如果要找序号为k的序列的话,实际上应该找k-1对应的逆康托序列 [代码] cl…
题目链接 [题解] 会归并排序吧? 就把这K个链表当成是K个数字就好. 然后做归并排序. 因为归并排序的时候本来就会有这么一个过程. [l..mid]和[mid+1..r]这两段区间都是有序的了已经. 然后再把他们俩合并起来. 合并起来之后把这个链表直接放在这个区间的最左边那个位置就好 上一级的合并要用的时候就直接取这个区间最左边那个链表. 有个人关于时间复杂度的证明说的挺好的贴一下 [代码] /** * Definition for singly-linked list. * struct L…
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the val…