« Previous entry | Next entry » Browse > Snippets

Skip to comments (6) spoj SUMITR
Posted by guest on Jul 17 2008 @ 08:47  :: 1307 unique visits

First solution:
CODE: PYTHON
def f(i,j):
    m=t[i][j]
    return i==l-1 and[m,]or [x+m for x in f(i+1,j)+f(i+1,j+1)]
c=input()
r=[]
for i in range(c):
    l=input()
    t=[]
    for i in range(l):
        t+=[(map(int,raw_input().split()))]
    r+=[max(f(0,0))]
for i in r:
    print i


Optimized solution with profiling code:
CODE: PYTHON
t = [] #
l = 0 #

def f(i,j,s):
    m=t[i][j]
    if i==l-1:
        return m+s
    else:
        return max(f(i+1,j,m+s),f(i+1,j+1,m+s))

def main():
    global strNum, inpData, l, t #
    for i in range(1): #
        strNum = 0 #
        c=int(inpData[strNum])#input()
        strNum += 1 #
        r=[]
        for i in range(c):
            l=int(inpData[strNum])#input()
            strNum += 1 #
            t=[]
            for i in range(l):
                t+=[[int(x) for x in inpData[strNum].split()]]#raw_input().split()))]
                strNum += 1 #
            r+=[f(0,0,0)]
        for i in r:
            print i

if __name__ == '__main__':
    import profile, pstats

    lineCount = 20
    inpData = ["1",lineCount]#["2","3","1","2 1","1 2 3","4","1","1 2","4 1 2","2 3 1 1"]#
    for i in range(1,lineCount + 1):
        inpData.append(reduce(lambda x, y: x + " " + str(y), range(i), ""))
    strNum = 0
    profile.run('main()', 'result.prof')
    pstats.Stats('result.prof').strip_dirs().sort_stats(-1).print_stats()

6 comments posted so far
Add your own »

1. On Jan 23 2009 @ 03:57 guest wrote:

who has lived in close communication with nature allem and took itto an editor.<a href="http://www.cheapkamas.com" title="dofus kamas">dofus kamas</a>It was a living pas<a href="http://www.cheapkamas.com" title="kamas dofus">kamas dofus</a>toral, full of the ge<a href="http://www.cheapkamas.com" title="acheter dofus">acheter dofus</a>nuine breath of the field<a href="http://www.cheapkamas.com" title="buy kamas">buy kamas</a>s, the song of <a href="http://www.cheapkamas.com" title="acheter kamas">acheter kamas</a>birds, and the pleasant chatter of trickling streams.When the poet cal

2. On Jan 23 2009 @ 03:58 guest wrote:

who has lived in close communicati on with nature all hisdofus kamas life, wrote a poem and took it to an editor.It was a living pastoral, full of the gkamas dofusenuine breath of the fieacheter dofuslds, the song of bibuy kamasrds, and the pleasant cacheter kamashatter of trickling streams.When the poet calledagain to see about it,

3. On Jan 23 2009 @ 03:59 guest wrote:

nddofus kamas  wh kamas dofuso hacheter kamasadbuy kamas nevacheter dofuser dofus kamaskamas dofusacheter kamasdofus kamaslookdofus kamased upon bucolic skamas dofuscenes except with sachat kamasensations of disgust dofus kamasfrom thedofus kamaskamas dofusbuy kamaswindows of express trains.

4. On Jul 14 2009 @ 04:26 guest wrote:

buy wow gold
my wow power leveling
buy wow gold
good wow power leveling
BUY wow gold
my wow power leveling
CHEAP rs gold
cheap wow power leveling
CHEAPEST lotro gold
MY aion gold
buy wow gold
cheap wow gold
CHEAPEST wow gold

6. On Jan 05 2010 @ 14:56 uggbaileybutton wrote:

bailey button uggs

-ugg boots cheap

ugg boots uk

ugg classic

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: