I recently learned about Google Code Jam, a programming competition project. After logging on to the project, I can see the previous questions. I think they are all very interesting. So I 'd like to try it out.
The first question I saw was Store Credit, which probably meant giving you a certain amount of money, then giving you an item price list, and choosing two items, just to spend all the money. Specific content can refer to the http://code.google.com/codejam/contest/351101/dashboard#s=p0
The greedy algorithm is not very useful because it requires that all the money be spent. Fortunately, you only need to select two items. The direct method is to traverse the search. Determine an item and find the item whose price meets the requirement. In traversal search, you can also reduce the number of searches.
For example, you can traverse items sequentially. After selecting an item, you only need to traverse and search for the remaining items.
For example, you can sort the items by price first to exclude the items whose prices are equal to or greater than the total amount of money from the search range. Note that the order of items needs to be recorded during sorting, because it requires the output of the original number of items.
After confirming this, I started to write the program. After learning Python for so long, almost no program has been officially written, so I decided to use Python for implementation.
First, the sorting function uses the quick sorting algorithm,
Refer to Region.
The Code is as follows,
#!/usr/bin/python
#encoding:UTF-8
#Filename:QuickSort.py
def partition(A,B,p,r):
x = A[r]
i = p-1
for j in range(p,r):
if A[j]<=x:
i = i + 1
A[i],A[j] = A[j],A[i]
B[i],B[j] = B[j],B[i]
A[i+1],A[r] = A[r],A[i+1]
B[i+1],B[r] = B[r],B[i+1]
return i+1
def quickSort(A,B,p,r):
if p<r:
q = partition(A,B,p,r)
quickSort(A,B,p,q-1)
quickSort(A,B,q+1,r)
After sorting, the search uses binary search,
The Code is as follows,
#!/usr/bin/python
#encoding:UTF-8
#Filename:BinarySearch.py
def binarySearch(A,x,lo,hi):
res = -1
while(lo<=hi):
mid = (lo+hi) / 2
if x==A[mid]:
res = mid
break
elif x<A[mid]:
hi = mid - 1
else:
lo = mid + 1
return res
Then I wrote a solver program. One is direct search, and the other is sorting and searching,
The Code is as follows,
#!/usr/bin/python
#encoding:UTF-8
#Filename:CreditSolver.py
import QuickSort
import BinarySearch
def creditSolverDirect(m,l,p):
an = [0,0]
for i in range(l-1):
for j in range(i+1,l):
if p[i]+p[j]==m:
an = [i+1,j+1]
return an
return an
def creditSolverSort(m,l,p):
an = [0,0]
pa = p;
pb = range(0,l)
QuickSort.quickSort(pa,pb,0,l-1)
i = 0;
for price in pa:
i = i+1
if price < m:
res = BinarySearch.binarySearch(pa,m-price,i,l-1)
if res!=-1:
an = [pb[i-1],pb[res]]
break
if an[0]>an[1]:
an[0],an[1] = an[1],an[0]
an = [an[0]+1,an[1]+1]
return an
Finally, the main program reads the input, solves the problem, and writes the output to the file,
The Code is as follows,
#!/usr/bin/python
#Filename: StoreCredit.py
import CreditSolver
# read file
linenum = 0;
testCaseNum = 0;
caseLines = 3;
fin = open("input.txt")
fout = open("output.txt","w")
# to process first line, test case number
line = fin.readline()
if not line:
print "failed to open input.txt"
linenum = linenum + 1
testCaseNum = int(line)
#print "test cases number:%d" %(testCaseNum)
for i in range(testCaseNum):
money = int(fin.readline())
items = int(fin.readline())
priceStr = fin.readline().split(" ");
price = [int(stri) for stri in priceStr]
# print "--- to solve case %d ---" %(i)
# print "money:%d" %(money)
# print "items:%d" %(items)
# print "price:"
# print price
L = [0,0]
if items > 128:
L = CreditSolver.creditSolverSort(money,items,price)
else:
L = CreditSolver.creditSolverDirect(money,items,price)
answer = ""
if L[1]!=0:
answer = "Case #" + str(i) + ": " + str(L[0]) + " " + str(L[1]) + "\n"
else:
answer = "Case #" + str(i) + ": x\n"
fout.write(answer)
#print "done!"
fin.close()
fout.close()
The output is correct for the tests given in the questions. No larger examples have been tested. I wonder whether exceptions or poor performance will occur. If anyone finds the problem, I hope you can tell me how to improve the problem solving process and program implementation.