[Assorted-commits] SF.net SVN: assorted:[1619] problems/mit-acm
Brought to you by:
yangzhang
From: <yan...@us...> - 2010-04-05 14:23:16
|
Revision: 1619 http://assorted.svn.sourceforge.net/assorted/?rev=1619&view=rev Author: yangzhang Date: 2010-04-05 14:23:10 +0000 (Mon, 05 Apr 2010) Log Message: ----------- added mit acm 2006 Added Paths: ----------- problems/mit-acm/2006/ problems/mit-acm/2006/3/ problems/mit-acm/2006/3/prog3.java problems/mit-acm/2006/3/prog3.py problems/mit-acm/2006/3/rot.py problems/mit-acm/2006/3/test problems/mit-acm/2006/4/ problems/mit-acm/2006/4/prob4.py problems/mit-acm/2006/4/test Added: problems/mit-acm/2006/3/prog3.java =================================================================== --- problems/mit-acm/2006/3/prog3.java (rev 0) +++ problems/mit-acm/2006/3/prog3.java 2010-04-05 14:23:10 UTC (rev 1619) @@ -0,0 +1,132 @@ +static enum Orient { left, upleft, upright, right, downright, downleft, inleft, inupleft, inupright, inright, indownright, indownleft } +static enum Angle { openRight, openLeft } +angles[left] = Angle.openRight +angles[upleft] = Angle.openLeft +angles[upright] = Angle.openRight +angles[right] = Angle.openLeft +angles[downright] = Angle.openRight +angles[downleft] = Angle.openLeft +angles[inleft] = Angle.openLeft +angles[inupleft] = Angle.openRight +angles[inupright] = Angle.openLeft +angles[inright] = Angle.openRight +angles[indownright] = Angle.openLeft +angles[indownleft] = Angle.openRight + angles.put( Orient.left, Angle.openRight ); + angles.put( Orient.upleft, Angle.openLeft ); + angles.put( Orient.upright, Angle.openRight ); + angles.put( Orient.right, Angle.openLeft ); + angles.put( Orient.downright, Angle.openRight ); + angles.put( Orient.downleft, Angle.openLeft ); + angles.put( Orient.inleft, Angle.openLeft ); + angles.put( Orient.inupleft, Angle.openRight ); + angles.put( Orient.inupright, Angle.openLeft ); + angles.put( Orient.inright, Angle.openRight ); + angles.put( Orient.indownright, Angle.openLeft ); + angles.put( Orient.indownleft, Angle.openRight ); + +for line in lines[1:]: + hexs = set() + pos = (0,0) + orient = None + for c in line.strip(): + if orient is None: + if c == '1': + orient = left + elif c == '2': + orient = inleft + else: + assert False + row,col = pos + if angle[orient] == open_left: +hex(pos.row,pos.col); +hex(pos.row-1,pos.col+1); +hex(pos.row+1,pos.col+1); +hex(pos.row,pos.col); +hex(pos.row-1,pos.col-1); +hex(pos.row+1,pos.col-1);; + else: + assert False + hexs.update(adjs) + if c == '1': + if (orient == Orient.left) { + orient = Orient.upleft; + pos = hex(pos.row-1,pos.col-1); + } else if (orient == Orient.upleft) { + orient = Orient.upright; + pos = hex(pos.row,pos.col+2); + } else if (orient == Orient.upright) { + orient = Orient.right; + pos = hex(pos.row+1,pos.col-1); + } else if (orient == Orient.right) { + orient = Orient.downright; + pos = hex(pos.row+1,pos.col+1); + } else if (orient == Orient.downright) { + orient = Orient.downleft; + pos = hex(pos.row,pos.col-2); + } else if (orient == Orient.downleft) { + orient = Orient.left; + pos = hex(pos.row-1,pos.col+1); + } else if (orient == Orient.inleft) { + orient = Orient.left; + pos = hex(pos.row-1,pos.col+1); + } else if (orient == Orient.inupleft) { + orient = Orient.upleft; + pos = hex(pos.row-1,pos.col-1); + } else if (orient == Orient.inupright) { + orient = Orient.upright; + pos = hex(pos.row,pos.col+2); + } else if (orient == Orient.inright) { + orient = Orient.right; + pos = hex(pos.row+1,pos.col-1); + } else if (orient == Orient.indownright) { + orient = Orient.downright; + pos = hex(pos.row+1,pos.col+1); + } else if (orient == Orient.indownleft) { + orient = Orient.downleft; + pos = hex(pos.row-1,pos.col-1); + else: + assert False + elif c == '2': + if orient == left: + orient = Orient.inleft; + pos = hex(pos.row-1,pos.col-1); + } else if (orient == Orient.upleft) { + orient = Orient.inupleft; + pos = hex(pos.row,pos.col+2); + } else if (orient == Orient.upright) { + orient = Orient.inupright; + pos = hex(pos.row+1,pos.col-1); + } else if (orient == Orient.right) { + orient = Orient.inright; + pos = hex(pos.row+1,pos.col+1); + } else if (orient == Orient.downright) { + orient = Orient.indownright; + pos = hex(pos.row,pos.col-2); + } else if (orient == Orient.downleft) { + orient = Orient.indownleft; + pos = hex(pos.row-1,pos.col+1); + } else if (orient == Orient.inleft) { + orient = Orient.indownleft; + pos = hex(pos.row-1,pos.col+1); + } else if (orient == Orient.inupleft) { + orient = Orient.inleft; + pos = hex(pos.row-1,pos.col-1); + } else if (orient == Orient.inupright) { + orient = Orient.inupleft; + pos = hex(pos.row,pos.col+2); + } else if (orient == Orient.inright) { + orient = Orient.inupright; + pos = hex(pos.row+1,pos.col-1); + } else if (orient == Orient.indownright) { + orient = Orient.inright; + pos = hex(pos.row+1,pos.col+1); + } else if (orient == Orient.indownleft) { + orient = Orient.indownright; + pos = hex(pos.row-1,pos.col-1); + } else { + assert false; + } + else: + assert False + print len( hexs ) Added: problems/mit-acm/2006/3/prog3.py =================================================================== --- problems/mit-acm/2006/3/prog3.py (rev 0) +++ problems/mit-acm/2006/3/prog3.py 2010-04-05 14:23:10 UTC (rev 1619) @@ -0,0 +1,134 @@ +#!/usr/bin/env python +import sys +lines = sys.stdin.readlines() +left, upleft, upright, right, downright, downleft = range(6) +inleft, inupleft, inupright, inright, indownright, indownleft = range(6,12) +xs = [ 'left', 'upleft', 'upright', 'right', 'downright', 'downleft', 'inleft', 'inupleft', 'inupright', 'inright', 'indownright', 'indownleft' ] + +def trans( c, orient, pos ): + row,col=pos + if c == '1': + if orient == left: + orient = upleft + pos = (row-1,col-1) + elif orient == upleft: + orient = upright + pos = (row,col+2) + elif orient == upright: + orient = right + pos = (row+1,col-1) + elif orient == right: + orient = downright + pos = (row+1,col+1) + elif orient == downright: + orient = downleft + pos = (row,col-2) + elif orient == downleft: + orient = left + pos = (row-1,col+1) + elif orient == inleft: + orient = left + pos = (row-1,col+1) + elif orient == inupleft: + orient = upleft + pos = (row-1,col-1) + elif orient == inupright: + orient = upright + pos = (row,col+2) + elif orient == inright: + orient = right + pos = (row+1,col-1) + elif orient == indownright: + orient = downright + pos = (row+1,col+1) + elif orient == indownleft: + orient = downleft + pos = (row,col-2) + else: + assert False + elif c == '2': + if orient == left: + orient = inleft + pos = (row-1,col-1) + elif orient == upleft: + orient = inupleft + pos = (row,col+2) + elif orient == upright: + orient = inupright + pos = (row+1,col-1) + elif orient == right: + orient = inright + pos = (row+1,col+1) + elif orient == downright: + orient = indownright + pos = (row,col-2) + elif orient == downleft: + orient = indownleft + pos = (row-1,col+1) + elif orient == inleft: + orient = indownleft + pos = (row-1,col+1) + elif orient == inupleft: + orient = inleft + pos = (row-1,col-1) + elif orient == inupright: + orient = inupleft + pos = (row,col+2) + elif orient == inright: + orient = inupright + pos = (row+1,col-1) + elif orient == indownright: + orient = inright + pos = (row+1,col+1) + elif orient == indownleft: + orient = indownright + pos = (row,col-2) + else: + assert False + else: + assert False + return orient, pos +def xxs( o ): + if o is None: + return 'None' + else: + return xs[o] +open_left, open_right = 0, 1 +angle = [open_right, open_left, open_right, open_left, open_right, open_left, + open_left, open_right, open_left, open_right, open_left, open_right] +for line in lines[1:]: + hexs = set() + orig = pos = (0,0) + first = orient = None + for c in line.strip(): + if first is None: + first = c + start = True + if c == '1': + origorient = orient = left + elif c == '2': + origorient = orient = inleft + else: + assert False + else: + start = False + + oldorient = orient + oldpos = pos + row,col = pos + + if angle[orient] == open_left: + adjs = [(row,col),(row-1,col+1),(row+1,col+1)] + elif angle[orient] == open_right: + adjs = [(row,col),(row-1,col-1),(row+1,col-1)] + else: + assert False + hexs.update(adjs) + + if not start: + orient, pos = trans(c,orient,pos) + #print c, xxs(oldorient), oldpos, xs[orient], pos, adjs + orient, pos = trans(line[0],orient,pos) + assert origorient == orient, orient + assert orig == pos, pos + print len( hexs ) Property changes on: problems/mit-acm/2006/3/prog3.py ___________________________________________________________________ Added: svn:executable + * Added: problems/mit-acm/2006/3/rot.py =================================================================== --- problems/mit-acm/2006/3/rot.py (rev 0) +++ problems/mit-acm/2006/3/rot.py 2010-04-05 14:23:10 UTC (rev 1619) @@ -0,0 +1,5 @@ +#!/usr/bin/env python +import sys +s = sys.stdin.read().strip() +print len(s) +for i in range(len(s)): print s; s=s[1:]+s[0] Property changes on: problems/mit-acm/2006/3/rot.py ___________________________________________________________________ Added: svn:executable + * Added: problems/mit-acm/2006/3/test =================================================================== --- problems/mit-acm/2006/3/test (rev 0) +++ problems/mit-acm/2006/3/test 2010-04-05 14:23:10 UTC (rev 1619) @@ -0,0 +1,5 @@ +4 +111111 +1221111211212121111212 +2111121121121121111222 +2221111211221121111222211112211112 Added: problems/mit-acm/2006/4/prob4.py =================================================================== --- problems/mit-acm/2006/4/prob4.py (rev 0) +++ problems/mit-acm/2006/4/prob4.py 2010-04-05 14:23:10 UTC (rev 1619) @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +from commons import * +import sys +from itertools import * + +import psyco +psyco.full() + +debug = True +debug = False + +linez = sys.stdin.readlines() +while len(linez)>0: + lines = linez[:3] + del linez[:3] + k = int(lines[0].strip()) + a = [ int(x) - 1 for x in lines[1].split() ] + b = [ int(x) - 1 for x in lines[2].split() ] + n = 2*k + + seats = [ False ] * n + + indent = 0 + mincumcount = sys.maxint + + def go( ai, bi, cumcount ): + global indent,mincumcount + indent += 1 + if debug: + if cumcount >= mincumcount: + status = 'prune!' + else: + status = '' + print indent * ' ', 'go(%s,%s,%s)' % (ai,bi,cumcount), [{False:0,True:1}[x] for x in seats], mincumcount, status + if cumcount >= mincumcount: + ret = cumcount + else: + ret = -1 + assert ai <= len( a ) and bi <= len( b ) + + if ai < len( a ) and cumcount < mincumcount: + nextA = a[ai] + countLeft = len( [ i for i in seats[nextA:] if i ] ) + countRight = len( [ i for i in seats[:nextA] if i ] ) + count = min( countLeft, countRight ) + seats[nextA] = True + countA = go( ai+1, bi, cumcount + count ) + seats[nextA] = False + else: + countA = sys.maxint + + if bi < len( b ): + nextB = b[bi] + if nextB < k: + countLeft = len( [ i for i in seats[nextB:k] if i ] ) + countRight = len( [ i for i in seats[k:] if i ] ) + \ + len( [ i for i in seats[:nextB] if i ] ) + else: + countLeft = len( [ i for i in seats[:k] if i ] ) + \ + len( [ i for i in seats[nextB:] if i ] ) + countRight = len( [ i for i in seats[k:nextB] if i ] ) + count = min( countLeft, countRight ) + seats[nextB] = True + countB = go( ai, bi+1, cumcount + count ) + seats[nextB] = False + else: + countB = sys.maxint + + if ret == -1: + ret = min(countA,countB) + if ret == sys.maxint: + ret = cumcount + mincumcount = min( mincumcount, cumcount ) + print 'hit bottom', mincumcount + if debug: + print indent * ' ', ret + indent -= 1 + return ret + + print go(0,0,0) + #break Property changes on: problems/mit-acm/2006/4/prob4.py ___________________________________________________________________ Added: svn:executable + * Added: problems/mit-acm/2006/4/test =================================================================== --- problems/mit-acm/2006/4/test (rev 0) +++ problems/mit-acm/2006/4/test 2010-04-05 14:23:10 UTC (rev 1619) @@ -0,0 +1,31 @@ +3 +4 5 3 +6 2 1 +2 +1 2 +3 4 +2 +1 3 +2 4 +2 +2 1 +4 3 +2 +3 1 +4 2 +2 +1 2 +4 3 +2 +1 3 +4 2 +2 +2 1 +4 3 +2 +3 1 +4 2 +100 +0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 +100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |