QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420112#7398. Triangle TilingBronyaRE 0ms5780kbC++142.1kb2024-05-24 14:42:442024-05-24 14:42:45

Judging History

你现在查看的是最新测评结果

  • [2024-05-24 14:42:45]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:5780kb
  • [2024-05-24 14:42:44]
  • 提交

answer

#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1

using namespace std;
int n;
char s[5005];
bool mp[5005][5005];
vector<pair<int,int> >vt;
vector<int>hav[5005];
char ans[5005][5005];
int fa[5005],fr[5005];
void Init(int x){
	fr[0]=0;
    for(int i=1;i<=x;i++)fa[i]=fr[i]=i;
}
int Find(int x){
    return (fa[x]!=x?fa[x]=Find(fa[x]):x);
}
void shaber(){
    puts("Impossible!");
}
void del(int x){
    int y=Find(x);
    if(y==x)fa[x]=Find(x-1),fr[Find(x-1)]=fr[x];
    else fa[fr[y]+1]=y,fr[y]=fr[fr[y]+1];
}
void Solve(){
    scanf("%d",&n);vt.clear();
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        for(int j=1;j<=i;j++){
            mp[i][j]=s[j]-'0';
            if(mp[i][j]==0)vt.push_back({i,j}),ans[i][j]='-';
        }
    }
    for(int i=n;i>=1;i--){
        for(int j=1;j<=i;j++)hav[j].clear();
        for(auto [x,y]:vt){
            if(x>=i)break;
            hav[y+(i-1-x)].push_back(y);
        }
        vector<int>sb;
        for(int j=1;j<=i;j++)
            if(mp[i][j]==0)sb.push_back(j);
        if(sb.empty())shaber();
        for(int j=1;j<sb[0];j++)ans[i][j]='3';
        for(int j=sb[sb.size()-1]+1;j<=i;j++)ans[i][j]='1';
        if(sb.size()==1)continue;
        Init(i+1);
        for(int j=1;j<sb[0];j++){
            for(auto x:hav[j])del(x);
            if(fa[j+1]!=j+1){
				shaber();
				return;
			}
        } 
        for(int j=0;j+1<sb.size();j++){
            int u=sb[j],v=sb[j+1],r=u-1;
            for(int k=u;k<v;k++){
                 for(auto x:hav[k]){
                 	del(x);
                 }
                if(fa[k+1]!=k+1){
					shaber();
					return;
				}
                if(Find(k)+1<=u)r=k;
            }
            if(r==v-1){
				shaber();
				return;
			}
            ans[i-1][r+1]='2';
            mp[i-1][r+1]=0;del(r+1);
            for(int k=u+1;k<v;k++)ans[i][k]=(k<=r+1?'1':'3');
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++)
            putchar(ans[i][j]);
        putchar('\n');
    }
    return;
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--)Solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5780kb

input:

1
4
0
11
010
1101

output:

-
21
-3-
33-1

result:

ok ok

Test #2:

score: -100
Runtime Error

input:

909
6
0
11
100
1111
11100
111110
7
0
11
011
1001
10111
100111
1111111
6
0
00
001
1111
11101
111111
8
1
01
111
1111
10111
101110
1101101
11111100
2
1
00
2
0
01
3
0
01
110
7
1
00
011
1010
11011
110111
1111111
8
1
11
111
1011
11010
011110
1101110
01111111
5
1
00
010
1111
10111
6
0
10
011
0101
01111
111...

output:


result: