Created
February 10, 2015 16:01
-
-
Save PirosB3/130e3b016e5bc340d9c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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