[关闭]
@sensitive-cs 2017-10-12T14:18:55.000000Z 字数 2756 阅读 759

PAT划水记

贪心,模拟

Pat数据比较水,py都能过


Pat 1033 To fill or not to fill
贪心,水题

  1. stations = list()
  2. minPrice = 1e5
  3. maxArrived = 0
  4. cmax=0
  5. d=0
  6. davg=0
  7. n=0
  8. def find_next_station(currTotalPrice,curr,gasLength,max_len):
  9. global stations,cmax,d,davg,n,minPrice,maxArrived
  10. sindex=list()
  11. currLen = stations[curr]['distance']
  12. currIndex = curr
  13. cheapPrice =1e5
  14. cheapIndex=0
  15. flag = False
  16. for i in range(curr+1,len(stations)):
  17. if(flag==False and stations[i]['distance']-currLen<=max_len ):
  18. sindex.append(i)
  19. if(stations[i]['price']<stations[curr]['price']):
  20. cheapPrice = stations[i]['price']
  21. cheapIndex = i
  22. flag=True
  23. if(len(sindex)>0):
  24. if(cheapPrice<stations[curr]['price']):
  25. pushGasLen = stations[cheapIndex]['distance'] - stations[curr]['distance']-gasLength
  26. currTotalPrice+=stations[curr]['price'] * pushGasLen*1.0/(davg*1.0)
  27. gasLength+=pushGasLen
  28. else:
  29. if(stations[curr]['distance']+max_len>=d):
  30. pushGasLen = d - stations[curr]['distance'] -gasLength;
  31. currTotalPrice += stations[curr]['price'] * pushGasLen*1.0/(davg*1.0);
  32. if(currTotalPrice < minPrice):
  33. minPrice=currTotalPrice
  34. maxArrived = d
  35. return
  36. else:
  37. pushGasLen = max_len - gasLength
  38. currTotalPrice+=stations[curr]['price'] * pushGasLen*1.0/(1.0*davg)
  39. gasLength = max_len
  40. if(cheapIndex!=0):
  41. curr = cheapIndex
  42. else:
  43. curr = sindex[len(sindex)-1];
  44. gasLength -= stations[curr]['distance']-stations[currIndex]['distance']
  45. find_next_station(currTotalPrice,curr,gasLength,max_len)
  46. else:
  47. if(stations[curr]['distance']+max_len>=d):
  48. lastDistance = d-stations[curr]['distance']
  49. currTotalPrice =currTotalPrice+ (lastDistance*1.0/davg)*stations[curr]['price']
  50. if(currTotalPrice<minPrice):
  51. minPrice = currTotalPrice
  52. maxArrived = d
  53. else:
  54. distance = stations[curr]['distance']+max_len
  55. if(distance>maxArrived):
  56. maxArrived = distance;
  57. return
  58. def main():
  59. global stations,cmax,d,davg,n,minPrice,maxArrived
  60. s=input().strip().split()
  61. cmax=float(s[0])
  62. d=float(s[1])
  63. davg=float(s[2])
  64. n=int(s[3])
  65. MAX_LEN=cmax*davg
  66. noway = True
  67. for i in range(n):
  68. s=input().strip().split()
  69. ss={'price':float(s[0]),'distance':float(s[1])}
  70. stations.append(ss)
  71. if(ss['distance']==0):
  72. noway=False
  73. if(noway):
  74. print("The maximum travel distance = %.2lf"%maxArrived)
  75. return
  76. stations = sorted(stations,key=lambda x:x['distance'])
  77. find_next_station(0,0,0,MAX_LEN)
  78. if(maxArrived<d):
  79. print("The maximum travel distance = %.2lf"%maxArrived)
  80. else:
  81. print("%.2lf"%minPrice)
  82. return
  83. main()

Pat1007 Maximum Subsequence Sum 最大子序列和

  1. n=int(input())
  2. arr=map(lambda x:int(x),input().split(' '))
  3. maxSum,tmpSum = -1,0
  4. ms,mt = -1,-1
  5. ts,tt = -1,-1
  6. for x in arr:
  7. if ts==-1:
  8. start=x
  9. else:
  10. start=ts
  11. if tmpSum+x<0:
  12. tmpSum=0
  13. ts,tt=-1,-1
  14. elif tmpSum+x>maxSum:
  15. tmpSum+=x
  16. maxSum=tmpSum
  17. ms=ts=start
  18. mt=tt=x
  19. else:
  20. tmpSum+=x
  21. ts=start
  22. tt=x
  23. if ms==-1:
  24. first_f=True
  25. f,l=0,0
  26. for x in arr:
  27. if first_f:
  28. f=x
  29. first_f=False
  30. l=x
  31. print("0 %d %d"%(f,l))
  32. else:
  33. print("%d %d %d"%(maxSum,ms,mt))

Pat 1008 Elevator 模拟,水题

  1. from functools import *
  2. first=True
  3. n=0
  4. f,tt=0,0
  5. for x in input().split():
  6. if first:
  7. n=int(x)
  8. first=False
  9. else:
  10. y=int(x)
  11. if f<y:
  12. tt+=(y-f)*6
  13. f=y
  14. else:
  15. tt+=(f-y)*4
  16. f=y
  17. print(tt+n*5)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注