changyuheng's blog

By changyuheng, 12 years ago, In English

Here is my code for 222 B


table = [] n, m, k = map(int, raw_input().split()) [table.append(raw_input().split()) for i in xrange(n)] rs, cs = [i for i in xrange(n)], [i for i in xrange(m)] for i in xrange(k): s, x, y = raw_input().split() x, y = int(x) - 1, int(y) - 1 if s == "c": buf = cs[x] cs[x] = cs[y] cs[y] = buf elif s == "r": buf = rs[x] rs[x] = rs[y] rs[y] = buf else: print table[rs[x]][cs[y]]

It exceeded the time limit. I do not see any solution written in Python so come here for help. Could somebody give me an advise?

UPDATE: Thanks everybody. I just upload another solution in which replace the swap by buffer with by xor swap algorithm. It passed with 80ms under the time bound.

And I just saw that dkirienko submitted another solution with the a, b = b, a swap mechanism of Python. It speeds up a bit more.

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

its mayb because of Large input data set. Python's IO is too slow.

»
12 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You can slightly optimize your program if you will use tuples instead of temporary variable buf to swap two elements of list:

cs[x], cs[y] = cs[y], cs[x]

Its enough for this problem. See submision 2128261