QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#336876#7398. Triangle TilingKevin5307WA 3ms11784kbC++203.9kb2024-02-24 23:11:192024-02-24 23:11:19

Judging History

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

  • [2024-02-24 23:11:19]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:11784kb
  • [2024-02-24 23:11:19]
  • 提交

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 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];
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=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!"<<endl;
			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!"<<endl;
			continue;
		}
		memset(token,0,sizeof(token));
		for(int i=1;i<=n;i++)
			if(grid[n][i]=='1')
				token[i]=1;
		for(int i=1;i<=n;i++)
			segt[i].build(1,1,n);
		for(int i=1;i<=n;i++)
			for(int j=i;j<=n;j++)
				segt[j].update(1,1,n,i,i,val[i][j]);
		for(int i=n;i>1;i--)
		{
			static bool ntoken[5005];
			memset(ntoken,0,sizeof(ntoken));
			for(int j=1;j<=i;j++)
				if(token[j])
				{
					int mn=segt[j+n-i].query(1,1,n,1,j);
					if(mn)
					{
						assert(j!=i);
						ntoken[j]=1;
						segt[j+n-i].update(1,1,n,1,j,-1);
						ans[i][j]='3';
					}
					else
					{
						int p=i;
						while(p&&grid[p][j-(i-p)]=='1')
						{
							ans[p][j-(i-p)]='1';
							p--;
						}
						assert(p);
						for(int k=j+n-i;k<=n;k++)
							segt[k].update(1,1,n,j-(i-p)+1,j,-1);
					}
				}
			memcpy(token,ntoken,sizeof(token));
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=i;j++)
				cout<<ans[i][j];
			cout<<endl;
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9760kb

input:

1
4
0
11
010
1101

output:

-
21
-3-
33-1

result:

ok ok

Test #2:

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

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
22-2-
-2212-
21-313-
-3333331
Impossible!
Impossible!
Impossible!
Impossible!
2
--
2
--
Impossible!
-
21
--1
-
-1
Impossible!
2
22
2--
-3-1
Impossible!
2
-2
-2-
2121
-2121
3221-1
3--3211
3333-...

result:

wrong answer duplicate tile