Codeforces Round #654 (Div. 2) D. Grid-00100(構造)

題目鏈接

思路:題目要求價值最小,怎麼放價值最小?每次放的地方行和列最大值最小值之差爲1,那麼當行或者列%n==0時就從頭開始此時的行列最大值最小值之差依舊是1

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e4+7;
const int N=2e5+10;
const int inf=0x7f7f7f7f;


ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
        write(x / 10);
    putchar('0' + x % 10);
}


int a[305][305],R[305],C[305];
int main()
{
    SIS;
   int t;
   cin>>t;

   while(t--)
   {
       memset(R,0,sizeof R);
       memset(C,0,sizeof C);
       memset(a,0,sizeof a);
       int n,k;
       cin>>n>>k;
       int x=0,y=0;
       int cnt=0;
       while(k--)
       {
           a[x][y]=1;
           R[x]++;
           C[y]++;
           x++;
           y++;
           cnt++;
           x%=n;
           y%=n;
           if(cnt==n){
            y++;y%=n;cnt=0;
           }
       }
       int mxr=0,mnr=inf,mxc=0,mnc=inf;
       for(int i=0;i<n;i++)
       {
           mxr=max(mxr,R[i]);
           mnr=min(mnr,R[i]);
           mxc=max(mxc,C[i]);
           mnc=min(mnc,C[i]);

       }
       int ans=(mxr-mnr)*(mxr-mnr)+(mxc-mnc)*(mxc-mnc);
       cout<<ans<<endl;
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
            cout<<a[i][j];
           cout<<endl;
       }
   }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章