google code jam exercise——MilkShakes(3)

來源:互聯網
上載者:User

還是MilkShakes的問題。今天看了一下Contest Analysis,按照提示總算得到了較好的結果。

在Contest Analysis中,給出了演算法的描述,如下:

1. 初始化flavor choice全為0,檢查customer的滿意情況;

2. 對於不滿意的customer,如果他只喜歡unmalted的,但是所有的已經malted,那麼該customer不可能被滿足,給出IMPOSSIBLE;

3. 對於不滿意的customer,如果他只有一種喜歡的malted,那麼將對應的malted,回到步驟2;

4. 如果沒有不滿意的customer,那麼就是得到瞭解。

在用Python實現的時候,做了一些改進,步驟如下:

1. 初始化flavor choice
遍曆所有customer,如果他只喜歡一種flavor,那麼要滿足他就必須把對應的flavor設為對應的狀態。如果在他之前已經被設定為不同的狀態,那麼他就不可能被滿足,給出IMPOSSIBLE,否則,將沒有被設定的flavor設定為0,繼續下面步驟;

2. 檢查所有customer是否滿意,如果滿意,給出flavor的序列,否則進入迴圈2-3;

3. 更新flavor choice

遍曆不滿意的customer,統計他喜歡的malted的個數,如果為0,即該customer只喜歡unmalted,那麼他不可能被滿足,給出IMPOSSIBLE,如果為1,那麼將對應的flavor設為1,即完成flavor choice的更新

代碼如下:

#!/usr/bin/python
#encoding:UTF-8
#Filename:MilkShakes_Good.py

import sys

def preChoice(customers,flavorNum):
flag = 1
flavorSure = []
flavorChoice = []
for i in range(flavorNum):
flavorChoice.append(-1)
flavorSure.append(0)

for i,customer in enumerate(customers):
if customer[0]==1:
if flavorChoice[customer[1]-1]==-1:
flavorChoice[customer[1]-1] = customer[2]
elif flavorChoice[customer[1]-1]!=customer[2]:
flag = 0
break
flavorSure[customer[1]-1] = 1

return [flag,flavorSure,flavorChoice]

def checkSatisfy(customerNum,customers,flavorChoice):
# for k,sat in enumerate(customerSatisfy):
# if sat==0:
customerSatisfy = []
for i in range(customerNum):
customerSatisfy.append(0)
for k in range(customerNum):
customer = customers[k]
for i in range(customer[0]):
j = 2 * i + 1
if flavorChoice[customer[j]-1]==customer[j+1]:
customerSatisfy[k] = 1
break
return customerSatisfy

def checkAllSatisfy(customerSatisfy):
flag = 1
for v in customerSatisfy:
if v==0:
flag = 0
break
return flag


def updateChoice(customers,flavorSure,flavorChoice,customerSatisfy):
flag = 1
twoflag = 1
for k,v in enumerate(customerSatisfy):
if v==0:
customer = customers[k]
index = []
for i in range(customer[0]):
j = 2 * i + 1
if customer[j+1]==1:
index.append(j)
if len(index)==0:
flag = 0
break
elif len(index)==1:
flavorSure[customer[index[0]]-1] = 1
flavorChoice[customer[index[0]]-1] = 1
twoflag = 0
break

if twoflag==1:
print "two more malted in unsatisfied customer\n"
return [flag,flavorSure,flavorChoice]



inname = "input.txt"
outname = "output.txt"
if len(sys.argv)>1:
inname = sys.argv[1]
outname = inname.rstrip(".in")
outname = outname + ".out"
fin = open(inname,"r")
fout = open(outname,"w")

line = fin.readline()

testCaseNum = int(line)

caseNum = 0

for caseNum in range(testCaseNum):
line = fin.readline()
flavorNum = int(line)
line = fin.readline()
customerNum = int(line)
customers = []
# for line in lines:
# print "Case #%d:" %(caseNum+1)
# print flavorNum
# print customerNum
for i in range(customerNum):
line = fin.readline()
line = line.rstrip("\n")
customer = [int(val) for val in line.split()]
# print customer
customers.append(customer)

# print "\n"

answer = "Case #%d:" %(caseNum+1)

tpre = preChoice(customers,flavorNum)

# print "pre choice:"
# print tpre

flag = tpre[0]
if flag == 0:
answer = answer + " IMPOSSIBLE"
else:
flavorSure = tpre[1]
flavorChoice = tpre[2]
for i in range(flavorNum):
if flavorChoice[i]==-1:
flavorChoice[i] = 0
customerSatisfy = checkSatisfy(customerNum,customers,flavorChoice)
# print customerSatisfy
    allSatisfy = checkAllSatisfy(customerSatisfy)
# print allSatisfy

while(allSatisfy==0):
tpre = updateChoice(customers,flavorSure,flavorChoice,customerSatisfy)
# print "update:"
# print tpre
      flag = tpre[0]
if flag==0:
break
else:
flavorSure = tpre[1]
flavorChoice = tpre[2]
customerSatisfy = checkSatisfy(customerNum,customers,flavorChoice)
allSatisfy = checkAllSatisfy(customerSatisfy)

if flag==0:
answer = answer + " IMPOSSIBLE"
else:
for val in flavorChoice:
answer = answer + " " + str(val)

answer = answer + "\n"

fout.write(answer)

fin.close()
fout.close()

這次,small/large case都測試通過。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.