QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#477733 | #9114. Black or White 2 | ucup-team1231# | TL | 184ms | 8044kb | C++14 | 2.9kb | 2024-07-14 09:33:27 | 2024-07-14 09:33:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ 2999999
typedef long long ll;
#define pb push_back
int n,m,k,ans=2e9,a[2000][2000];
string rst;
void dfs(int x,int y,int tt=0,int ca=0) {
if(ca>=ans) return;
if(x>=n) {
if(tt!=k) return;
ans=min(ans,ca);
rst.clear();
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) rst.pb(a[i][j]+'0');
rst.pb('\n');
}
return;
}
if(y>=m) {
dfs(x+1,0,tt,ca);
return;
}
for(a[x][y]=0;a[x][y]<=1;++a[x][y]) {
int caa=ca;
if(x&&y)
caa+=a[x][y]+a[x][y-1]+a[x-1][y]+a[x-1][y-1]==2;
dfs(x,y+1,tt+a[x][y],caa);
}
a[x][y]=0;
}
string mem[50][50][50];
int col[23333];
void sol() {
map<int,int> cc;
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
++cc[i+j];
}
int R=n+m-2;
for(int i=0;i<=R;++i) col[i]=-1;
vector<pair<int,int>> s,g;
int RR=4;
for(int i=4;i<=R-4;i+=2) {
s.pb({cc[i]+cc[i+1],i});
RR=i+2;
}
for(int i=0;i<4;++i) g.pb({cc[i],i});
for(int i=RR;i<=R;++i) g.pb({cc[i],i});
// for(auto t:g) cerr<<t.first<<" "; cerr<<"|"<<endl;
// for(auto t:s) cerr<<t.first<<" "; cerr<<endl;
while(1) {
random_shuffle(s.begin(),s.end());
int u[2]={k,n*m-k},ou[2];
for(auto c:s) {
int cc;
if(u[0]>=u[1]) u[cc=0]-=c.first;
else u[cc=1]-=c.first;
if(u[0]<0||u[1]<0) break;
col[c.second]=col[c.second+1]=cc;
}
if(u[0]<0||u[1]<0) continue;
ou[0]=u[0]; ou[1]=u[1];
for(int uu=1;uu<=100;++uu) {
random_shuffle(g.begin(),g.end());
for(auto t:g) col[t.second]=-1;
u[0]=ou[0]; u[1]=ou[1];
for(auto c:g) {
int cc;
if(u[0]>=u[1]) u[cc=0]-=c.first;
else u[cc=1]-=c.first;
if(u[0]<0||u[1]<0) break;
col[c.second]=cc;
}
if(u[0]<0||u[1]<0) continue;
for(int i=0;i<=R;++i)
if(col[i]==col[i+2]&&col[i]!=col[i+1]) goto nxt;
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
putchar(col[i+j]+48);
}
putchar(10);
}
// cout<<u[0]<<" "<<u[1]<<endl;
return;
nxt:;
}
}
}
int main() {
for(n=1;n<=20;++n)
for(m=1;n*m<=20;++m) {
for(k=0;k<=n*m;++k) {
ans=2e9;rst="";
dfs(0,0);
mem[n][m][k]=rst;
// cout<<ans<<"+"<<endl<<rst<<endl;
}
}
int T; cin>>T;
while(T--) {
cin>>n>>m>>k;
if(n*m<=20) printf("%s",mem[n][m][k].c_str());
else sol();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 184ms
memory: 8044kb
input:
2 2 2 2 2 3 0
output:
00 11 000 000
result:
ok Output is valid. OK.
Test #2:
score: -100
Time Limit Exceeded
input:
27520 2 2 0 2 2 1 2 2 2 2 2 3 2 2 4 2 3 0 2 3 1 2 3 2 2 3 3 2 3 4 2 3 5 2 3 6 3 2 0 3 2 1 3 2 2 3 2 3 3 2 4 3 2 5 3 2 6 3 3 0 3 3 1 3 3 2 3 3 3 3 3 4 3 3 5 3 3 6 3 3 7 3 3 8 3 3 9 2 4 0 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 2 4 6 2 4 7 2 4 8 3 4 0 3 4 1 3 4 2 3 4 3 3 4 4 3 4 5 3 4 6 3 4 7 3 4 8 3 4 9 3 4 10...
output:
00 00 00 01 00 11 01 11 11 11 000 000 000 001 000 101 001 011 010 111 011 111 111 111 00 00 00 00 00 01 01 00 01 00 01 11 01 11 01 01 11 11 11 11 11 000 000 000 000 000 001 000 000 101 000 001 011 000 010 111 000 101 111 001 011 111 010 111 111 011 111 111 111 111 111 0000 0000 0000 0001 0000 0101 0...