zoukankan      html  css  js  c++  java
  • ZOJ-3349 Special Subsequence 线段树优化DP

      题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3349

      题意:给定一个数列,序列A是一个满足|Ai-Ai-1| <= d的一个序列,其中Ai为给定数列中的元素,要保持相对顺序,求最长的序列A。

      显然用DP来解决,f[i]表示以第i个数结尾时的最长长度,则f[i]=Max{ f[j]+1 | j<i && |num[j]-num[i]|<=d }。直接转移复杂度O(n^2),超时。显然对于每个数num只要保存最大的f值就可以了,然后就是查找num[j]在区间[num[i]-d, num[i]+d]的最大的f[j],用颗线段树维护就可以了。。

      1 //STATUS:C++_AC_480MS_3008KB
      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 //#pragma comment(linker,"/STACK:102400000,102400000")
     25 //using namespace __gnu_cxx;
     26 //define
     27 #define pii pair<int,int>
     28 #define mem(a,b) memset(a,b,sizeof(a))
     29 #define lson l,mid,rt<<1
     30 #define rson mid+1,r,rt<<1|1
     31 #define PI acos(-1.0)
     32 //typedef
     33 typedef long long LL;
     34 typedef unsigned long long ULL;
     35 //const
     36 const int N=100010;
     37 const int INF=0x3f3f3f3f;
     38 const int MOD=1e+7,STA=8000010;
     39 const LL LNF=1LL<<60;
     40 const double EPS=1e-8;
     41 const double OO=1e15;
     42 const int dx[4]={-1,0,1,0};
     43 const int dy[4]={0,1,0,-1};
     44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     45 //Daily Use ...
     46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
     50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     56 //End
     57 
     58 int num[N],order[N],hig[N<<2],f[N];
     59 int n,d;
     60 
     61 void update(int l,int r,int rt,int w,int val)
     62 {
     63     if(l==r){
     64         hig[rt]=max(hig[rt],val);
     65         return ;
     66     }
     67     int mid=(l+r)>>1;
     68     if(w<=mid)update(lson,w,val);
     69     else update(rson,w,val);
     70     hig[rt]=max(hig[rt<<1],hig[rt<<1|1]);
     71 }
     72 
     73 int query(int l,int r,int rt,int L,int R)
     74 {
     75     if(L<=l && r<=R){
     76         return hig[rt];
     77     }
     78     int mid=(l+r)>>1,ret=0;
     79     if(L<=mid)ret=max(ret,query(lson,L,R));
     80     if(R>mid)ret=max(ret,query(rson,L,R));
     81     return ret;
     82 }
     83 
     84 int main()
     85 {
     86  //   freopen("in.txt","r",stdin);
     87     int i,j,m,L,R,lnum,rnum,ans,w;
     88     while(~scanf("%d%d",&n,&d))
     89     {
     90         for(i=0;i<n;i++){
     91             scanf("%d",&num[i]);
     92             order[i]=num[i];
     93         }
     94         sort(order,order+n);
     95         m=unique(order,order+n)-order;
     96 
     97         mem(hig,0);ans=0;
     98         for(i=0;i<n;i++){
     99             lnum=num[i]-d;rnum=num[i]+d;
    100             L=lower_bound(order,order+m,lnum)-order;
    101             R=upper_bound(order,order+m,rnum)-order-1;
    102             f[i]=query(0,m-1,1,L,R)+1;
    103             w=lower_bound(order,order+m,num[i])-order;
    104             update(0,m-1,1,w,f[i]);
    105             ans=max(ans,f[i]);
    106         }
    107 
    108         printf("%d
    ",ans);
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    httpRuntime 元素(ASP.NET 设置架构)
    Change GridView RowColor OnMouseClick
    创建自定义配置结
    设置自定义服务器控件的TagPrefix
    使用DataTable.ReadXml时抛出异常
    正则表达式的字符,定位字符和重复字符
    URL 编码
    tschs.xml 全文索引文件配置说明
    SQL2005 全文索引 contains
    SQL 语句执行时间
  • 原文地址:https://www.cnblogs.com/zhsl/p/3578811.html
Copyright © 2011-2022 走看看