POJ-1877,3903 LIS

  1877题目链接:http://poj.org/problem?id=1887

  3903题目链接:http://poj.org/problem?id=3903

  数据大,线性优化,O(n*logn)。

1877:

 1 //STATUS:C++_AC_16MS_172KB
 2 #include <functional>
 3 #include <algorithm>
 4 #include <iostream>
 5 //#include <ext/rope>
 6 #include <fstream>
 7 #include <sstream>
 8 #include <iomanip>
 9 #include <numeric>
10 #include <cstring>
11 #include <cassert>
12 #include <cstdio>
13 #include <string>
14 #include <vector>
15 #include <bitset>
16 #include <queue>
17 #include <stack>
18 #include <cmath>
19 #include <ctime>
20 #include <list>
21 #include <set>
22 #include <map>
23 using namespace std;
24 //define
25 #define pii pair<int,int>
26 #define mem(a,b) memset(a,b,sizeof(a))
27 #define lson l,mid,rt<<1
28 #define rson mid+1,r,rt<<1|1
29 #define PI acos(-1.0)
30 //typedef
31 typedef __int64 LL;
32 typedef unsigned __int64 ULL;
33 //const
34 const int N=800010;
35 const int INF=0x3f3f3f3f;
36 const int MOD=100000,STA=8000010;
37 const LL LNF=1LL<<60;
38 const double EPS=1e-8;
39 const double OO=1e15;
40 const int dx[4]={-1,0,1,0};
41 const int dy[4]={0,1,0,-1};
42 //Daily Use ...
43 inline int sign(double x){return (x>EPS)-(x<-EPS);}
44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
46 template<class T> inline T Min(T a,T b){return a<b?a:b;}
47 template<class T> inline T Max(T a,T b){return a>b?a:b;}
48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
52 //End
53 
54 int num,f[N];
55 int T,n;
56 
57 int binary(int l,int r,int tar)
58 {
59     int mid;
60     while(l<r){
61         mid=(l+r)>>1;
62         if(f[mid]>=tar)l=mid+1;
63         else r=mid;
64     }
65     return l;
66 }
67 
68 int main()
69 {
70  //   freopen("in.txt","r",stdin);
71     int i,j,l,r,k,ca=1;
72     while(1){
73         scanf("%d",&num);
74         if(num==-1)break;
75         l=1;r=2;
76         f[1]=num;
77         while(1){
78             scanf("%d",&num);
79             if(num==-1)break;
80             k=binary(l,r,num);
81             f[k]=num;
82             r=Max(r,k+1);
83         }
84 
85         printf("Test #%d:\n",ca++);
86         printf("  maximum possible interceptions: %d\n\n",r-1);
87     }
88     return 0;
89 }
View Code

3903:

 1 //STATUS:C++_AC_63MS_756KB
 2 #include <functional>
 3 #include <algorithm>
 4 #include <iostream>
 5 //#include <ext/rope>
 6 #include <fstream>
 7 #include <sstream>
 8 #include <iomanip>
 9 #include <numeric>
10 #include <cstring>
11 #include <cassert>
12 #include <cstdio>
13 #include <string>
14 #include <vector>
15 #include <bitset>
16 #include <queue>
17 #include <stack>
18 #include <cmath>
19 #include <ctime>
20 #include <list>
21 #include <set>
22 #include <map>
23 using namespace std;
24 //define
25 #define pii pair<int,int>
26 #define mem(a,b) memset(a,b,sizeof(a))
27 #define lson l,mid,rt<<1
28 #define rson mid+1,r,rt<<1|1
29 #define PI acos(-1.0)
30 //typedef
31 typedef __int64 LL;
32 typedef unsigned __int64 ULL;
33 //const
34 const int N=100010;
35 const int INF=0x3f3f3f3f;
36 const int MOD=100000,STA=8000010;
37 const LL LNF=1LL<<60;
38 const double EPS=1e-8;
39 const double OO=1e15;
40 const int dx[4]={-1,0,1,0};
41 const int dy[4]={0,1,0,-1};
42 //Daily Use ...
43 inline int sign(double x){return (x>EPS)-(x<-EPS);}
44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
46 template<class T> inline T Min(T a,T b){return a<b?a:b;}
47 template<class T> inline T Max(T a,T b){return a>b?a:b;}
48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
52 //End
53 
54 int num[N],f[N];
55 int T,n;
56 
57 int binary(int l,int r,int tar)
58 {
59     int mid;
60     while(l<r){
61         mid=(l+r)>>1;
62         if(f[mid]<tar)l=mid+1;
63         else r=mid;
64     }
65     return l;
66 }
67 
68 int main()
69 {
70  //   freopen("in.txt","r",stdin);
71     int i,j,l,r,k;
72     while(~scanf("%d",&n))
73     {
74         for(i=0;i<n;i++){
75             scanf("%d",&num[i]);
76         }
77 
78         l=1;r=2;
79         f[1]=INF;
80         for(i=0;i<n;i++){
81             k=binary(l,r,num[i]);
82             f[k]=num[i];
83             r=Max(r,k+1);
84         }
85 
86         printf("%d\n",r-1);
87     }
88     return 0;
89 }
View Code

 

posted @ 2013-05-21 01:53  zhsl  阅读(204)  评论(0编辑  收藏  举报