• P7115 [NOIP2020] 移球游戏 题解


    题目

    不太想说了,NOIP2020T3

    P7115 [NOIP2020] 移球游戏

    题解

    某位大佬写的

    代码

    #include<cstdio>
    #include<algorithm>
    int n,m;
    int ans[820001][2],len;
    struct line{
    	int a[405],top,id;
    	int pop(){int ret=a[top];a[top--]=0;return ret;}
    	void push(int x){a[++top]=x;}
    	int count(int x){
    		int ret=0;
    		for(int i=1;i<=top;i++)ret+=a[i]==x;
    		return ret;
    	}
    }p[55];
    struct IO{
        static const int S=1<<21;
        char buf[S],*p1,*p2;int st[105],Top;
        ~IO(){clear();}
        inline void clear(){fwrite(buf,1,Top,stdout);Top=0;}
        inline void pc(const char c){Top==S&&(clear(),0);buf[Top++]=c;}
        inline char gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
        inline IO&operator >> (char&x){while(x=gc(),x==' '||x=='
    '||x=='r');return *this;}
        template<typename T>inline IO&operator >> (T&x){
            x=0;bool f=0;char ch=gc();
            while(ch<'0'||ch>'9'){if(ch=='-') f^=1;ch=gc();}
            while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=gc();
            f?x=-x:0;return *this;
        }
        inline IO&operator << (const char c){pc(c);return *this;}
        template<typename T>inline IO&operator << (T x){
            if(x<0) pc('-'),x=-x;
            do{st[++st[0]]=x%10,x/=10;}while(x);
            while(st[0]) pc('0'+st[st[0]--]);return *this;
        }
    }fin,fout;
    void move(int x,int y,int num=1){  //从x移到y 
    	while(num--){
    		p[y].push(p[x].pop());
    		++len;ans[len][0]=p[x].id;ans[len][1]=p[y].id;
    	}
    	return ;
    }
    int main(){
    	freopen("P7115.in","r",stdin);
    	freopen("P7115.out","w",stdout);
    	fin>>n>>m;
    	for(int i=1;i<=n;i++){
    		p[i].top=m;p[i].id=i;
    		for(int j=1;j<=m;j++)fin>>p[i].a[j];
    	}
    	p[n+1].id=n+1;
    	if(n==2){
    		int p1_count_1=p[1].count(1);
    		move(2,3,p1_count_1);
    		while(p[1].top){if(p[1].a[p[1].top]==1)move(1,2);else move(1,3);}
    		move(2,1,p1_count_1);move(3,1,m-p1_count_1);move(3,2,p1_count_1);move(1,3,m-p1_count_1);
    		while(p[2].top){if(p[2].a[p[2].top]==1)move(2,1);else move(2,3);}
    	}
    	else {
    		for(int i=1;i<=n-2;i++){
    			int pi_count_x=p[i].count(i);
    			move(n,n+1,pi_count_x);
    			while(p[i].top)if(p[i].a[p[i].top]==i)move(i,n);else move(i,n+1);
    			move(n+1,i,m-pi_count_x);
    			while(p[i+1].top){//第i列成为全0列 
    				if(p[i].top==m||p[i+1].a[p[i+1].top]==i)move(i+1,n+1);else move(i+1,i);
    			}
    			std::swap(p[i],p[n]);std::swap(p[i+1],p[n+1]);//第n列成全零列
    			for(int j=i;j<n;j++){
    				int pj_count_x=p[j].count(i);
    				move(n,n+1,pj_count_x);
    				while(p[j].top)if(p[j].a[p[j].top]==i)move(j,n);else move(j,n+1);
    				std::swap(p[j],p[n]);std::swap(p[n],p[n+1]);
    			}
    			for(int j=i;j<n;j++){
    				while(p[j].a[p[j].top]==i) move(j,n+1);
    				while(p[j].top!=m) move(n,j);
    			}
    			std::swap(p[i],p[n+1]);std::swap(p[n],p[n+1]);
    		}
    		std::swap(p[n-1],p[1]);std::swap(p[n],p[2]);std::swap(p[n+1],p[3]);
    		int p1_count_1=p[1].count(n-1);
    		move(2,3,p1_count_1);
    		while(p[1].top){if(p[1].a[p[1].top]==n-1)move(1,2);else move(1,3);}
    		move(2,1,p1_count_1);move(3,1,m-p1_count_1);move(3,2,p1_count_1);move(1,3,m-p1_count_1);
    		while(p[2].top){if(p[2].a[p[2].top]==n-1)move(2,1);else move(2,3);}
    	}
    	printf("%d
    ",len);
    	for(int i=1;i<=len;i++)fout<<ans[i][0]<<' '<<ans[i][1]<<'
    ';
    	return 0;
    }
    
  • 相关阅读:
    mysql分表和表分区详解
    CNN 文本分类
    基于深度学习的目标检测研究进展
    标注工具
    在Ubuntu登陆界面输入密码之后,黑屏一闪后,又跳转到登录界面
    R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO, SSD系列深度学习检测方法梳理
    LeNet,AlexNet,GoogleLeNet,VggNet等网络对比
    nginx使用与配置入门指南
    CentOS 7上重新编译安装nginx
    酸汤肉沫豆腐
  • 原文地址:https://www.cnblogs.com/martian148/p/15347505.html
Copyright © 2020-2023  润新知