# [考试反思]0317省选模拟48：记忆

$T3$保证有解但是题目没说还害得我以为读错题了又是浪费了时间。

T1：A/事情的相似度

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define S 100005
4 #define ull unsigned long long
5 int n,m,sa[S],h[S],f[S],ans[S],lcnt,t[S]; ull hsh[S],pw[S]; char s[S];
6 void mdf(int p,int v){for(;p<=n;p+=p&-p)t[p]=max(t[p],v);}
7 int ask(int p,int a=0){for(;p;p^=p&-p)a=max(a,t[p]);return a;}
8 set<int>pt[S]; vector<int>v[S];
9 struct lim{int a,b,w;friend bool operator<(lim x,lim y){return x.b<y.b;}}l[S<<8];
10 struct qs{int l,r,o;friend bool operator<(qs x,qs y){return x.r<y.r;}}q[S];
11 ull Hsh(int l,int r){return hsh[r]-hsh[l-1]*pw[r-l+1];}
12 int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
13 bool cmp(int la,int lb){
14     int r=max(n-la+1,n-lb+1),l=0,ans,md;
15     while(md=l+r>>1,l<=r)if(Hsh(la,la+md-1)==Hsh(lb,lb+md-1))l=ans=l+r>>1,l++;else r=(l+r>>1)-1;
16     if(la+ans>n)return 1; if(lb+ans>n)return 0;
17     return s[lb+ans]-48;
18 }
19 int getsame(int la,int lb){
20     int r=max(n-la+1,n-lb+1),l=0,ans,md;
21     while(md=l+r>>1,l<=r)if(Hsh(la,la+md-1)==Hsh(lb,lb+md-1))l=ans=l+r>>1,l++;else r=(l+r>>1)-1;
22     return ans;
23 }
24 void merge(int a,int b,int val){
25     a=find(a);b=find(b);
26     if(pt[a].size()<pt[b].size())swap(a,b);
27     f[b]=a;
28     for(auto j:pt[b]){
29         auto x=pt[a].lower_bound(j);
30         if(x!=pt[a].begin())x--,l[++lcnt]=(lim){*x,j,val},x++;
31         if(x!=pt[a].end())l[++lcnt]=(lim){j,*x,val};
32     }
33     for(auto j:pt[b])pt[a].insert(j); pt[b].clear();
34 }
35 int main(){
36     scanf("%d%d%s",&n,&m,s+1); pw[0]=1; reverse(s+1,s+1+n);
37     for(int i=1;i<=n;++i)pw[i]=pw[i-1]*11,hsh[i]=hsh[i-1]*11+s[i]-48,sa[i]=f[i]=i,pt[i].insert(i);
38     stable_sort(sa+1,sa+1+n,cmp);
39     for(int i=2;i<=n;++i)h[i]=getsame(sa[i-1],sa[i]),v[h[i]].push_back(i);
40     for(int i=n;i;--i)for(auto j:v[i])merge(sa[j-1],sa[j],i);
41     for(int i=1,x,y;i<=m;++i)scanf("%d%d",&x,&y),q[i]=(qs){n+1-y,n+1-x,i};
42     sort(q+1,q+1+m);sort(l+1,l+1+lcnt);
43     int ptl=1,ptq=1;
44     for(int i=1;i<=n;++i){
45         while(l[ptl].b==i)mdf(n+1-l[ptl].a,l[ptl].w),ptl++;
47     }
48     for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
49 }

T2：B/跳蚤王国的宰相

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define S 2000005
4 int sz[S],C,n,fir[S],l[S],to[S],ec,v[S],vc,bl[S],sum[S],pos[S];
6 void dfs(int p,int fa=0,int anc=0,int o=0){
7     sz[p]=1;int mxs=0; if(o==1)anc=p;bl[p]=anc;
8     for(int i=fir[p];i;i=l[i])if(to[i]!=fa)dfs(to[i],p,anc,o+1),sz[p]+=sz[to[i]],mxs=max(mxs,sz[to[i]]);
9     mxs=max(mxs,n-sz[p]);
10     if(mxs<=n>>1)C=p;
11 }
12 int main(){
13     cin>>n;
15     dfs(1);dfs(C);
16     int mx=0;
17     for(int i=fir[C];i;i=l[i])v[++vc]=sz[to[i]],mx=max(mx,sz[to[i]]);
18     sort(v+1,v+1+vc);reverse(v+1,v+1+vc);
19     for(int i=1;i<=vc;++i)sum[i]=sum[i-1]+v[i],pos[v[i]]=pos[v[i]]?pos[v[i]]:i;
20     for(int i=1;i<=n;++i)if(i!=C){
21         int z=sz[bl[i]],t=pos[z],p=lower_bound(sum,sum+t,n-n/2-sz[i])-sum;
22         printf("%d\n",p<t?p:min(lower_bound(sum+t,sum+vc+1,n-n/2+z-sz[i])-sum-1,lower_bound(sum+t,sum+vc+1,n-n/2+sz[i]-sz[i])-sum));
23     }else puts("0");
24 }
View Code

T3：C/蛐蛐国的修墙方案

 1 #include<bits/stdc++.h>
2 using namespace std;
3 int n,ans[111],to[111],q[111],t;
4 int main(){
5     cin>>n;
6     for(int i=1;i<=n;++i)cin>>to[i];
7     for(int i=1;i<=n;++i)if(!ans[i]){
8         int j=to[i],mn=i,op=40,cnt=1;
9         while(j!=i)mn=min(mn,j),j=to[j],cnt++;
10         while(!ans[mn])ans[mn]=op,op^=1,mn=to[mn];
11         if(cnt>2)q[++t]=mn;
12     }
13     for(int i=0;i<1<<t;++i){
14         for(int j=1;j<=t;++j){
15             int p=q[j],op=40+(i&1<<j-1?0:1);
16             ans[p]=op;op^=1;p=to[p];
17             while(p!=q[j])ans[p]=op,p=to[p],op^=1;
18         }
19         for(int i=1,v=0;i<=n;++i){
20             v+=ans[i]==40?1:-1;
21             if(v<0)goto x;
22         }break;x:;
23     }
24     for(int i=1;i<=n;++i)putchar(ans[i]);
25 }
View Code

posted @ 2020-03-17 18:33  DeepinC  阅读(...)  评论(...编辑  收藏