Skip to content

Instantly share code, notes, and snippets.

@PirosB3
Created February 10, 2015 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PirosB3/130e3b016e5bc340d9c6 to your computer and use it in GitHub Desktop.
Save PirosB3/130e3b016e5bc340d9c6 to your computer and use it in GitHub Desktop.
import bisect
def main():
with open('rope_large.in') as f:
n = int(f.readline())
for n in xrange(n):
amount = int(f.readline())
items_left = []
items_right = []
for _ in xrange(amount):
left, right = map(int, f.readline().split(' '))
item = (left, right)
items_left.append((left, item,))
items_right.append((right, item,))
items_left.sort()
items_right.sort()
items_left_without_idx = [item for _, item in items_left]
items_right_without_idx = [item for _, item in items_right]
total_count = 0
already_completed = set()
MEMO = {}
for left_idx, (_, item) in enumerate(items_left):
# reconstruct right item
left, right = item
item_right = (right, item)
# find position in array
right_idx = bisect.bisect_left(items_right, item_right)
#top-left to bottom-right merge
len_items = len(items_left_without_idx) - 1
set_1 = set(items_left_without_idx[:left_idx]) & set(items_right_without_idx[right_idx+1:])
set_2 = set(items_left_without_idx[left_idx+1:]) & set(items_right_without_idx[:right_idx])
all_intersections = set_1 | set_2
# Remove duplicates by adding to master set
already_completed |= set(frozenset((item, intersection,)) for intersection in all_intersections)
print "Case #%s: %s" % (n+1, len(already_completed))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment