Topcoder上较有意思python编程——农民与酸雨的问题

第一次挑战中等难度——纪念

难度等级: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)

提交成功!但算法仍存在弊端,希望大佬们有想法的在评论区讲出来!运行成功的截图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值