The programming question has the question number, should be randomly extracted. The main effect of the topic
Given a sequence, the length of its longest ascending subsequence is obtained. Idea-DP
Very simple DP, first to do the programming questions, the first quickly wrote an O (N2) solution then to do the previous question. Set Dp[i] Represents the length of the longest ascending sequence ending with the number I, and the state transition equation is: Dp[i]=max (Dp[j]) +1,j<i&&num[j]<num[i]
All done, found that there are half an hour, began to think O (NLOGN) solution, although I have forgotten how to write it, I remember that in the process of processing, the length of the ascending subsequence is subscript, and the last number of ascending subsequence is the value (same length, smaller value), and a monotonically increasing array can be constructed. Then you can use a second to find the length of the longest ascending subsequence of the last number that is less than the current number.
After proficiency, you don't need a DP array to code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, NUM, L, R, Mid, MX;
int Mn[100003];//mn[i] Represents the minimum value
int main () {
while (1 = scanf ("%d", &n)) in the ascending subsequence of length i {
mx = 0;
Memset (MN, 0x3f, sizeof (MN));
while (n--> 0) {
scanf ("%d", &num);
L = 1;
r = mx;
while (L <= R) {
mid = (L + r) >> 1;
if (Mn[mid] < num) {
L = mid + 1;
}
else {
r = mid-1;
}
}
Mn[r + 1] = min (mn[r + 1], num);
mx = max (mx, r + 1);
}
printf ("%d\n", MX);
}
return 0;
}
American group Reviews also Youdao reading program questions, gave an activity to create and destroy functions, create a function in the file read, but did not close the input stream. Only found this one error ...