codeforces 1060C. Maximum Subrectangle(思维)

C. Maximum Subrectangle

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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

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, andx2∑i=x1y2∑j=y1ci,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

Copy

3 3
1 2 3
1 2 3
9

output

Copy

4

input

Copy

5 1
5 4 2 4 5
2
5

output

Copy

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):

 

 

一、原题地址

点我传送

 

二、题目大意

给出两个序列,他们能组成一个矩阵,然后求子矩阵的和小于k的,最大的子矩阵的面积。

 

三、大致思路

可以处理出数列a和数列b中连续序列的最小和,用suma[ ]和sumb[ ]来记录。

比如suma[ x ] = y表示a序列中连续的x个数字能组成的最小的和为y。

暴力枚举一下两个序列的段数,更新答案。

 

四、代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }



int n, m;
LL a[2005], b[2005], K;
LL suma[2005], sumb[2005];
void read()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)scanf("%lld", &a[i]);
	for (int i = 1; i <= m; i++)scanf("%lld", &b[i]);
	scanf("%lld", &K);
}
int main()
{
	read();
	for (int len = 1; len <= n; len++)
	{
		queueq;
		LL now = 0, minn = 0;
		for (int i = 1; i <= len; i++)
		{
			q.push(a[i]);
			now += a[i]; minn += a[i];
		}
		for (int j = len + 1; j <= n; j++)
		{
			now -= q.front();
			q.pop();
			q.push(a[j]);
			now += a[j];
			minn = min(now, minn);
		}
		suma[len] = minn;
	}
	//更新a的连续段最小和
	for (int len = 1; len <= m; len++)
	{
		queueq;
		LL now = 0, minn = 0;
		for (int i = 1; i <= len; i++)
		{
			q.push(b[i]);
			now += b[i]; minn += b[i];
		}
		for (int j = len + 1; j <= m; j++)
		{
			now -= q.front();
			q.pop();
			q.push(b[j]);
			now += b[j];
			minn = min(now, minn);
		}
		sumb[len] = minn;
	}
	//更新b的连续段最小和

	int ans = 0;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (suma[i] * sumb[j] <= K)
			{
				ans = max(ans, i*j);
			}
		}
	}
	printf("%d\n", ans);
	getchar();
	getchar();
}

 

你可能感兴趣的:(codeforces 1060C. Maximum Subrectangle(思维))