1. 程式人生 > >Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) C

Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) C

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 a and b of positive integers, with length n and m

respectively.

Let c be an n×m matrix, where ci,j=ai⋅bj

.

You need to find a subrectangle of the matrix c such that the sum of its elements is at most x

, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number s such that it is possible to choose integers x1,x2,y1,y2 subject to 1≤x1≤x2≤n, 1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s, and x2∑i=x1y2∑j=y1ci,j≤x.

Input

The first line contains two integers n and m (1≤n,m≤2000

).

The second line contains n integers a1,a2,…,an (1≤ai≤2000

).

The third line contains m integers b1,b2,…,bm (1≤bi≤2000

).

The fourth line contains a single integer x (1≤x≤2⋅109

). Output

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

. 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

給定一個矩陣,求一個子矩陣的sum<=x的面積的最大值;

n,m∈[ 0,2000 ];

思路: 題目中給了 a,b 兩個陣列; 其中 c [ i ] [ j ]=a [ i ] * b [ j ]; 我之前的思路: 明顯可以預處理出 c[ i ] [ j ]; 接著可以預處理出 二維字首和; 如果列舉左上點和右下點,複雜度 O(N^4); T到飛起23333

很顯然; 子矩陣的和就是對應區間的和的乘積; 區間和可以字首和預處理; 我們要求某個長度和某個寬度的子矩陣,那麼我們只需要儲存該長度或寬度對應的區間字首和的min值; 貌似有點繞 那麼O(N^2)就可以了;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

ll cal(ll x) {
	ll ans = 0;
	while (x) {
		ans += (x % 10);
		x /= 10;
	}
	return ans;
}

ll a[4002], b[4002];
ll suma[4002], sumb[4002];
int n, m;
ll x;
ll SA[4003], SC[4003];

int main()
{
	//ios::sync_with_stdio(false);
	rdint(n); rdint(m);
	for (int i = 1; i <= n; i++) {
		rdllt(a[i]); suma[i] = suma[i - 1] + a[i];
	}
	for (int i = 1; i <= m; i++) {
		rdllt(b[i]); sumb[i] = sumb[i - 1] + b[i];
	}
	rdllt(x);
	int ans = 0;
	for (int i = 1; i <= max(n, m); i++)SA[i] = SC[i] = x + 1;
	for (int i = 1; i <= n; i++) {
		for (int j = i; j <= n; j++) {
			SA[j - i + 1] = min(SA[j - i + 1], suma[j] - suma[i - 1]);
		}
	}
	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j++) {
			SC[j - i + 1] = min(SC[j - i + 1], sumb[j] - sumb[i - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (i*j > ans&&SA[i] * SC[j] <= x)ans = i * j;
		}
	}
	cout << ans << endl;
}