QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#340030#7398. Triangle TilingKevin5307WA 2ms18048kbC++204.1kb2024-02-28 12:39:312024-02-28 12:39:31

Judging History

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

  • [2024-02-28 12:39:31]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:18048kb
  • [2024-02-28 12:39:31]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#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 ls (x<<1)
#define rs (ls|1)
void die(string S){puts(S.c_str());exit(0);}
using ll=long long;
using ull=unsigned ll;
using longer=__int128_t;
const int maxn=5005;
int ans[maxn][maxn];
int val[maxn][maxn];
char grid[maxn][maxn];
bool token[maxn];
int segt[maxn][maxn<<2],tag[maxn][maxn<<2];
bool used[maxn][maxn];
void build(int ind,int x,int l,int r)
{
	if(l==r)
	{
		segt[ind][x]=val[l][ind];
		return ;
	}
	int mid=(l+r)/2;
	build(ind,ls,l,mid);
	build(ind,rs,mid+1,r);
	segt[ind][x]=max(segt[ind][ls],segt[ind][rs]);
}
int query(int ind,int x,int l,int r,int qr)
{
	if(qr<l) return -inf;
	if(qr==r) return segt[ind][x];
	int mid=(l+r)/2;
	if(qr<=mid) return query(ind,ls,l,mid,qr)+tag[ind][x];
	return max(segt[ind][ls],query(ind,rs,mid+1,r,qr))+tag[ind][x];
}
void update(int ind,int x,int l,int r,int qr,int v)
{
	if(qr<l) return ;
	if(r==qr)
	{
		tag[ind][x]+=v;
		segt[ind][x]+=v;
		return ;
	}
	int mid=(l+r)/2;
	if(qr<=mid)
		update(ind,ls,l,mid,qr,v);
	else
	{
		update(ind,ls,l,mid,mid,v);
		update(ind,rs,mid+1,r,qr,v);
	}
	segt[ind][x]=max(segt[ind][ls],segt[ind][rs])+tag[ind][x];
}
int Main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string s;
		cin>>s;
		for(int j=1;j<=i;j++)
			grid[i][j]=s[j-1];
	}
	for(int i=1;i<=n;i++)
		for(int j=i;j<=n;j++)
			val[i][j]=used[i][j]=0;
	int cnt=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=i;j++)
			if(grid[i][j]=='0')
			{
				cnt++;
				val[j][j+n-i]++;
			}
	if(cnt!=n)
	{
		cout<<"Impossible!";
		return 0;
	}
	for(int i=1;i<=n;i++)
		val[i][i]--;
	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];
	int mx=0;
	for(int i=1;i<=n;i++)
		for(int j=i;j<=n;j++)
			mx=max(mx,val[i][j]);
	if(mx)
	{
		cout<<"Impossible!";
		return 0;
	}
	for(int i=1;i<=n;i++)
		build(i,1,1,i);
	for(int i=1;i<=n;i++)
		token[i]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=i;j++)
			if(grid[i][j]=='0')
				ans[i][j]=0;
			else
				ans[i][j]=2;
	for(int i=n;i>=1;i--)
	{
		for(int j=1;j<=i;j++)
			if(grid[i][j]=='0')
			{
				if(token[j]) token[j]=0;
				else if(!used[i][j])
				{
					cout<<"Impossible!";
					return 0;
				}
			}
		static bool ntoken[maxn];
		memset(ntoken,0,sizeof(ntoken));
		int cl=inf,cr=inf;
		for(int j=i;j>=1;j--)
		{
			if(token[j])
			{
				int mn=query(j+n-i,1,1,j+n-i,j);
				if(j!=i&&mn<0&&!used[i-1][j]&&!ntoken[j])
				{
					ntoken[j]=1;
					update(j+n-i,1,1,j+n-i,j,1);
					ans[i][j]=3;
				}
				else if(mn>=0||j==i)
				{
					int p=i;
					while(p>=1&&j>(i-p)&&grid[p][j-(i-p)]!='0')
					{
						ans[p][j-(i-p)]=1;
						p--;
						used[p][j-(i-p)]=1;
					}
					if(!p||j==i-p)
					{
						cout<<"Impossible!";
						return 0;
					}
					for(int k=j+n-i;k<=n;k++)
					{
						update(k,1,1,k,j,1);
						update(k,1,1,k,j-(i-p),-1);
					}
				}
				else
				{
					ans[i][j]=1;
					ntoken[j-1]=1;
					if(cl==inf)
						cl=cr=j;
					else
						cl=j;
				}
			}
			if(cl>j&&cl!=inf)
			{
				for(int k=cl+n-i;k<=n;k++)
				{
					update(k,1,1,k,min(cr,k-n+i),1);
					update(k,1,1,k,cl-1,-1);
				}
				cl=cr=inf;
			}
		}
		if(cl==1)
			for(int k=cl+n-i;k<=n;k++)
				update(k,1,1,k,min(cr,k-n+i),1);
		memcpy(token,ntoken,sizeof(token));
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
			cout<<"-123"[ans[i][j]];
		cout<<'\n';
	}
	return 0;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
		Main();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
4
0
11
010
1101

output:

-
21
-3-
33-1

result:

ok ok

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 18048kb

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!Impossible!Impossible!Impossible!Impossible!Impossible!2
--
2
--
Impossible!-
21
--1
-
-1
Impossible!Impossible!Impossible!Impossible!Impossible!Impossible!2
--
1-1
2
--
13-
Impossible!Impossible!Impossible!Imp...

result:

wrong answer jury does not has a solution while user does