@sensitive-cs
2017-10-12T14:18:55.000000Z
字数 2756
阅读 1012
贪心,模拟
Pat数据比较水,py都能过
Pat 1033 To fill or not to fill
贪心,水题
stations = list()minPrice = 1e5maxArrived = 0cmax=0d=0davg=0n=0def find_next_station(currTotalPrice,curr,gasLength,max_len):global stations,cmax,d,davg,n,minPrice,maxArrivedsindex=list()currLen = stations[curr]['distance']currIndex = currcheapPrice =1e5cheapIndex=0flag = Falsefor i in range(curr+1,len(stations)):if(flag==False and stations[i]['distance']-currLen<=max_len ):sindex.append(i)if(stations[i]['price']<stations[curr]['price']):cheapPrice = stations[i]['price']cheapIndex = iflag=Trueif(len(sindex)>0):if(cheapPrice<stations[curr]['price']):pushGasLen = stations[cheapIndex]['distance'] - stations[curr]['distance']-gasLengthcurrTotalPrice+=stations[curr]['price'] * pushGasLen*1.0/(davg*1.0)gasLength+=pushGasLenelse:if(stations[curr]['distance']+max_len>=d):pushGasLen = d - stations[curr]['distance'] -gasLength;currTotalPrice += stations[curr]['price'] * pushGasLen*1.0/(davg*1.0);if(currTotalPrice < minPrice):minPrice=currTotalPricemaxArrived = dreturnelse:pushGasLen = max_len - gasLengthcurrTotalPrice+=stations[curr]['price'] * pushGasLen*1.0/(1.0*davg)gasLength = max_lenif(cheapIndex!=0):curr = cheapIndexelse:curr = sindex[len(sindex)-1];gasLength -= stations[curr]['distance']-stations[currIndex]['distance']find_next_station(currTotalPrice,curr,gasLength,max_len)else:if(stations[curr]['distance']+max_len>=d):lastDistance = d-stations[curr]['distance']currTotalPrice =currTotalPrice+ (lastDistance*1.0/davg)*stations[curr]['price']if(currTotalPrice<minPrice):minPrice = currTotalPricemaxArrived = delse:distance = stations[curr]['distance']+max_lenif(distance>maxArrived):maxArrived = distance;returndef main():global stations,cmax,d,davg,n,minPrice,maxArriveds=input().strip().split()cmax=float(s[0])d=float(s[1])davg=float(s[2])n=int(s[3])MAX_LEN=cmax*davgnoway = Truefor i in range(n):s=input().strip().split()ss={'price':float(s[0]),'distance':float(s[1])}stations.append(ss)if(ss['distance']==0):noway=Falseif(noway):print("The maximum travel distance = %.2lf"%maxArrived)returnstations = sorted(stations,key=lambda x:x['distance'])find_next_station(0,0,0,MAX_LEN)if(maxArrived<d):print("The maximum travel distance = %.2lf"%maxArrived)else:print("%.2lf"%minPrice)returnmain()
Pat1007 Maximum Subsequence Sum 最大子序列和
n=int(input())arr=map(lambda x:int(x),input().split(' '))maxSum,tmpSum = -1,0ms,mt = -1,-1ts,tt = -1,-1for x in arr:if ts==-1:start=xelse:start=tsif tmpSum+x<0:tmpSum=0ts,tt=-1,-1elif tmpSum+x>maxSum:tmpSum+=xmaxSum=tmpSumms=ts=startmt=tt=xelse:tmpSum+=xts=starttt=xif ms==-1:first_f=Truef,l=0,0for x in arr:if first_f:f=xfirst_f=Falsel=xprint("0 %d %d"%(f,l))else:print("%d %d %d"%(maxSum,ms,mt))
Pat 1008 Elevator 模拟,水题
from functools import *first=Truen=0f,tt=0,0for x in input().split():if first:n=int(x)first=Falseelse:y=int(x)if f<y:tt+=(y-f)*6f=yelse:tt+=(f-y)*4f=yprint(tt+n*5)