QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#191217 | #6309. Aqre | ucup-team870# | RE | 1ms | 3464kb | C++17 | 3.1kb | 2023-09-29 20:35:19 | 2023-09-29 20:35:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define P pair<int,int>
#define ll long long
#define vi vector<int>
vi f[100]; int cnt;
const int NM=1e6+6;
const P dir[]={{-1,0},{1,0},{0,1},{0,-1}};
int fa[NM];
int fd(int x){
if(x==fa[x])return x;
return fa[x]=fd(fa[x]);
}
void slv(){
int n,m;cin>>n>>m;
if(n<4 && m<4){
cout<<n*m<<'\n';
rep(i,1,n){
rep(j,1,m)cout<<1; cout<<'\n';
}
return;
}
if(n==2 || m==2){
vector<vi>res;
int fl=0;
if(m==2)fl=1,swap(n,m);
res.resize(n+1);
rep(i,1,n)res[i].resize(m+1);
rep(j,1,m)res[1][j]=(j%4!=2);
rep(j,1,m)res[2][j]=(j%4!=0);
int num=0;
rep(i,1,n)rep(j,1,m)num+=res[i][j];
cout<<num<<'\n';
if(fl){
rep(j,1,m){
rep(i,1,n)cout<<res[i][j]; cout<<'\n';
}
}
else{
rep(i,1,n){
rep(j,1,m)cout<<res[i][j]; cout<<'\n';
}
}
return;
}
vector<vi>res(n+1);
rep(i,1,n)res[i].resize(m+1);
auto inok=[&](int x,int y){
return 1<=x && x<=n && 1<=y && y<=m;
};
auto cal=[&](int i,int j){
return (i-1)*m+j;
};
auto ck=[&](int cnt){ //cnt个1
// cout<<cnt<<endl;
// return true;
rep(i,1,n*m)fa[i]=i;
rep(i,1,n){
rep(j,1,m){
// cout<<res[i][j];
if(!res[i][j])continue;
rep(k,0,3){
int ii=i+dir[k].first,jj=j+dir[k].second;
if(inok(ii,jj) && res[ii][jj]){
int u=cal(i,j),v=cal(ii,jj);
u=fd(u),v=fd(v);
if(u!=v)--cnt,fa[u]=v;
}
}
}
// cout<<'\n';
}
return cnt==1;
};
int mi=1e9,ans;
rep(i,1,cnt){
int num=0;
rep(x,1,n){
rep(y,1,m){
if(f[i][(x-1)%4]%4==y%4)++num,res[x][y]=0;
else res[x][y]=1;
}
}
if(ck(n*m-num) && num<mi){
mi=num; ans=i;
}
}
cout<<n*m-mi<<'\n'; assert(mi==n*m/4);
rep(x,1,n){
rep(y,1,m)cout<<int(f[ans][(x-1)%4]%4!=y%4);
cout<<'\n';
}
}
void ck(vi q){
map<int,int>mp;
for(auto i:q)mp[i]=1;
if(mp.size()<4)return;
mp.clear();
rep(i,0,3)mp[(i+q[i])%4]=1;
if(mp.size()==1)return;
mp.clear();
rep(i,0,3)mp[(i-q[i]+4)%4]=1;
if(mp.size()==1)return;
f[++cnt]=q;
}
signed main(){
// IOS
rep(i1,1,4){
rep(i2,1,4){
rep(i3,1,4){
rep(i4,1,4){
ck(vector<int>({i1,i2,i3,i4}));
}
}
}
}
// rep(i,1,cnt){
// for(auto v:f[i])cout<<v<<' '; cout<<'\n';
// }
// return 0;
int T;cin>>T;
while(T--)slv();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3464kb
input:
3 2 2 3 4 3 8
output:
4 11 11 9 0111 1011 1110 18 01110111 10111011 11101110
result:
ok ok (3 test cases)
Test #2:
score: -100
Runtime Error
input:
361 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 5 1...
output:
4 11 11 6 111 111 6 1011 1110 8 10111 11101 9 101110 111011 11 1011101 1110111 12 10111011 11101110 14 101110111 111011101 15 1011101110 1110111011 17 10111011101 11101110111 18 101110111011 111011101110 20 1011101110111 1110111011101 21 10111011101110 11101110111011 23 101110111011101 1110111011101...