第一次挑战中等难度——纪念
难度等级:medium
问题称述
Problem Statement
You are a farmer living in a 2-dimensional world. Your crops look like an infinite line parallel to the x-axis, with the y-coordinate equal to 0. According to the weather forecast, the next rain will be acid, and therefore very harmful to your plants. The rain consists of an infinite number of drops that fall down vertically (parallel to the y-axis). For every x, where x is an arbitrary number (not necessarily an integer), a drop will fall toward the point (x, 0).
To protect your crops, you have built some shields above the ground. Each shield is a line segment parallel to the x-axis, with negligible thickness. If a drop of rain falls on a shield, it will flow to the closest end of the shield and continue falling straight down vertically from that point. If a drop falls exactly in the middle of a shield, the drop will divide into two equal parts that flow to opposite ends of the shield and continue falling from there. If two or more shields have common endpoints they will act as one combined shield. See examples for clarification.
The locations of your existing shields will be given in the three int[]s b, e and y. The endpoints of the i-th shield are located at (b[i], y[i]) and (e[i], y[i]). Define B as the minimum value in b, and E as the maximum value in e. Your task is to build enough additional shields to protect all the crops between (B, 0) and (E, 0), exclusive (that is, all crops whose x-coordinates lie in the open interval (B, E) ). Each shield must be a line segment parallel to the x-axis with non-zero length and a positive y-coordinate. Each shield you build must have integer endpoints and a positive length. Build these new shields in such a way that the sum of their lengths is minimized. Return the sum of the new shields’ lengths.
问题限定
Constraints
- b will contain between 1 and 25 elements, inclusive.
- b, e and y will contain the same number of elements.
- Each element of b will be between 0 and 10, inclusive.
- Each element of e will be between 0 and 10, inclusive.
- For all i, the i-th element of b will be less than the i-th element of e.
- Each element of y will be between 1 and 100000, inclusive.
- No two shields will overlap, but they can have common endpoints.
python代码块
class AcidRain:
def saveHarvest(self, b, e, y):
con1=len(b)
num1=0
num2=0
num3=0
num4=0
if(1<con1<25):
for i in range(con1):
if(0<=b[i]<=10):
num1+=1
else:
print("the value of b is out range! please try again!")
for i in range(con1):
if(0<=e[i]<=10):
num2+=1
else:
print("the value of e is out range! please try again!")
for i in range(con1):
if(0<=y[i]<=1000):
num3+=1
else:
print("the value of y is out range! please try again!")
for i in range(con1):
if(b[i]<=e[i]):
num4+=1
else:
print("the value of b must less than the value of e!please try again!")
if(num1==con1 and num2==con1 and num3==con1 and num4==con1):
for i in range(con1):
count=y.count(y[con1-1])
if(count==con1 and b[con1-1]==e[0]):
return 0
else:
B=min(b)
E=max(e)
Y=max(y)
oppsite=y.index(Y)
if(B==b[oppsite] and E==e[oppsite]):
return 0
elif((B!=b[oppsite] and E==e[oppsite])or(B==b[oppsite] and E!=e[oppsite])):
return 1
else:
return 2
rain=AcidRain()
c=rain.saveHarvest([1],[2],[1])
print(c)