Xiao Ming series of questions-Xiao Ming Series
Time Limit: 3000/1000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 1848 accepted submission (s): 564
Problem description everyone knows that James prefers to study sequence-related problems, but that's why James has almost gone through various sequence problems. Poor James struggled to find new sequence problems on major websites, but finding them was a sequence he had already studied. James figured that since he could not find it, I 'd like to invent a new sequence problem by myself! James finally came up with a new sequence problem. He was ecstatic because he had come up with the new sequence problem and named it "James sequence ".
When talking about James's sequence, he defined it as follows:
① Define s as an ordered sequence. S = {A1, A2, A3,..., an}, and n are the number of elements;
② Define sub as a subsequence taken out in S. sub = {aI1, ai2, AI3,..., aim}, and m are the number of elements;
③ Where sub satisfies aI1 <ai2 <AI3 <... <Aij-1 <AIJ + 1 <... <aim;
④ At the same time sub satisfies for any connected two Aij-1 and AIJ have Ij-ij-1> D (1 <j <= m, D is the given integer );
⑤ Obviously, there will be many sub sequences that meet such requirements, and in the obtained sub sequences, the sub-sequence with the largest number of elements is called the "Xiao Ming sequence ).
Example: sequence S = {,}, where D = 1;
The m value of the "Xiao Ming sequence" is 2. That is, sub = {2, 3}, {2, 4}, or {1, 4} are all "Xiao Ming sequences ".
When James invented the "Xiao Ming sequence", he was so excited that his mind was so messy that he wanted to ask you to help him calculate the given S sequence and integer d, how many elements are required in the "Xiao Ming sequence?
Multiple groups of input data are processed until the end of the file;
The first two positive integers n and D are input. (1 <= n <= 10 ^ 5, 0 <= d <= 10 ^ 5)
The second input behavior is n integers A1, A2, A3,..., An, representing n elements of the S sequence. (0 <= AI <= 10 ^ 5)
For output, please output the number of elements in the "Xiao Ming sequence" for each group of data, and output a row of test data for each group.
Sample Input
2 01 25 13 4 5 1 25 23 4 5 1 2
Sample output
221
To solve the problem of deformation of the longest ascending subsequence, the restriction condition requires that the subscript difference between the two adjacent items must be greater than D.
C [] array stores the currently available values.
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespace std;#define N 100005const int inf=0x1f1f1f1f;int a[N],c[N],dp[N];int n,d;int findd(int x){ int l,r,mid; l=0; r=n-1; while(l<=r) { mid=(l+r)>>1; if(c[mid]<x) l=mid+1; else r=mid-1; } return l;}int solve(){ int i,j,ans=0; for(i=0;i<n;i++) { dp[i]=findd(a[i]); ans=max(ans,dp[i]); j=i-d; if(j>=0&&c[dp[j]]>a[j]) c[dp[j]]=a[j]; } return ans;}int main(){ while(scanf("%d%d",&n,&d)!=-1) { for(int i=0;i<n;i++) { scanf("%d",&a[i]); c[i]=inf; } printf("%d\n",solve()+1); } return 0;}
HDU 4521 Xiao Ming series problems-Xiao Ming sequence (LIS deformation)