QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#337854#7398. Triangle TilingKevin5307WA 3ms14128kbC++235.3kb2024-02-25 15:13:182024-02-25 15:13:19

Judging History

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

  • [2024-02-25 15:13:19]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:14128kb
  • [2024-02-25 15:13:18]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
char grid[5005][5005];
int val[5005][5005];
bool token[5005];
class segtree
{
	public:
		#define ls (x<<1)
		#define rs (ls|1)
		int mn[5005<<2];
		int tag[5005<<2];
		void build(int x,int l,int r)
		{
			mn[x]=0;
			tag[x]=0;
			if(l==r) return ;
			int mid=(l+r)/2;
			build(ls,l,mid);
			build(rs,mid+1,r);
		}
		void build2(int x,int l,int r,int tr)
		{
			if(l<=tr)
				mn[x]=val[l][tr];
			tag[x]=0;
			if(l==r) return ;
			int mid=(l+r)/2;
			build2(ls,l,mid,tr);
			build2(rs,mid+1,r,tr);
		}
		void pushdown(int x,int l,int r)
		{
			mn[x]+=tag[x];
			if(l!=r)
			{
				tag[ls]+=tag[x];
				tag[rs]+=tag[x];
			}
			tag[x]=0;
		}
		int query(int x,int l,int r,int ql,int qr)
		{
			pushdown(x,l,r);
			if(l==ql&&r==qr)
				return mn[x];
			int mid=(l+r)/2;
			if(qr<=mid)
				return query(ls,l,mid,ql,qr);
			if(ql>mid)
				return query(rs,mid+1,r,ql,qr);
			return min(query(ls,l,mid,ql,mid),query(rs,mid+1,r,mid+1,qr));
		}
		void pushup(int x,int l,int r)
		{
			int mid=(l+r)/2;
			pushdown(ls,l,mid);
			pushdown(rs,mid+1,r);
			mn[x]=min(mn[ls],mn[rs]);
		}
		void update(int x,int l,int r,int ql,int qr,int v)
		{
			pushdown(x,l,r);
			if(l==ql&&r==qr)
			{
				tag[x]+=v;
				return ;
			}
			int mid=(l+r)/2;
			if(ql<=mid)
				update(ls,l,mid,ql,min(mid,qr),v);
			if(qr>mid)
				update(rs,mid+1,r,max(mid+1,ql),qr,v);
			pushup(x,l,r);
		}
		#undef ls
		#undef rs
}segt[5005];
char ans[5005][5005];
bool used[5005][5005];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			string s;
			cin>>s;
			for(int j=0;j<i;j++)
				grid[i][j+1]=s[j];
		}
		for(int i=1;i<n;i++)
			for(int j=1;j<=i;j++)
				used[i][j]=0;
		for(int i=1;i<=n;i++)
			for(int j=i;j<=n;j++)
				val[i][j]=0;
		vector<pii> vec;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=i;j++)
				if(grid[i][j]=='0')
				{
					ans[i][j]='-';
					vec.pb(i,j);
					val[j][j+n-i]++;
				}
				else
					ans[i][j]='2';
		if(sz(vec)!=n)
		{
			cout<<"Impossible!\n";
			continue;
		}
		for(int len=1;len<=n;len++)
			for(int i=1,j=len;j<=n;i++,j++)
				val[i][j]+=val[i+1][j]+val[i][j-1]-val[i+1][j-1];
		bool flag=1;
		for(int len=1;len<=n;len++)
			for(int i=1,j=len;j<=n;i++,j++)
			{
				val[i][j]-=len;
				if(val[i][j]>0)
					flag=0;
				val[i][j]=-val[i][j];
			}
		srt(vec);
		for(int i=0;i<n;i++)
			if(vec[i].first<=i)
				flag=0;
		if(!flag)
		{
			cout<<"Impossible!\n";
			continue;
		}
		memset(token,0,sizeof(token));
		for(int i=1;i<=n;i++)
			if(grid[n][i]=='1')
				token[i]=1;
		for(int j=1;j<=n;j++)
			segt[j].build2(1,1,n,j);
		bool flag_=0;
		for(int i=n;i>1;i--)
		{
			for(int j=1;j<=i;j++)
				if(token[j]&&grid[i][j]=='0')
					token[j]=0;
			static bool ntoken[5005];
			memset(ntoken,0,sizeof(ntoken));
			int lst=inf;
			for(int j=i;j>=1;j--)
			{
				flag_=0;
				if(token[j])
				{
					int mn=segt[j+n-i].query(1,1,n,1,j);
					if(mn&&!used[i-1][j]&&j!=i)
					{
						assert(j!=i);
						ntoken[j]=1;
						segt[j+n-i].update(1,1,n,1,j,-1);
						ans[i][j]='3';
					}
					else if(!used[i-1][j])
					{
						int p=i;
						while(p&&grid[p][j-(i-p)]=='1')
						{
							ans[p][j-(i-p)]='1';
							p--;
							used[p][j-(i-p)]=1;
						}
						// assert(p);
						for(int k=j+n-i;k<=n;k++)
							segt[k].update(1,1,n,j-(i-p)+1,j,-1);
					}
					else
					{
						int p=i-1;
						ntoken[j-1]=1;
						used[p][j-(i-p)]=1;
						ans[i][j]='1';
						// for(int k=j+n-i;k<=n;k++)
						// 	segt[k].update(1,1,n,j-(i-p)+1,j,-1);
						if(lst==inf)
							lst=j;
						flag_=1;
					}
				}
				if(lst!=inf&&!flag_)
				{
					// cerr<<"! "<<j+1<<" "<<lst<<endl;
					for(int k=j+1+n-i;k<=n;k++)
						segt[k].update(1,1,n,j+1,min(k-n+i,lst),-1);
					lst=inf;
				}
			}
			if(lst!=inf)
			{
				// cerr<<1<<" "<<lst<<endl;
				for(int k=1+n-i;k<=n;k++)
					segt[k].update(1,1,n,1,min(k-n+i,lst),-1);
			}
			memcpy(token,ntoken,sizeof(token));
		}
		bool flag2=1;
		for(int i=1;i<=n;i++)
		{
			if(ans[i][i]=='3'||ans[i][1]=='1')
				flag2=0;
			for(int j=2;j<=i;j++)
				if(ans[i][j-1]=='3'&&ans[i][j]=='1')
					flag2=0;
			for(int j=1;j<=i;j++)
				if(ans[i][j]=='3'&&ans[i-1][j]=='2')
					flag2=0;
			for(int j=2;j<=i;j++)
				if(ans[i][j]=='1'&&ans[i-1][j-1]=='2')
					flag2=0;
		}
		if(!flag2)
		{
			cout<<"Impossible!\n";
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=i;j++)
				cout<<ans[i][j];
			cout<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
4
0
11
010
1101

output:

-
21
-3-
33-1

result:

ok ok

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 14128kb

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:

-
32
3--
3332
333--
33333-
Impossible!
Impossible!
Impossible!
2
--
-
-1
-
-1
33-
Impossible!
2
22
222
2-22
23-2-
-3213-
33-333-
-1111111
Impossible!
Impossible!
Impossible!
Impossible!
2
--
2
--
Impossible!
-
21
--1
-
-1
Impossible!
2
22
2--
-3-1
Impossible!
2
-2
-1-
2111
-2111
3233-1
3--1111
3333-...

result:

wrong answer jury has a solution while user does not