51
Solution: The data volume is large, and the O (N ^ 2) method does not work! Only the O (nlogn) algorithm is available.
Attach the Tle code first.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int d[100010],dp[100010];12 int main(){13 int n,i,j,ans;14 while(~scanf("%d",&n)){15 for(i = 1; i <= n; i++){16 scanf("%d",d+i);17 dp[i] = 1;18 }19 ans = 1;20 for(i = 2; i <= n; i++){21 for(j = i-1; j; j--){22 if(d[j] < d[i] && dp[i] < dp[j]+1) dp[i] = dp[j]+1;23 }24 if(dp[i] > ans) ans = dp[i];25 }26 printf("%d\n",ans);27 }28 return 0;29 }
View code
Okay, the AC code and the O (nlogn) algorithm are exquisite in half-lookup and fast. Oh, the last top? Top: 1 can be removed. It's funny! The loss is 4 ms.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int stk[100010],top;12 void bsearch(int lt,int rt,int val) {13 int mid;14 while(lt <= rt) {15 mid = (lt+rt)>>1;16 if(val < stk[mid]) rt = mid-1;17 else lt = mid+1;18 }19 stk[lt] = val;20 }21 int main() {22 int n,i,temp;23 while(~scanf("%d",&n)) {24 top = 0;25 for(i = 1; i <= n; i++) {26 scanf("%d",&temp);27 if(!top || stk[top-1] < temp)28 stk[top++] = temp;29 else bsearch(0,top-1,temp);30 }31 printf("%d\n",top?top:1);32 }33 return 0;34 }
View code