扫描型最大问题求解,continue,经典

实际上是一个画线分段求值问题,对于固定输入而言,每一条线确定一个最大值,多条线再确定一个最大值即为最终值

x = [1,1,1,1,2,1,1]
y = [5,2,5,4,5,1,6]
x_num = x.copy()

y_num = y.copy()

last_max = 0
now_max = 0
now_maxi = 0
last_maxi = 0
l_max = int(y_num[0])
for i in range(len(y_num)):
	if l_max<int(y_num[i]):
		l_max = int(y_num[i])
# print(l_max)
for i in range(1,l_max+1):
	for j in range(len(x_num)):
		if int(y_num[j])>=i:
			now_max += int(x_num[j])*int(i)
		else:
			if last_max<now_max:
				last_max = now_max
			now_max = 0
			continue
	if now_max == sum(x)*i:
		last_max = now_max
	now_max = 0
	# print(last_max)
	# maybe no need be modify
	now_maxi = last_max
	if last_maxi<now_maxi:
		last_maxi = now_maxi
print(last_maxi)

效果拔群,效率还比较高,至于为何用Python写,因为最近在用,顺便用了,其实var型定义有时候是真的方便

import matplotlib.pyplot as plt


plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# xlabel and y-value

x = ('01-1','02-1','03-1','04-1','05-2','06-1','07-1')
y = [5,2,5,4,5,1,6]

plt.bar(x,y)
plt.title('Poster hist show')

plt.show()

Bar图自己体会(题目要求不得搬动箱子位序<否则问题将很复杂>),continue、static、now_var、last_var显得格外重要

发表评论

电子邮件地址不会被公开。 必填项已用*标注