Petya and Post (Codeforces 66E)
Tags: DP, data structures

Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to n. One can fuel the car at the i-th station with no more than ai liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is b1 kilometers, the distance between the 2-nd and the 3-rd one is b2 kilometers, ..., between the (n - 1)-th and the n-th ones the distance is bn - 1 kilometers and between the n-th and the 1-st one the distance is bn kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all ai is equal to the sum of all bi. The i-th gas station and i-th post office are very close, so the distance between them is 0 kilometers.Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it?Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem.

Input: The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ones, respectively. The sum of all bi equals to the sum of all ai and is no more than 109. Each of the numbers ai, bi is no less than 1 and no more than 109.

Output: Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start.


Sample Inputs:
4
1 7 2 3
8 1 1 3

8
1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1

Sample Outputs:
2
2 4

8
1 2 3 4 5 6 7 8

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Sa = [0] * n
Sb = [0] * n
Sa[0] = A[0]
Sb[0] = B[0]
for i in range(1, n):
    Sa[i] = Sa[i - 1] + A[i]
    Sb[i] = Sb[i - 1] + B[i]
D = [0] * n
for i in range(n):
    D[i] = Sa[i] - Sb[i]
m = min(D)
ans = []
for i in range(n):
    if D[i] == m:
        ans.append(i + 1)
        if ans[-1] == n:
            ans[-1] = 0
Sa[n - 1] = A[n - 1]
B = [B[-1]] + B[:n - 1]
Sb[n - 1] = B[n - 1]
for i in range(n - 2, -1, -1):
    Sa[i] = Sa[i + 1] + A[i]
    Sb[i] = Sb[i + 1] + B[i]
for i in range(n):
    D[i] = Sa[i] - Sb[i]
m = min(D)
for i in range(n):
    if D[i] == m:
        ans.append(i - 1)
        if ans[-1] == -1:
            ans[-1] = n - 1
ans = sorted(set(ans))
print(len(ans))
for item in ans:
    print(item + 1, end=' ')

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
INF = 1000000000000009
ml = INF
mr = INF
i = 0
first = True
cur = 0
while first or i != 0:
    cur += a[i]
    cur -= b[i]
    i += 1
    i %= n
    ml = min(ml, cur)
    first = False
first = True
cur = 0
i = 0
while first or i != 0:
    cur += a[i]
    o1 = a[i]
    next = i - 1
    if next < 0:
        next = n - 1
    cur -= b[next]
    o2 = a[next]
    mr = min(mr, cur)
    i = next
    first = False
ans = []
if ml >= 0 or mr >= 0:
    ans.append(1)
for i in range(1, n):
    minus = a[i - 1] - b[i - 1]
    ml -= minus
    plus = a[i] - b[i - 1]
    mr += plus
    if ml >= 0 or mr >= 0:
        ans.append(i + 1)
print(len(ans))
for x in ans:
    print(x, end=' ')

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
(mi, x) = (0, 0)
for (i, j) in zip(a, b):
    x += i - j
    mi = min(mi, x)
(u, x) = ([0] * (n + 1), 0)
for i in range(n):
    if x == mi:
        u[i + 1] = 1
    x += a[i] - b[i]
a.reverse()
b.reverse()
b = b[1:] + [b[0]]
(mi, x) = (0, 0)
for (i, j) in zip(a, b):
    x += i - j
    mi = min(mi, x)
for i in range(n):
    if x == mi:
        u[n - i] = 1
    x += a[i] - b[i]
ans = []
for i in range(1, n + 1):
    if u[i]:
        ans.append(i)
k = len(ans)
print(k)
sys.stdout.write(' '.join(map(str, ans)))