noip模拟79[.]

noip模拟79 solutions

仍然不是很理想,还是忘记算内存了,又炸了

T1 F

期望线性性

AC_code
#include<bits/stdc++.h>
using namespace std;
#define oj
#define int long long
#define fo(i,x,y) for(int i=(x);i<=(y);i++)
#define fu(i,x,y) for(int i=(x);i>=(y);i--)
const int N=1005;
const int mod=998244353;
char s[N][N];
int n;
int ksm(int x,int y){
    int ret=1;
    while(y){
        if(y&1)ret=ret*x%mod;
        x=x*x%mod;y>>=1;
    }return ret;
}
int to[N*N],nxt[N*N],head[N],rp;
int du[N];
void add_edg(int x,int y){
    to[++rp]=y;
    du[y]++;
    nxt[rp]=head[x];
    head[x]=rp;
    return ;
}
int bl[N],cbl,siz[N];
int dfn[N],low[N],cnt;
bitset<N> bit[N],U,now[N];
stack<int> q;
int vit[N];
void tarjan(int x){
    //cout<<x<<" ";
    dfn[x]=low[x]=++cnt;
    q.push(x);vit[x]=true;
    for(int i=head[x];i;i=nxt[i]){
        int y=to[i];
        if(!dfn[y]){tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(vit[y])low[x]=min(low[x],dfn[y]);
    }
    if(dfn[x]==low[x]){
        bl[x]=++cbl;
        siz[cbl]=1;
        while(q.top()!=x){
            int t=q.top();q.pop();
            bl[t]=cbl;
            vit[t]=false;
            siz[cbl]++;
        }
        q.pop();vit[x]=false;
    }
    return ;
}
bool vis[N][N],via[N];
void dfs(int x){
    bit[x].set(x);via[x]=true;
    for(int i=head[x];i;i=nxt[i]){
        int y=to[i];
        if(!via[y])dfs(y);
        bit[x]|=bit[y];
    }
    return ;
}
int sm[N],ans;
signed main(){
    #ifdef oj
        freopen("f.in","r",stdin);
        freopen("f.out","w",stdout);
    #endif
    scanf("%lld",&n);
    fo(i,1,n){
        scanf("%s",s[i]+1);
        fo(j,1,n)if(s[i][j]=='1')add_edg(i,j);
    }
    fo(i,1,n)if(!du[i])tarjan(i);
    fo(i,1,n)if(!dfn[i])tarjan(i);
    //cout<<endl;
    //fo(i,1,n)cout<<bl[i]<<" ";cout<<endl;
    memset(head,0,sizeof(head));rp=0;
    memset(du,0,sizeof(du));
    fo(i,1,n){
        fo(j,1,n){
            if(vis[bl[i]][bl[j]])continue;
            if(s[i][j]=='1'){
                add_edg(bl[i],bl[j]);
                vis[bl[i]][bl[j]]=true;
            }
        }
    }
    fo(i,1,cbl)U.set(i);
    fo(i,1,cbl)if(!via[i])dfs(i);
    fo(i,1,cbl)fo(j,1,cbl){
        if(bit[i][j]==1)sm[j]+=siz[i];
    }
    
    //fo(i,1,cbl)cout<<siz[i]<<" "<<sm[i]<<endl;
    fo(i,1,n)ans=(ans+siz[i]*ksm(sm[i],mod-2))%mod;
    printf("%lld",ans);
    return 0;
}

T2 S

\(KMP\)自动机板子

AC_code
#include<bits/stdc++.h>
using namespace std;
#define oj
#define fo(i,x,y) for(int i=(x);i<=(y);i++)
#define fu(i,x,y) for(int i=(x);i>=(y);i--)
const int N=8005;
int n,m,ans;
char s[N],t[N];
int nxt[N],tans[N][26];
int f[N][N];
signed main(){
    #ifdef oj
        freopen("s.in","r",stdin);
        freopen("s.out","w",stdout);
    #endif
    //cout<<((sizeof(now)+sizeof(s)*10)>>20)<<endl;
    scanf("%s%s",s+1,t+1);
    n=strlen(s+1);
    m=strlen(t+1);
    if(m>n){printf("0");return 0;}
    for(int i=2,j=0;i<=m;i++){
        while(j&&t[i]!=t[j+1])j=nxt[j];
        if(t[i]==t[j+1])j++;
        nxt[i]=j;
    }
    tans[0][(int)(t[1]-'a')]=1;
    fo(i,1,m)fo(j,0,25)tans[i][j]=(t[i+1]-'a'==j?i+1:tans[nxt[i]][j]);
    fo(i,0,n)fo(j,0,m)f[i][j]=0x3f3f3f3f;
    f[0][0]=0;
    fo(i,0,n)fo(j,0,m-1){
        if(i<n)f[i+1][j]=min(f[i+1][j],f[i][j]+1);
        f[i+1][tans[j][s[i+1]-'a']]=min(f[i+1][tans[j][s[i+1]-'a']],f[i][j]);
    }ans=f[n][m-1];
    fo(i,0,m-2)ans=min(ans,f[n][i]);
    printf("%d",ans);
    return 0;
}

T3 Y

仍然不是很好写,我没有写出来

不调了。。

但是有遗憾的是,考场上没有打搜索,亏了

不管是啥题都要搜一搜

AC_code
#include<bits/stdc++.h>
using namespace std;
#define oj
#define fo(i,x,y) for(int i=(x);i<=(y);i++)
#define fu(i,x,y) for(int i=(x);i>=(y);i--)
const int N=35;
const int Q=505;
const int inf=0x3f3f3f3f;
int n,m,t,ans;
int jz[N][N];
int ex,ey,sx,sy,tx,ty;
int mx[4]={1,0,-1,0};
int my[4]={0,-1,0,1};
int num[N][N][4],ok[N][N][4];
void get_num(){int cnt=0;fo(i,1,n)fo(j,1,m)fo(k,0,3)num[i][j][k]=++cnt;}
void get_ok(){fo(i,1,n)fo(j,1,m)fo(k,0,3)ok[i][j][k]=(i+mx[k]>0&&i+mx[k]<=n&&j+my[k]>0&&j+my[k]<=n&&jz[i+mx[k]][j+my[k]]&&jz[i][j]);}
bool ifok(int x,int y){return (x>0&&x<=n&&y>0&&y<=m&&jz[x][y]);}
struct node{int x,y,sum;node(int a,int b,int c){x=a;y=b;sum=c;}};
bool vis[N][N],via[N*N*4];
int bfs(int ex,int ey,int sx,int sy,int tx,int ty){
    queue<node> q;while(!q.empty())q.pop();
    memset(vis,false,sizeof(vis));
    q.push(node(sx,sy,0));
    vis[sx][sy]=true;
    while(!q.empty()){
        node now=q.front();q.pop();
        if(now.x==tx&&now.y==ty)return now.sum;
        fo(i,0,3){
            int x=now.x+mx[i],y=now.y+my[i];
            if(!ifok(x,y))continue;
            if(x==ex&&y==ey)continue;
            if(vis[x][y])continue;
            vis[x][y]=true;
            q.push(node(x,y,now.sum+1));
        }
    }
    return inf;
}
int to[N*N*32],nxt[N*N*32],val[N*N*32],head[N*N*4],rp;
void add_edg(int x,int y,int z){
    to[++rp]=y;
    val[rp]=z;
    nxt[rp]=head[x];
    head[x]=rp;
}
void pre(){
    get_num();get_ok();
    fo(i,1,n)fo(j,1,m)fo(k,0,3)fo(l,k+1,3){
        if(ok[i][j][k]&&ok[i][j][l]){
            int v=bfs(i,j,i+mx[k],j+my[k],i+mx[l],j+my[l]);
            if(v==inf)continue;
            add_edg(num[i][j][k],num[i][j][l],v);
            add_edg(num[i][j][l],num[i][j][k],v);
        }
    }
    fo(i,1,n)fo(j,1,m){
        if(ok[i][j][0]&&ok[i+1][j][2]){
            add_edg(num[i][j][0],num[i+1][j][2],1);
            add_edg(num[i+1][j][2],num[i][j][0],1);
        }
    }
    fo(i,1,n)fo(j,1,m){
        if(ok[i][j][3]&&ok[i][j+1][1]){
            add_edg(num[i][j][3],num[i][j+1][1],1);
            add_edg(num[i][j+1][1],num[i][j][3],1);
        }
    }
}
int dis[N*N*4];
int jud(){
    int ret=inf;
    if(!jz[sx][sy]||!jz[tx][ty])return inf;
    if(sx==tx&&sy==ty)return 0;
    queue<int> q;
    memset(via,false,sizeof(via));
    memset(dis,0x3f,sizeof(dis));
    fo(i,0,3){
        if(ifok(sx+mx[i],sy+my[i])){
            int v=bfs(sx,sy,ex,ey,sx+mx[i],sy+my[i]);
            if(v==inf)continue;
            dis[num[sx][sy][i]]=v;
            q.push(num[sx][sy][i]);
            //cout<<dis[num[sx][sy][i]]<<endl;
            via[num[sx][sy][i]]=true;
        }
    }
    while(!q.empty()){
        int x=q.front();q.pop();via[x]=false;
        for(int i=head[x];i;i=nxt[i]){
            int y=to[i];
            if(dis[y]>dis[x]+val[i]){
                dis[y]=dis[x]+val[i];
                if(!via[y])q.push(y),via[y]=true;
            }
        }
    }
    fo(i,0,3)ret=min(ret,dis[num[tx][ty][i]]);//cout<<dis[num[tx][ty][i]]<<" ";cout<<endl;
    return ret;
}
signed main(){
    #ifdef oj
        freopen("y.in","r",stdin);
        freopen("y.out","w",stdout);
    #endif
    scanf("%d%d%d",&n,&m,&t);
    fo(i,1,n)fo(j,1,m)scanf("%d",&jz[i][j]);
    pre();fo(i,1,t){
        //cout<<"fuck"<<endl;
        scanf("%d%d%d%d%d%d",&ex,&ey,&sx,&sy,&tx,&ty);
        ans=jud();printf("%d\n",ans==inf?-1:ans);
    }
    return 0;
}

T4 O

拉格朗日差值咕咕咕

posted @ 2021-10-18 11:35  fengwu2005  阅读(41)  评论(0编辑  收藏  举报