google code jam exercise——MilkShakes(3)
还是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都测试通过。