当前位置:网站首页>codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
2022-08-03 19:51:00 【白速龙王的回眸】
分析
两个sigma的求和最终等价于
a的子数组和乘b的子数组和 小于等于 x
对于固定长度的子数组而言,我们都能找出一个和最小的
对于a和b我们都找出指定长度L和最小的子数组和(贪心)
然后一一配对,看看是否面积更大且满足和乘积小于等于x即可
ac code
import sys
import math
from itertools import accumulate
input = sys.stdin.readline
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = int(input())
# (aj1 + ... + aj2) * (bi1 + ... + bi2) <= x
ans = 0
# for a, b; fix length L, the min Sum => greedy
f1 = [math.inf] * (n + 1)
f2 = [math.inf] * (m + 1)
preSum1 = list(accumulate(a, initial = 0))
preSum2 = list(accumulate(b, initial = 0))
# for a
for l in range(1, n + 1):
for st in range(n - l + 1):
end = st + l - 1
f1[l] = min(f1[l], preSum1[end + 1] - preSum1[st])
# for b
for l in range(1, m + 1):
for st in range(m - l + 1):
end = st + l - 1
f2[l] = min(f2[l], preSum2[end + 1] - preSum2[st])
# combine
for l1, v1 in enumerate(f1):
for l2, v2 in enumerate(f2):
if l1 * l2 > ans and v1 * v2 <= x:
ans = l1 * l2
print(ans)
总结
转化为两个子数组的乘积
长度是两个未知数,枚举出来
然后选出当长度固定时最小的和即可
这个用前缀和稍微操作即可
边栏推荐
猜你喜欢
随机推荐
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
MySQL 主从,6 分钟带你掌握!
Unity获取canvas 下ui 在屏幕中的实际坐标
边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
ctfshow php features
Kettle 读取 Excel 数据输出到 Oracle 详解
告诉你0基础怎么学好游戏建模?
net-snmp私有mib动态加载到snmpd
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
机器学习中专业术语的个人理解与总结(纯小白)
php根据两点经纬度计算距离
Introduction to Cosine Distance
Postgresql source code (64) Query execution - data structure and execution process before submodule Executor (2) execution
The effective square of the test (one question of the day 7/29)
安装radondb mysql遇到问题
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
In-depth understanding of JVM-memory structure
Detailed explanation of JWT
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
FreeRTOS中级篇