BZOJ2244 拦截导弹
此题最早看到是在我还什么都不会的去年的暑期集训,是V8讲的DP专题,我当时还跑去问这概率怎么做。这道题要求的是二维最长不上升子序列,加上位置一维就成了三维偏序问题,也就是套用CDQ分治,对位置排序,然后对一维分治,对剩下的一维树状数组,类似的问题用树状数组套平衡树也能解决,但似乎常数很大。然后这题的第一个关键就是在做CDQ的时候先做CDQ(l,mid)的区间,然后去计算左边对右边的影响,最后去计算CDQ(mid+1,r),昨晚看别人博客中说这是显然的,当时我就懵逼了,于是这题今天上课看了很久。发现的确要这么做的,因为其实你是在维护第三维树状数组的时候去做了这个DP的过程,当计算以该点为结尾的最长不上升子序列时,你当然应该先做左区间,然后就可以去更新一下相对右边这些点的值,然后就再去做CDQ的右区间。具体是分别维护区间的最长不上升子序列的长度,以及出现的次数。做法是这样的,首先对左右两边分别按照第二维从小到大排序,将左边的所有的第二维大于右边的点全部加入树状数组,更新第三维,要注意更新和查询的方向正好是反过来的,因为你要查的是大于右边某个值的最长不上升子序列的长度,然后更新一下dp数组,也就是代码中的f[0]数组。然后再把整个序列反过来做一遍CDQ,求以某个点为开始的最长不上升子序列。
加一个学习的链接:https://www.cnblogs.com/liu-runda/p/6416195.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | #include<bits/stdc++.h> #define ll long double #define pb push_back #define _mp make_pair const int maxn=1e5+7; const int mod=1e9+7; using namespace std; int n; int h[2][maxn],v[2][maxn]; int iq[maxn],hq[maxn],vq[maxn]; int f[2][maxn]; ll g[2][maxn]; int MAX[maxn]; ll CNT[maxn]; void add1( int x, int w,ll cnt) { while (x>0) { if (MAX[x]<w) { MAX[x]=w;CNT[x]=cnt; } else if (MAX[x]==w)CNT[x]+=cnt; x-=x&-x; } } void add2( int x, int w,ll cnt) { while (x<maxn) { if (MAX[x]<w) { MAX[x]=w;CNT[x]=cnt; } else if (MAX[x]==w)CNT[x]+=cnt; x+=x&-x; } } int query1( int x) { int ans=0; while (x<maxn) { if (MAX[x]>ans)ans=MAX[x]; x+=x&-x; } return ans; } int query2( int x) { int ans=0; while (x>0) { if (MAX[x]>ans)ans=MAX[x]; x-=x&-x; } return ans; } ll query_cnt( int x, int val) { ll ans=0; while (x<maxn) { if (MAX[x]==val)ans+=CNT[x]; x+=x&-x; } return ans; } ll query_cnt2( int x, int val) { ll ans=0; while (x>0) { if (MAX[x]==val)ans+=CNT[x]; x-=x&-x; } return ans; } bool cmp1( const int & a, const int & b) { return h[0][a]<h[0][b]; } bool cmp2( const int & a, const int & b) { return v[0][a]<v[0][b]; } bool cmp3( const int & a, const int & b) { return h[1][a]<h[1][b]; } void del1( int x) { while (x>0) { MAX[x]=CNT[x]=0; x-=x&-x; } } void del2( int x) { while (x<maxn) { MAX[x]=CNT[x]=0; x+=x&-x; } } void cdq1( int l, int r) { if (l==r) return ; int mid=(l+r)>>1; cdq1(l,mid); for ( int i=l;i<=r;i++)iq[i]=i; sort(iq+l,iq+mid+1,cmp1); sort(iq+mid+1,iq+r+1,cmp1); int pp=mid; for ( int i=r;i>mid;i--) { while (pp>=l&&h[0][iq[pp]]>=h[0][iq[i]]) { add1(v[0][iq[pp]],f[0][iq[pp]],g[0][iq[pp]]); pp--; } int tmp=query1(v[0][iq[i]]); if (tmp+1>f[0][iq[i]]) { f[0][iq[i]]=tmp+1;g[0][iq[i]]=query_cnt(v[0][iq[i]],tmp); } else if (tmp+1==f[0][iq[i]]) { g[0][iq[i]]+=query_cnt(v[0][iq[i]],tmp); } } for ( int i=mid;i>pp;i--) { del1(v[0][iq[i]]); } cdq1(mid+1,r); } void cdq2( int l, int r) { if (l==r) return ; int mid=(l+r)>>1; cdq2(l,mid); for ( int i=l;i<=r;i++)iq[i]=i; sort(iq+l,iq+mid+1,cmp3); sort(iq+mid+1,iq+r+1,cmp3); int pp=l; for ( int i=mid+1;i<=r;i++) { while (pp<=mid&&h[1][iq[pp]]<=h[1][iq[i]]) { add2(v[1][iq[pp]],f[1][iq[pp]],g[1][iq[pp]]); pp++; } int tmp=query2(v[1][iq[i]]); if (tmp+1>f[1][iq[i]]) { f[1][iq[i]]=tmp+1;g[1][iq[i]]=query_cnt2(v[1][iq[i]],tmp); } else if (tmp+1==f[1][iq[i]]) { g[1][iq[i]]+=query_cnt2(v[1][iq[i]],tmp); } } for ( int i=l;i<pp;i++) { del2(v[1][iq[i]]); } cdq2(mid+1,r); } int main() { scanf ( "%d" ,&n); for ( int i=1;i<=n;i++) { scanf ( "%d%d" ,&h[0][i],&v[0][i]); iq[i]=i; } // cout<<222<<endl; sort(iq+1,iq+1+n,cmp1); int tot=0,las=-1; for ( int i=1;i<=n;i++) { if (las!=h[0][iq[i]]) { las=h[0][iq[i]]; ++tot; } h[0][iq[i]]=tot; } tot=0;las=-1; for ( int i=1;i<=n;i++) iq[i]=i; sort(iq+1,iq+1+n,cmp2); for ( int i=1;i<=n;i++) { if (las!=v[0][iq[i]]) { las=v[0][iq[i]]; ++tot; }v[0][iq[i]]=tot; } for ( int i=1;i<=n;i++) { h[1][i]=h[0][n+1-i]; v[1][i]=v[0][n+1-i]; } for ( int i=1;i<=n;i++) { iq[i]=i; f[0][i]=f[1][i]=g[0][i]=g[1][i]=1; } // cout<<222<<endl; cdq1(1,n);cdq2(1,n); // cout<<222<<endl; int ans=0; for ( int i=1;i<=n;i++) { if (f[0][i]>ans)ans=f[0][i]; } printf ( "%d\n" ,ans); ll sum=0; for ( int i=1;i<=n;i++) { if (f[0][i]==ans)sum+=g[0][i]; } //cout<<sum<<endl; for ( int i=1;i<=n;i++) { // cout<<g[0][i]<<" "<<g[1][n-i+1]<<endl; if (f[0][i]+f[1][n-i+1]!=ans+1) printf ( "%.8f " ,0.0); else printf ( "%.8f " , double (g[0][i]*g[1][n-i+1]/(sum))); //cout<<endl; // cout<<222<<endl; } // } // for(int i=1;i<=n;i++) cout<<g[0][i]<<" "<<g[1][n-i+1]<<endl; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步