T1
Determine the answer from the high end by the bit
A=1 F[i] Indicates that the first number of the legal division must be at least a few paragraphs, time complexity $o (n^2log (ANS)) $
A>1 F[i][j] Indicates whether the number of pre-I is divided into J-segment may be legal, transfer obviously, time complexity $o (n^3log (ANS)/32) $
#include <cstdio>#include<cstring>#include<bitset>typedefLong Longi64;intn,a,b;intf[ -];i64 s[ -],ans=0; Std::bitset<207>d[207];intMain () {scanf ("%d%d%d",&n,&a,&B); for(intI=1; i<=n;++i) scanf ("%lld", s+i), s[i]+=s[i-1]; if(a==1){ for(intx= -; x>=0;--x) {i64 v=ans| ((1ll<<x)-1); for(intI=1; i<=n;++i) {F[i]=0x3f3f3f3f; for(intj=0; j<i;++j)if(((S[I]-S[J)) |v) ==v&&f[j]+1<f[i]) f[i]=f[j]+1; } if(f[n]>b) ans|=1ll<<x; } }Else{d[0][0]=1; for(intx= -; x>=0;--x) {i64 v=ans| ((1ll<<x)-1); for(intI=1; i<=n;++i) {D[i].reset (); for(intj=0; j<i;++j)if(((S[I]-S[J)) |v) = =v) d[i]|=d[j]<<1; } BOOLDd=0; for(intI=a;i<=b;++i) dd|=D[n][i]; if(!DD) ans|=1ll<<x; }} printf ("%lld\n", ans); return 0;}
View Code
T2
For each of the different (P[i],b[i]%p[i]), new points indicate where I can go, where each position and I also construct corresponding points
can finally be converted to 01-Edge shortest
Can prove that space-time complexity is $o (N^\frac 3 2) $
#include <bits/stdc++.h>Const intinf=0x3f3f3f3f, c= -;intn,m,b[30007],p[30007],b;structedge{intto ; Edge*NX;}*ep=0, *mep=0; Edge*e0[30007*C];intl[30007*C];BOOL is[30007*C];voidAeintAintb) { if(EP==MEP) ep=Newedge[10000],mep=ep+10000; *ep=(Edge) {B,e0[a]}; E0[a]=ep++;}intIDP;structnode{intw,l;voidUPD1 (int);}; std::d eque<node>Q;std::map<int,int>MP;voidNODE::UPD1 (intu) { if(:: l[u]>l+1) q.push_back (node) {u,::l[u]=l+1});}intMain () {scanf ("%d%d",&n,&m); for(intI=0; i<m;++i) scanf ("%d%d", b+i,p+i); IDP=n+m; for(intI=0; i<m;++i) {AE (b[i],n+i); AE (n+I,b[i]); intx=p[i],y=b[i]%x; int&z=mp[x<< the|y]; if(!z) {Z=IDP; intA; for(a=y;a+x<n;a+=x) { is[idp]=1; AE (IDP++, a); } AE (IDP++, a); } AE (n+i,z+ (b[i]-y)/x); } std::fill (L,l+idp+1, INF); Q.push_back (node) {N,l[n]=0}); while(!Q.empty ()) {Node W=Q.front (); Q.pop_front (); if(W.L!=L[W.W])Continue; if(w.w==n+1)returnprintf"%d\n", W.L),0; for(edge*i=e0[w.w];i;i=i->NX) { intU=i->to ; if(L[U]>W.L) Q.push_front ((node) {u,l[u]=W.L}); } if( is[w.w-1]) W.UPD1 (w.w-1); if( is[W.W]) W.UPD1 (w.w+1); } returnPuts"-1"),0;}
View Code
T3
Contribute to the direct calculation of the same side, otherwise consider the different side
K=1, median position optimal
k=2, it can be proved that the optimal solution can be found after dividing the $s_i+t_i$ into left and right parts after sorting according to the k=1 situation.
The median with insert can be maintained with the top heap
Time complexity $o (NLOGN) $
#include <bits/stdc++.h>typedefLong Longi64;intK,n;Chars1[2],s2[2];intp1,p2,xs[200007],xp=0; i64 ans=0;intAbsintx) {returnX>0? x:-x;}structpos{intb; BOOL operator< (POS W)Const{returna+b<w.a+w.b;}} ps[100007];intpp=0; i64 SS1=0, ss2=0, f[100007];std::p riority_queue<int>q1;std::p riority_queue<int,std::vector<int>,std::greater<int> >Q2;voidinit () {Q1=STD::p riority_queue<int>(); Q2=STD::p riority_queue<int,std::vector<int>,std::greater<int> >(); SS1=ss2=0;}voidInsintx) { if(Q1.size () &&x<=Q1.top ()) {Q1.push (x), SS1+=x; if(Q1.size () >q2.size () +1){ intx=q1.top (); Q1.pop (); SS1-=x;ss2+=x; Q2.push (x); } }Else{Q2.push (x), SS2+=x; if(Q2.size () >q1.size () +1){ intx=q2.top (); Q2.pop (); SS2-=x;ss1+=x; Q1.push (x); } }}voidmins (i64&a,i64 b) {if(a>b) a=b;} I64 cal () {returnss2-SS1;}intMain () {scanf ("%d%d",&k,&N); if(k==1){ for(intt=0; t<n;++t) {scanf ("%s%d%s%d",s1,&p1,s2,&p2); if(s1[0]==s2[0]) Ans+=abs (p2-p1); Else++ans,xs[xp++]=p1,xs[xp++]=P2; } if(XP) {std::nth_element (Xs,xs+xp/2, xs+XP); intx=xs[xp/2]; for(intA=0; a<xp;++a) Ans+=abs (xs[a]-x); } }Else{ for(intt=0; t<n;++t) {scanf ("%s%d%s%d",s1,&p1,s2,&p2); if(s1[0]==s2[0]) Ans+=abs (p2-p1); Else++ans,ps[pp++]=(POS) {P1,P2}; } std::sort (Ps,ps+PP); if(PP) {init (); for(intA=0; a<pp;++a) {ins (ps[a].a); ins (ps[a].b); F[a]=cal (); } i64 MX=f[pp-1]; Init (); for(inta=pp-1; a;--a) {ins (ps[a].a); ins (ps[a].b); mins (mx,cal ()+f[a-1]); } ans+=MX; }} printf ("%lld\n", ans); return 0;}
View Code
Bzoj 4069~4071 APIO2015