Description
We all know 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?
Input
Multiple groups of input data, processing to 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)
Output
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 train of thought: I think it's a tough question. Unlike the previous O (nlogn) Lis, every time we process the I element, to process the number of I-d and determine whether to put it into the queue.#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn = 100005;const int inf = 0x3f3f3f3f;int n, d;int seq[maxn], num[maxn];int dp[maxn];int main() {while (scanf("%d%d", &n, &d) != EOF) {for (int i = 1; i <= n; i++)scanf("%d", &seq[i]);for (int i = 0; i <= n+1; i++)num[i] = inf;memset(dp, 0, sizeof(dp));int ans = 0;for (int i = 1; i <= n; i++) {int k = lower_bound(num, num+n, seq[i]) - num;dp[i] = k;ans = max(ans, dp[i]+1);int j = i - d;if (j > 0) {int tmp = dp[j];num[tmp] = min(num[tmp], seq[j]);}}printf("%d\n", ans);}return 0;}
Problem of HDU-4521 Xiao Ming series-Xiao Ming sequence (LIS with interval)