【bellovin】LIS+dp
2017-08-10 10:44
141 查看
Peter has a sequence a1,a2,…,ana1,a2,…,an and he define a function on the sequence – F(a1,a2,…,an)=(f1,f2,…,fn)F(a1,a2,…,an)=(f1,f2,…,fn), where fifi is the length of the longest increasing subsequence ending with aiai.
Peter would like to find another sequence b1,b2,…,bnb1,b2,…,bn in such a manner that F(a1,a2,…,an)F(a1,a2,…,an) equals to F(b1,b2,…,bn)F(b1,b2,…,bn). Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.
The sequence a1,a2,…,ana1,a2,…,an is lexicographically smaller than sequence b1,b2,…,bnb1,b2,…,bn, if there is such number ii from 11 to nn, that ak=bkak=bk for 1≤k
Peter would like to find another sequence b1,b2,…,bnb1,b2,…,bn in such a manner that F(a1,a2,…,an)F(a1,a2,…,an) equals to F(b1,b2,…,bn)F(b1,b2,…,bn). Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.
The sequence a1,a2,…,ana1,a2,…,an is lexicographically smaller than sequence b1,b2,…,bnb1,b2,…,bn, if there is such number ii from 11 to nn, that ak=bkak=bk for 1≤k
#include<cstdio> #include<cstring> #define INF 0x3f3f3f3f #include<algorithm> using namespace std; int g[100010],a[100010],dp[100010]; int T,n; int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); fill(g+1,g+n+1,INF); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { int pos=lower_bound(g+1,g+n+1,a[i])-g;//找出第一个大于等于a[i]的位置 g[pos]=min(g[pos],a[i]);//g[]为INF,若a[i]小就把a[i]放进去 dp[i]=max(pos,dp[i]);//dp[]是从1递增的一次加1; } /* g[]赋值为INF,肯定能找到 大于等于a[i]的值, 此时g[pos]这个位置的值就换成a[i],下一次再找, 就会先与放进去的值比较, 若刚刚放进去的值大,那么现在的值又会取代它放到这个位置, 这就说明此时以这个值为结尾的序列长度为1,pos的值不会改变; 若刚刚放进去的值小,就说明能够构成长度大于1 的上升子序列 , pos的值会依次增大,每次都是加1 同时dp[i]里的值会取max(pos,dp[i]),再下一次,依次找, 到最后dp 里面存的值就是最长子序列的长度; 每一个dp[i]就是以a[i]为结尾的最长子序列的长度; */ for(int i=1;i<=n;i++) { printf("%d%c",dp[i],i==n?'\n':' '); } } return 0; }
相关文章推荐
- Bellovin【hdu 5748 dp lis】
- 计蒜客 The Heaviest Non-decreasing Subsequence Problem dp LIS变形 || 线段树+dp
- POJ 3670 DP LIS?
- HDUOJ 1025 - ConstructingRoadsInJGShining'sKingdom(DP:最长递增子序列LIS【nlogn算法】)
- HDU 1069 Monkey and Banana DP LIS
- hdoj 4352 XHXJ's LIS 【数位dp】
- 动态规划(DP),类似LIS,FatMouse's Speed
- Hdu5748-Bellovin-最长上升子序列(LIS)
- UVA 10534 Wavio Sequence(dp + LIS)
- hdu 4352 XHXJ's LIS 数位DP
- POJ 3670 Eating Together (DP,LIS)
- HDU_4352 _ XHXJ's LIS (数位Dp+LIS + 状压)
- hdu1025 dp(最长上升子序列LIS)
- HDU 5087 (线性DP+次大LIS)
- hdu 5087 Revenge of LIS II 题解(DP) BestCoder Round #16 1002
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
- dp-挑战程序设计竞赛-状态的选择 多重部分和 LIS
- 【CJOJ2498】【DP合集】最长上升子序列 LIS
- HDU 1069 Monkey and Banana DP LIS
- HDU 1025 Constructing Roads In JGShining's Kingdom (DP----LIS 的nlogn算法)