Created
March 20, 2012 17:30
-
-
Save heat/2138403 to your computer and use it in GitHub Desktop.
store credit problem resolve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys, re, os | |
class Item : | |
def __init__(self) : | |
self.no = 0 | |
self.value = 0.0 | |
def __init__(self, no, value) : | |
self.no = no | |
self.value = value | |
def __str__(self) : | |
return "item %d + %d" %(self.no, self.value,) | |
def no(self) : | |
return self.no | |
i = Item(10,1) | |
def load_itens(f) : | |
qtd_itens = int(f.readline()) | |
itens_price = f.readline().split() | |
l = [] | |
for i in range(0, qtd_itens ) : | |
l.append( | |
Item(i + 1, int(itens_price[i]))) | |
l = sorted(l, key=lambda item: item.value) | |
return l | |
f = open('A-large-practice.in') | |
cases = 0 | |
cases = int(f.readline()) | |
for case in range(1, cases + 1) : | |
credit = int(f.readline()) | |
itens = load_itens(f) | |
x, y = 0, len(itens) - 1 | |
while x != y : | |
t = itens[x].value + itens[y].value | |
if t == credit : | |
print "Case #%d: %d %d"%(case, | |
min(itens[x].no, itens[y].no), | |
max(itens[x].no, itens[y].no)) | |
break; | |
elif t < credit : | |
x += 1 | |
else : | |
y -= 1 | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment