Schedule (Codeforces 31C)
Tags: Implement, implementation

At the beginning of the new semester there is new schedule in the Berland State University. According to this schedule, n groups have lessons at the room 31. For each group the starting time of the lesson and the finishing time of the lesson are known. It has turned out that it is impossible to hold all lessons, because for some groups periods of their lessons intersect. If at some moment of time one groups finishes it's lesson, and the other group starts the lesson, their lessons don't intersect.The dean wants to cancel the lesson in one group so that no two time periods of lessons of the remaining groups intersect. You are to find all ways to do that.

Input: The first line contains integer n (1 ≤ n ≤ 5000) — amount of groups, which have lessons in the room 31. Then n lines follow, each of them contains two integers li ri (1 ≤ li < ri ≤ 106) — starting and finishing times of lesson of the i-th group. It is possible that initially no two lessons intersect (see sample 1).

Output: Output integer k — amount of ways to cancel the lesson in exactly one group so that no two time periods of lessons of the remaining groups intersect. In the second line output k numbers — indexes of groups, where it is possible to cancel the lesson. Groups are numbered starting from 1 in the order that they were given in the input. Output the numbers in increasing order.


Sample Inputs:
3
3 10
20 30
1 3

4
3 10
20 30
1 3
1 39

3
1 5
2 6
3 7

Sample Outputs:
3
1 2 3

1
4

0

Solution (Python):
n = int(input().strip())
lessons = []
keypoints = []
for i in range(n):
    (s, e) = tuple((int(x) for x in input().strip().split()))
    lessons.append((s, e))
    keypoints.append((s, 1))
    keypoints.append((e, -1))
keypoints.sort()
(k, start, end) = (0,) * 3
for (t, dk) in keypoints:
    k += dk
    if k > 2:
        print('0')
        exit(0)
    if k == 2:
        start = t if not start else start
        end = t
out = []
for (i, lesson) in enumerate(lessons):
    (ls, le) = lesson
    if not start or (start >= ls and end < le):
        out.append(i + 1)
print(len(out))
print(*out)

Solution (Python):
""""""
n = int(input().strip())
lessons = []
keypoints = []
for i in range(n):
    (s, e) = tuple((int(x) for x in input().strip().split()))
    lessons.append((s, e))
    keypoints.append((s, 1))
    keypoints.append((e, -1))
keypoints.sort()
(k, start, end) = (0,) * 3
for (t, dk) in keypoints:
    k += dk
    if k > 2:
        print('0')
        exit(0)
    if k == 2:
        start = t if not start else start
        end = t
out = []
for (i, lesson) in enumerate(lessons):
    (ls, le) = lesson
    if not start or (start >= ls and end < le):
        out.append(i + 1)
print(len(out))
print(*out)

Solution (Python):
N = int(input())
allMeetings = [None] * N
for i in range(N):
    inLine = input().split(' ')
    (start, finish) = (int(inLine[0]), int(inLine[1]))
    allMeetings[i] = (start, finish, i + 1)
allMeetings.sort()
overlaps = [set() for _ in range(N)]
totalOverlaps = 0
for i in range(N):
    (start, finish, meetingNum) = allMeetings[i]
    j = i + 1
    while j < N and allMeetings[j][0] < finish:
        overlaps[meetingNum - 1].add(allMeetings[j][2])
        overlaps[allMeetings[j][2] - 1].add(meetingNum)
        totalOverlaps += 1
        j += 1
if totalOverlaps == 0:
    print(N)
    allRooms = [str(i + 1) for i in range(N)]
    print(' '.join(allRooms))
else:
    resSet = {}
    for overlapSet in overlaps:
        if len(overlapSet) == 1:
            meetingNum = overlapSet.pop()
            overlapSet.add(meetingNum)
            resSet[meetingNum] = resSet.get(meetingNum, 1)
        else:
            for num in overlapSet:
                if num in resSet:
                    resSet[num] = -1
    res = []
    for num in resSet:
        if resSet[num] > 0:
            res.append(num)
    if len(resSet) > 2:
        print(0)
    elif len(resSet) == 2:
        res = sorted(resSet)
        if overlaps[res[0] - 1].pop() != res[1] or overlaps[res[1] - 1].pop() != res[0]:
            print(0)
        else:
            print(len(res))
            if len(res) > 0:
                print(' '.join([str(num) for num in res]))
    else:
        res.sort()
        print(len(res))
        if len(res) > 0:
            print(' '.join([str(num) for num in res]))