QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103645 | #6309. Aqre | MIT01# | TL | 8ms | 13920kb | C++17 | 3.6kb | 2023-05-07 05:46:16 | 2023-05-07 05:46:18 |
Judging History
answer
#pragma GCC optimize("-Ofast","-ffast-math","-funroll-all-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pl pair<ll, ll>
#define db double
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int T,n,m,f[2333333],go[2333333],ans=-1;
pair<int,int> ps[2333333];
int g[2333333],V=0,cc;
int N,M;
void dfs2(int u) {
if(f[u]==0) return;
if(g[u]==V) return;
g[u]=V; ++cc;
int x=u/m,y=u%m;
// cout<<x<<y<<"!!"<<u<<"\n";
if(x) dfs2(u-m);
if(x+1<N) dfs2(u+m);
if(y) dfs2(u-1);
if(y+1<M) dfs2(u+1);
}
bool chk(int ss,int ay,int real=1) {
assert(ay!=-1);
++V; cc=0;
dfs2(ay);
if(!real) {
if(cc>=ss-(N-M)*3) return 0;
return 1;
}
if(cc!=ss) return 1;
// cerr<<cc<<" "<<ss<<"\n";
ans=ss;
// for(int i=0;i<n;++i) {
// for(int j=0;j<m;++j) go[i*m+j]=f[i*m+j];
// }
memcpy(go,f,sizeof(int)*(n*m));
// for(int i=0;i<n;++i) {
// for(int j=0;j<m;++j)
// printf("%d",f[i*m+j]);
// puts("");
// }
// cout<<"GG\n";
return 0;
}
int pp[4][4];
int tc=0,fc=0,lim;
void dfs(int d,int ss=-1,int ay=-1,int safe=-1) {
if(ss==-1&&d==n*m) {
ss=0;
for(int i=0;i<4&&i<n;++i)
for(int j=0;j<4&&j<m;++j)
ss+=pp[i][j]*f[i*m+j];
// cout<<ss<<"!!\n";
}
if(ss!=-1&&ss<=ans) return;
if(d>=n*m) {
++tc;
//cout<<"checking"<<ss<<"\n";
N=n,M=m;
chk(ss,ay);
return;
}
int x=ps[d].fi,y=ps[d].se;
if(x+y>=55&&safe==-1) {
// cout<<"sc"<<d<<"!!!\n";
N=min(n,27);
M=min(m,27);
int ss_=0,ay_=-1;
// for(int i=0;i<N;++i,cout<<"\n")
// for(int j=0;j<M;++j)
// cout<<f[i*m+j]<<"|";
for(int i=0;i<N;++i)
for(int j=0;j<M;++j) {
if(f[i*m+j]==1) ++ss_,ay_=i*M+j;
}
// cerr<<N<<","<<M<<" "<<ss<<","<<ay<<"!\n";
if(chk(ss_,ay_,0)) {
// cerr<<"FA\n";
return;
}
safe=0;
}
// cout<<d<<"!!"<<ss<<" "<<x<<","<<y<<"\n";
int L=0,R=1,sss=0;
if(x>=4) L=R=f[(x-4)*m+y],sss=1;
else if(y>=4) L=R=f[x*m+y-4],sss=1;
if(sss==1&&ss==-1) {
ss=0;
int zr=0;
for(int i=0;i<4&&i<n;++i)
for(int j=0;j<4&&j<m;++j)
ss+=pp[i][j]*f[i*m+j],zr+=f[i*m+j]==0;
if(zr>lim) return;
}
for(int t=R;t>=L;--t) {
f[x*m+y]=t;
if(x>=3) {
int o=1;
for(int u=x-3;u<x&&o;++u)
if(f[u*m+y]!=t) o=0;
if(o) continue;
}
if(y>=3) {
int o=1;
for(int u=y-3;u<y&&o;++u)
if(f[x*m+u]!=t) o=0;
if(o) continue;
}
dfs(d+1,ss,t?(x*m+y):ay,safe);
}
f[x*m+y]=-1;
}
void sol() {
cin>>n>>m; ans=0;
memset(pp,0,sizeof pp);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) ++pp[i%4][j%4];
for(int i=0;i<n*m;++i)
ps[i]=mp(i/m,i%m);
sort(ps,ps+n*m,[&](pair<int,int>a,pair<int,int>b) {return a.fi+a.se<b.fi+b.se;});
stable_sort(ps,ps+n*m,[&](pair<int,int>a,pair<int,int>b) {return (a.fi<4&&a.se<4)>(b.fi<4&&b.se<4);});
lim=4;
while(lim<=6)
dfs(0),++lim;
printf("%d\n",ans);
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j)
printf("%d",go[i*m+j]);
puts("");
}
}
int main() {
memset(f,-1,sizeof f);
cin>>T;
while(T--) sol();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 13920kb
input:
3 2 2 3 4 3 8
output:
4 11 11 9 1110 1110 1110 18 11101110 11101110 10111011
result:
ok ok (3 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 1110 1110 8 11101 10111 9 111011 101110 11 1110111 1011101 12 11101110 10111011 14 111011101 101110111 15 1110111011 1011101110 17 11101110111 10111011101 18 111011101110 101110111011 20 1110111011101 1011101110111 21 11101110111011 10111011101110 23 111011101110111 1011101110111...