https://ac.nowcoder.com/acm/contest/5668/D

出题人题解如下:(比较懒,不想写了

 

 

 

 

 

 

 

 

 

 

 

 

 代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN=300;
const int SIZE=3e5+5;
const long long mod=998244353;
typedef long long ll;
//typedef __int128 LL;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3f;
int g[205][205];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(g,0,sizeof g);
        int n,m;
        scanf("%d%d",&n,&m);
        int flag=0;
        int minn=inf;
        int a,b;
        if(m%2)flag=1;
        if(m>4*n)flag=1;
        
        int tmp=sqrt(n);
        if(tmp*tmp==n)a=tmp,b=tmp,minn=2*(tmp+tmp);
        else if(tmp*(tmp+1)>=n)a=tmp,b=tmp+1,minn=2*(tmp+tmp+1);
        else
        {
            a=tmp+1;
            b=a;
            minn=2*(tmp+1+tmp+1);
        }
        /*for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i*j>=n&&2*(i+j)<minn)
                {
                    a=i;
                    b=j;
                    minn=2*(i+j);
                }
            }
        }*/
        if(m<minn)flag=1;
        if(flag==1)
        {
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        int id=55,cnt=0;
        for(int i=1;i<=a;i++)
        {
            for(int j=1;j<=b;j++)
            {
                if(cnt==n)break;
                g[i][j]=1;
                cnt++;
            }
        }
        for(int i=1;i<=a;i++)
        {
            for(int j=1;j<=b;j++)
            {
                if(m>minn)
                {
                    if(g[i][j+1]==0)minn-=2;
                    if(g[i+1][j]==0)minn-=2;

                    g[i][j]=0,g[id][id]=1,id++,minn+=4;

                }
                else break;
            }
        }
        if(minn>m)
        {
            if(g[id-2][id-2]==0)g[id-1][id-1]=0,g[a+1][1]=1;
            else g[id-1][id-1]=0,g[id-2][id-1]=1;
        }
        for(int i=1;i<=200;i++)
        {
            for(int j=1;j<=200;j++)
            {
                if(g[i][j])printf("%d %d\n",i,j);
            }
        }
    }
    return 0;
}
View Code

 

posted on 2020-07-20 20:55  MZRONG  阅读(210)  评论(0编辑  收藏  举报