• 2019银川icpc K 思维,悬线法dp


    题意:

    给出两个 n × m n\times m n×m的矩阵 A , B A,B A,B,矩阵的元素都两两互异,且属于区间 [ 1 , n m ] [1,nm] [1,nm],求出他们最大的公共子矩阵的元素个数有多少个

    Solution:

    最大子矩阵问题我们考虑悬线法,重点在于如何转换至一个矩阵上解决问题。有一个技巧,我们记录 A A A矩阵中元素 x x x的位置为 p o s [ x ] pos[x] pos[x],以行优先或列优先都可以,这里以行优先为例,有 p o s [ a [ i ] [ j ] ] = ( i − 1 ) × n + j pos[a[i][j]]=(i-1)\times n+j pos[a[i][j]]=(i1)×n+j,这样做有什么好处?

    如果我们将 B B B矩阵的元素 x x x替换成 p o s [ x ] pos[x] pos[x],那么在 B B B内出现的 A A A已有的矩阵必然是满足左右差 1 1 1,上下差 m m m的矩阵,这个问题悬线法就可以轻松解决了

    #ifndef stdjudge
    #include
    #endif
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    
    using ll=long long;
    const int N=1e3+5,inf=0x3fffffff;
    const long long INF=0x3fffffffffffffff,mod=998244353;
    
    int a[N][N],b[N][N],pos[N*N];
    int n,m,l[N][N],r[N][N],up[N][N];
    
    int main()
    {
    	#ifdef stdjudge
    		freopen("in.txt","r",stdin);
    	#endif
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
    	for(int i=1,tt=1;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			scanf("%d",&b[i][j]);
    			pos[b[i][j]]=++tt;
    		}
    	}
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			a[i][j]=pos[a[i][j]];
    			l[i][j]=r[i][j]=j;
    			up[i][j]=1;
    		}
    	}
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=2;j<=m;j++)
    			if(a[i][j]==a[i][j-1]+1) l[i][j]=l[i][j-1];
    		for(int j=m-1;j>=1;j--)
    			if(a[i][j]==a[i][j+1]-1) r[i][j]=r[i][j+1];
    	}
    	for(int i=2;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			if(a[i-1][j]+m==a[i][j])
    			{
    				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;
    			}
    		}
    	}
    	ll ans=-INF;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++) ans=max(ans,1ll*(r[i][j]-l[i][j]+1)*up[i][j]);
    	cout<<ans;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
  • 相关阅读:
    Eureka
    锁-分布式锁
    【应用统计学】随机变量的概率分布,数学期望和方差及协方差
    教你几分钟学会DNAC查看设备健康度
    算法 学习杂谈
    使用dockerfile制作定时执行任务镜像
    基于javaweb的医院管理系统(java+springboot+mybatis+vue+mysql)
    美国隐私安全人工智能大模型公司【Fantix】160万美元融资
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    Python 反爬虫与反反爬虫
  • 原文地址:https://blog.csdn.net/stdforces/article/details/127931340