BZOJ - 2244 interceptor missiles (dp, CDQ partition + Fenwick tree optimization)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #define MAXN 50010 6 #define LL long long 7 using namespace std; 8 struct ddd 9 { 10 int t,h,v,ans; 11 #define t(x) a[x].t 12 #define h(x) a[x].h 13 #define v(x) a[x].v 14 #define ans(x) a[x].ans 15 }a[MAXN]; 16 int n,th[MAXN],tv[MAXN]; 17 bool cmp1(ddd a,ddd b){return a.t==b.t?(a.h==b.h?(a.v<b.v):a.h<b.h):a.t<b.t;} 18 bool cmp2(ddd a,ddd b){return a.h==b.h?(a.t==b.t?(a.v>b.v):a.t<b.t):a.h>b.h;} 19 int ans=1; 20 int C[MAXN]; 21 #define lowbit(x) ((x)&(-(x))) 22 //void add(int x,int y){while(x<=n){C[x]+=y;x+=lowbit(x);}} 23 //int ask(int x){int ans=0;while(x){ans+=C[x];x-=lowbit(x);}return ans;} 24 void add(int x,int y) 25 {//cout<<"add "<<x<<" "<<y<<endl; 26 while(x){C[x]=max(C[x],y);x-=lowbit(x);}} 27 void admin(int x,int y) 28 {while(x){C[x]=min(C[x],y);x-=lowbit(x);}} 29 int ask(int x){int ans=0;while(x<=n){ans=max(ans,C[x]);x+=lowbit(x);}return ans;} 30 void CDQ(int l,int r) 31 { 32 if(l==r){return;} 33 int mid=(l+r)>>1; 34 // sort(a+l,a+mid+1,cmp1); 35 CDQ(l,mid); 36 sort(a+l,a+mid+1,cmp2); 37 sort(a+mid+1,a+r+1,cmp2); 38 int j=l; 39 // cout<<"# "<<l<<" "<<r<<endl; 40 // for(int i=l;i<=r;i++)cout<<i<<": "<<t(i)<<" "<<h(i)<<" "<<v(i)<<endl; 41 for(int i=mid+1;i<=r;i++) 42 { 43 while(j<=mid&&h(j)>=h(i)){//cout<<"!!!!!!!!j "<<j<<" "<<ans(j)<<endl; 44 add(v(j),ans(j)),j++;} 45 ans(i)=max(ans(i),ask(v(i))+1);//cout<<"!!!!!!!!!!ans "<<i<<" "<<ans(i)<<endl; 46 } 47 for(int i=l;i<j;i++)admin(v(i),0); 48 sort(a+mid+1,a+r+1,cmp1); 49 CDQ(mid+1,r); 50 } 51 signed main() 52 { 53 // freopen("in.txt","r",stdin); 54 55 cin>>n; 56 for(int i=1;i<=n;i++) 57 cin>>h(i)>>v(i),t(i)=i,th[i]=h(i),tv[i]=v(i); 58 sort(th+1,th+n+1); 59 sort(tv+1,tv+n+1); 60 int len1=unique(th+1,th+n+1)-th-1; 61 int len2=unique(tv+1,tv+n+1)-tv-1; 62 for(int i=1;i<=n;i++) 63 h(i)=lower_bound(th+1,th+len1+1,h(i))-th, 64 v(i)=lower_bound(tv+1,tv+len2+1,v(i))-tv; 65 sort(a+1,a+n+1,cmp1); 66 for(int i=1;i<=n;i++)ans(i)=1; 67 CDQ(1,n); 68 sort(a+1,a+n+1,cmp1); 69 // for(int i=1;i<=n;i++)cout<<"ans "<<i<<" "<<ans(i)<<endl; 70 for(int i=1;i<=n;i++)ans=max(ans,ans(i)); 71 printf("%d\n",ans); 72 for(int i=1;i<=n;i++) cout<<(double)1/3<<" ";puts(""); 73 }
Let me first ask it, would feel quite simple, but doing it only to find has been stressed out ...... In fact, the first question is relatively easy to see out of a three partial order problem, but not a simple sum, and is a point in front of h and v are greater than the maximum +1 point his answer to update this point, so the tree suffix array to maintain, remember that the initial answers should be attached to each point 1.
The second question is more trouble, occurrence probability of one point is equal to $ \ {FRAC scheme includes the number of points {}} $ of the total number of programs , the maintenance of the 4 array, F [i] i is represented by the maximum length of the end, G [i] where i is the number represented by the maximum length of the end of the program, h [i] represents the maximum length i beginning, s [i] represents the number of the program beginning with i maximum length. CDQ run two sides, where the said first pass, a maintenance array suffix tree and the maximum number of programs (or implemented with a pair structure), the left first recursive processing section CDQ time, then regardless of the current interval, the last recursion deal with the right range. As for updating the answer:
if(tem.maxn==f(i))g(i)+=tem.num; else if(tem.maxn>f(i))f(i)=tem.maxn,g(i)=tem.num;
There are several points to note: If a point is not included in the longest sequence, then checked his probability to zero. Remember to open the double, this question will burst longlong.
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #define MAXN 50010 7 #define LL long long 8 #define int LL 9 using namespace std; 10 struct ddd 11 { 12 int t,h,v,ans; 13 double f,g,he,s; 14 #define t(x) a[x].t 15 #define h(x) a[x].h 16 #define v(x) a[x].v 17 #define ans(x) a[x].ans 18 #define f(x) a[x].f 19 #define g(x) a[x].g 20 #define he(x) a[x].he 21 #define s(x) a[x].s 22 }a[MAXN]; 23 int n,th[MAXN],tv[MAXN]; 24 bool cmp1(ddd a,ddd b){return a.t==b.t?(a.h==b.h?(a.v<b.v):a.h<b.h):a.t<b.t;} 25 bool cmp2(ddd a,ddd b){return a.h==b.h?(a.t==b.t?(a.v>b.v):a.t<b.t):a.h>b.h;} 26 bool cmp3(ddd a,ddd b){return a.t==b.t?(a.h==b.h?(a.v<b.v):a.h<b.h):a.t>b.t;} 27 bool cmp4(ddd a,ddd b){return a.h==b.h?(a.t==b.t?(a.v<b.v):a.t<b.t):a.h<b.h;} 28 double ans=1; 29 #define lowbit(x) ((x)&(-(x))) 30 struct cc{double maxn;double num;}C[MAXN]; 31 void add(int x,double y,double nu) 32 { 33 while(x) 34 { 35 if(C[x].maxn==y)C[x].num+=nu; 36 else if(C[x].maxn<y)C[x].maxn=y,C[x].num=nu; 37 x-=lowbit(x); 38 } 39 } 40 void admin(int x) 41 {while(x){C[x].maxn=C[x].num=0;x-=lowbit(x);}} 42 cc ask(int x) 43 { 44 cc ans={0,0}; 45 while(x<=n) 46 { 47 if(C[x].maxn==ans.maxn)ans.num+=C[x].num; 48 else if(C[x].maxn>ans.maxn)ans.maxn=C[x].maxn,ans.num=C[x].num; 49 x+=lowbit(x); 50 } 51 return ans; 52 } 53 cc C2[MAXN]; 54 void add2(int x,double y,double nu) 55 { 56 while(x<=n) 57 { 58 if(C2[x].maxn==y)C2[x].num+=nu; 59 else if(C2[x].maxn<y)C2[x].maxn=y,C2[x].num=nu; 60 x+=lowbit(x); 61 } 62 } 63 void admin2(int x) 64 {while(x<=n){C2[x].maxn=C2[x].num=0;x+=lowbit(x);}} 65 cc ask2(int x) 66 { 67 cc ans={0,0}; 68 while(x) 69 { 70 if(C2[x].maxn==ans.maxn)ans.num+=C2[x].num; 71 else if(C2[x].maxn>ans.maxn)ans.maxn=C2[x].maxn,ans.num=C2[x].num; 72 x-=lowbit(x); 73 } 74 return ans; 75 } 76 void CDQ(int l,int r) 77 { 78 if(l==r){return;} 79 int mid=(l+r)>>1; 80 CDQ(l,mid); 81 sort(a+l,a+mid+1,cmp2); 82 sort(a+mid+1,a+r+1,cmp2); 83 int j=l; 84 for(int i=mid+1;i<=r;i++) 85 { 86 while(j<=mid&&h(j)>=h(i)) 87 { 88 add(v(j),f(j),g(j));j++; 89 } 90 cc tem=ask(v(i));tem.maxn++; 91 if(tem.maxn==f(i))g(i)+=tem.num; 92 else if(tem.maxn>f(i))f(i)=tem.maxn,g(i)=tem.num; 93 } 94 for(int i=l;i<j;i++)admin(v(i)); 95 sort(a+mid+1,a+r+1,cmp1); 96 CDQ(mid+1,r); 97 } 98 void CDQ2(int l,int r) 99 { 100 if(l==r){return;} 101 int mid=(l+r)>>1; 102 CDQ2(l,mid); 103 sort(a+l,a+mid+1,cmp4); 104 sort(a+mid+1,a+r+1,cmp4); 105 int j=l; 106 for(int i=mid+1;i<=r;i++) 107 { 108 while(j<=mid&&h(j)<=h(i)) 109 { 110 add2(v(j),he(j),s(j));j++; 111 } 112 cc tem=ask2(v(i));tem.maxn++; 113 if(tem.maxn==he(i))s(i)+=tem.num; 114 else if(tem.maxn>he(i))he(i)=tem.maxn,s(i)=tem.num; 115 } 116 for(int i=l;i<j;i++)admin2(v(i)); 117 sort(a+mid+1,a+r+1,cmp3); 118 CDQ2(mid+1,r); 119 } 120 signed main() 121 { 122 // freopen("1.in","r",stdin); 123 // freopen("out.out","w",stdout); 124 125 cin>>n; 126 for(int i=1;i<=n;i++) 127 cin>>h(i)>>v(i),t(i)=i,th[i]=h(i),tv[i]=v(i); 128 sort(th+1,th+n+1); 129 sort(tv+1,tv+n+1); 130 int len1=unique(th+1,th+n+1)-th-1; 131 int len2=unique(tv+1,tv+n+1)-tv-1; 132 for(int i=1;i<=n;i++) 133 h(i)=lower_bound(th+1,th+len1+1,h(i))-th, 134 v(i)=lower_bound(tv+1,tv+len2+1,v(i))-tv; 135 for(int i=1;i<=n;i++)f(i)=g(i)=he(i)=s(i)=1; 136 sort(a+1,a+n+1,cmp1); 137 CDQ(1,n); 138 for(int i=1;i<=n;i++)ans=max(ans,f(i)); 139 printf("%0.0lf\n",ans); 140 sort(a+1,a+n+1,cmp3); 141 CDQ2(1,n); 142 double summ=0; 143 sort(a+1,a+n+1,cmp1); 144 for(int i=1;i<=n;i++) 145 if(fabs(f(i)-ans)<=1e-8) summ+=g(i); 146 for(int i=1;i<=n;i++) 147 { 148 if(f(i)+he(i)-1==ans){double ttt=(double)(1.0*g(i)*s(i))/summ;printf("%0.5lf ",ttt);} 149 else printf("0.00000 "); 150 }puts(""); 151 }