萌新悬线法求助40WA
查看原帖
萌新悬线法求助40WA
196899
lndjy楼主2020/5/27 07:44

RT

#include<iostream>
#include<cstdio>
using namespace std;
const int N=2005;
int inline read()
{
	int ans=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		ans=ans*10+ch-'0';
		ch=getchar();
	}
	return ans*f;
}
int a[N][N],f[N][N],up[N][N],l[N][N],r[N][N];
int n,k;
int cal(int x1,int y1,int x2,int y2)
{
	return f[x2][y2]+f[x1-1][y1-1]-f[x2][y1-1]-f[x1-1][y2];
}
int main()
{
	k=read();n=read();
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	{
		a[i][j]=read();
		if(a[i][j]>=k&&a[i][j]<=2*k)
		{
			cout<<j<<" "<<i<<" "<<j<<" "<<i;
			return 0;
		}
		if(a[i][j]>2*k) continue;
		r[i][j]=l[i][j]=j;
		up[i][j]=1;
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
	for(int i=1;i<=n;i++)
	for(int j=2;j<=n;j++)
	if(a[i][j]<k&&a[i][j-1]<k)
	l[i][j]=l[i][j-1];
	for(int i=1;i<=n;i++)
	for(int j=n-1;j>=1;j--)
	if(a[i][j]<k&&a[i][j+1]<k)
	r[i][j]=r[i][j+1];
	int x1=0,y1=0,x2=0,y2=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i>1&&a[i][j]<k&&a[i-1][j]<k)
			{
				l[i][j]=max(l[i][j],l[i-1][j]);
				r[i][j]=min(r[i][j],r[i-1][j]);
				up[i][j]=up[i-1][j]+1;
			}
		}
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	{
		if(a[i][j]<k&&cal(i-up[i][j]+1,j-l[i][j]+1,i,j+r[i][j]-1)>=k)
		{
			y1=j-l[i][j]+1;
			x1=i-up[i][j]+1;
			y2=j+r[i][j]-1;
			x2=i;
			break;
		}
	}
	if(x1==0)
	{
		cout<<"NIE";
		return 0;
	}
	while(cal(x1,y1,x2,y2)>2*k)
	{
		if(cal(x1,y1,x2,y2-1)>k)
		y2--;
		else if(cal(x1,y1,x2-1,y2)>k)
		x2--;
		else if(cal(x1+1,y1,x2,y2)>k)
		x1++;
		else y1++;
	}
	cout<<y1<<" "<<x1<<" "<<y2<<" "<<x2;
	return 0;
}
2020/5/27 07:44
加载中...