【CodeForces - 1060C】【Maximum Subrectangle 】

题目:

You are given two arrays aa and bb of positive integers, with length nn and mmrespectively.

Let cc be an n×mn×m matrix, where ci,j=ai⋅bjci,j=ai⋅bj.

You need to find a subrectangle of the matrix cc such that the sum of its elements is at most xx, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number ss such that it is possible to choose integers x1,x2,y1,y2x1,x2,y1,y2 subject to 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s(x2−x1+1)×(y2−y1+1)=s, and

∑i=x1x2∑j=y1y2ci,j≤x.∑i=x1x2∑j=y1y2ci,j≤x.

Input

The first line contains two integers nn and mm (1≤n,m≤20001≤n,m≤2000).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000).

The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤20001≤bi≤2000).

The fourth line contains a single integer xx (1≤x≤2⋅1091≤x≤2⋅109).

Output

If it is possible to choose four integers x1,x2,y1,y2x1,x2,y1,y2 such that 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, and ∑x2i=x1∑y2j=y1ci,j≤x∑i=x1x2∑j=y1y2ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1)(x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 00.

Examples

Input

3 3
1 2 3
1 2 3
9

Output

4

Input

5 1
5 4 2 4 5
2
5

Output

1

Note

Matrix from the first sample and the chosen subrectangle (of blue color):

Matrix from the second sample and the chosen subrectangle (of blue color):

解题报告:给我们两个矩阵(行,列),二者做矩阵乘法,然后得到新的矩阵C,其中我们找到其子矩阵,和小于等于X 且范围尽可能的大。这里用到了一个矩阵求和的小装换,子阵的和等于乘出来他的行列和的乘积(很溜的规律)。这样咱们就可以将而为矩阵的求和变为最初两个一维矩阵的求和求积。然后矩阵的话,是要求范围尽可能的大,所以在固定一边的长度情况下,这边的值越小,对方的值就要越大,那么长度就会越大。然后面积就变大。

ac代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn = 2050;
ll a[maxn],b[maxn];
ll suma[maxn],sumb[maxn];

int main()
{
	ll x,n,m;
	while(scanf("%lld%lld",&n,&m)!=EOF)
	{
		a[0]=b[0]=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&a[i]);
			a[i]=a[i]+a[i-1];	
		}	
		for(int i=1;i<=m;i++)
		{
			scanf("%lld",&b[i]);
			b[i]=b[i]+b[i-1];
		}
		scanf("%lld",&x);
		memset(suma,inf,sizeof(suma));
		memset(sumb,inf,sizeof(sumb));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				if(i+j-1>n) break;
				suma[i]=min(suma[i],a[i+j-1]-a[j-1]);
			}
		for(int i=1;i<=m;i++)
			for(int j=1;j<=m;j++)
			{
				if(i+j-1>m) break;
				sumb[i]=min(sumb[i],b[i+j-1]-b[j-1]);
			}
		ll ans=0;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				if((ll)suma[i]*sumb[j]<=x)
					ans=max(ans,(ll)i*j*1);
			}
		printf("%lld\n",ans);
	}	
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值