zoukankan      html  css  js  c++  java
  • 【TJOI2019 Day2】简要题解

    T1:

    传送门
    傻逼题
    考虑到kk很小,把2k+12k+1种暴力建出来跑dijdij就可以了
    结果出题人竟然不卡优先队列的O(n+mklog)O(n+mklog)写法
    跑的还比setO(m+nklog)set的O(m+nklog)快……

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define ll long long
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    inline void chemx(int &a,int b){a<b?a=b:0;}
    inline void chemn(int &a,int b){a>b?a=b:0;}
    cs int N=300005;
    vector<pii> e[N];
    int n,m,k,dis[N],vis[N],val[N],id[10005][23],tot,str,des;
    inline void addedge(int u,int v,int w){
    	for(int i=1;i<=2*k+1;i++)
    	if(id[v][i+val[v]])e[id[u][i]].pb(pii(id[v][i+val[v]],w));
    }
    priority_queue<pii,vector<pii>,greater<pii> >q;
    inline int dijkstra(){
    	memset(dis,127,sizeof(int)*(tot+1));
    	memset(vis,0,sizeof(int)*(tot+1));
    	int s=id[str][k+1+val[str]];
    	dis[s]=0,q.push(pii(0,s));
    	while(!q.empty()){
    		int u=q.top().se;q.pop();
    		if(vis[u])continue;
    		vis[u]=1;
    		for(pii &x:e[u]){
    			if(dis[x.fi]>dis[u]+x.se){
    				dis[x.fi]=dis[u]+x.se;
    				q.push(pii(dis[x.fi],x.fi));
    			}
    		}
    	}
    	int res=dis[0];
    	for(int i=1;i<=2*k+1;i++)chemn(res,dis[id[des][i]]);
    	return res==dis[0]?-1:res;
    }
    inline void solve(){
    	n=read(),m=read(),k=read(),tot=0;
    	for(int i=1,l=2*k+1;i<=n;i++)for(int j=1;j<=l;j++)id[i][j]=++tot;
    	for(int i=1;i<=n;i++)val[i]=read()==2?1:-1;
    	for(int i=1;i<=m;i++){
    		int u=read(),v=read(),w=read();
    		addedge(u,v,w),addedge(v,u,w);
    	}
    	str=read(),des=read();
    	cout<<dijkstra()<<'
    ';
    	for(int i=1;i<=tot;i++){
    		e[i].clear();
    	}
    	for(int i=1;i<=n;i++)
    	for(int j=1;j<=2*k+1;j++)id[i][j]=0;
    }
    int main(){
    	int T=read();
    	while(T--)
    		solve();
    }
    

    T2:

    传送门
    傻逼题
    SamSam建出来取rightright集合大小为kk
    做区间加可以写前缀和

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define ll long long
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    inline void chemx(int &a,int b){a<b?a=b:0;}
    inline void chemn(ll &a,ll b){a>b?a=b:0;}
    cs int N=200005;
    int tt1,tt2;
    
    namespace Sam{
    	int nxt[N][26],fail[N],len[N],siz[N],tot,last;
    	inline void init(){tot=last=1;}
    	inline void insert(int c){
    		int cur=++tot,p=last;last=tot;
    		len[cur]=len[p]+1,siz[cur]=1;
    		for(;p&&!nxt[p][c];p=fail[p])nxt[p][c]=cur;
    		if(!p)fail[cur]=1;
    		else{
    			int q=nxt[p][c];
    			if(len[p]+1==len[q])fail[cur]=q;
    			else{
    				int clo=++tot;
    				memcpy(nxt[clo],nxt[q],sizeof(nxt[q]));
    				len[clo]=len[p]+1,fail[clo]=fail[q];
    				for(;p&&nxt[p][c]==q;p=fail[p])nxt[p][c]=clo;
    				fail[q]=fail[cur]=clo;
    			}
    		}
    	}
    	int a[N],b[N],buc[N],tag[N];
    	inline void buc_sort(int k){
    		for(int i=1;i<=tot;i++)a[len[i]]++;
    		for(int i=1;i<=tot;i++)a[i]+=a[i-1];
    		for(int i=tot;i;i--)b[a[len[i]]--]=i;
    		for(int i=tot;i;i--)siz[fail[b[i]]]+=siz[b[i]];
    		for(int i=1;i<=tot;i++){
    			if(siz[i]==k)
    			tag[len[i]+1]--,tag[len[fail[i]]+1]++;
    		}
    		for(int i=1;i<=tot;i++)buc[i]=buc[i-1]+tag[i];
    		int mx=0;
    		for(int i=tot;i;i--){
    			if(buc[i]>buc[mx])mx=i;
    		}
    		if(mx)cout<<mx<<'
    ';
    		else cout<<-1<<'
    ';
    	}
    	inline void clear(){
    		for(int i=1;i<=tot;i++){
    			memset(nxt[i],0,sizeof(nxt[i]));
    			fail[i]=len[i]=siz[i]=a[i]=b[i]=buc[i]=tag[i]=0;
    		}
    	}
    }
    char s[N];
    int k;
    int main(){
    	int T=read();
    	while(T--){
    		scanf("%s",s+1);
    		Sam::init();
    		for(int i=1,len=strlen(s+1);i<=len;i++)Sam::insert(s[i]-'a');
    		k=read(),Sam::buc_sort(k),Sam::clear();
    	}
    }
    

    T3:

    传送门

    其实是原题,CF750GCF 750G
    题解传送门

    第一问直接暴力跳就可以了
    不过这里限制了树高
    需要判一下(调了一万年)

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define ll long long
    inline ll readl(){
        char ch=gc();
        ll res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    cs int N=55;
    int d,c;
    ll a,b,bin[N];
    inline ll solve1(ll x,ll y){
    	ll res=0;
    	while(x!=y){
    		if(x<y)swap(x,y);
    		res+=x,x>>=1;
    	}
    	res+=x;return res;
    }
    ll f[N][N*2][2];
    inline ll dp(ll mx,int l,int r,int t){
    	memset(f,0,sizeof(f));
    	f[0][0][0]=1;int len=1;
    	for(int i=1;bin[i-1]<=mx;i++,len++){
    		int d=(mx>>i)&1;
    		for(int j=0;j<=t;j++){
    			for(int x=0;x<=1;x++)
    			if(x!=1||i<l)
    			for(int y=0;y<=1;y++)
    			if(y!=1||i<r){
    				if((x+y)%2==d)f[i][j+x+y][(x+y)/2]+=f[i-1][j][0];
    				else f[i][j+x+y][(x+y+1)/2]+=f[i-1][j][1];
    			}
    		}
    	}
    	return f[len-1][t][0];
    }
    inline ll solve2(ll s){
    	ll ans=0;
    	for(int i=0;i<d&&bin[i]<=s;i++)
    	for(int j=0;j<d&&bin[j]<=s;j++){
    		ll x=(s-bin[j]+1)/(bin[i+1]+bin[j+1]-3);
    		if(max(i,j)+1+(int)log2(x)>d)continue;
    		if(!x)continue;
    		ll res=s-bin[j]+1-(bin[i+1]+bin[j+1]-3)*x;
    		if(!res){ans++;continue;}
    		for(int k=1;k<=i+j;k++)
    		if((res+k)%2==0)ans+=(dp(res+k,i,j,k));
    	}
    	return ans-1;
    }
    inline void solve(){
    	d=read(),a=readl(),b=readl(),c=read();
    	ll now=solve1(a,b);
    	if(c==1)cout<<now<<'
    ';
    	if(c==2)cout<<solve2(now)<<'
    ';
    }
    int main(){
    	for(int i=0;i<N;i++)bin[i]=1ll<<i;
    	int T=read();
    	while(T--)solve();
    }
    
  • 相关阅读:
    linux分区
    MySQL
    RGB中的颜色的设置
    解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
    解决运行scrapy是报错No module named cryptography,解决cryptography的安装问题,解决libffi的安装问题
    解决pycharm下安装reportLab报错的问题
    简单的爬取并下载图片的程序
    linux常用命令
    ubuntu下安装pycharm的方法
    win7下装ubuntu双系统后无法进入win7的解决方法
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328594.html
Copyright © 2011-2022 走看看