Spit Problem (Codeforces 29A)
Tags: Others, brute force

In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.

Input: The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.

Output: If there are two camels, which spitted at each other, output YES. Otherwise, output NO.


Sample Inputs:
2
0 1
1 -1

3
0 1
1 1
2 -2

5
2 -10
3 10
0 5
5 -5
10 1

Sample Outputs:
YES

NO

YES

Solution (Python):
n = int(input())
wielblady = {}
for _ in range(n):
    (a, b) = list(map(int, input().split()))
    wielblady[a] = b

def czy_jest_para():
    global wielblady
    for x in wielblady:
        if x + wielblady[x] in wielblady:
            if wielblady[x + wielblady[x]] + x + wielblady[x] == x:
                return 'YES'
    return 'NO'
print(czy_jest_para())

Solution (Python):
n = int(input())
wielblady = {}
for _ in range(n):
    (a, b) = list(map(int, input().split()))
    wielblady[a] = b

def czy_para():
    global wielblady
    for x in wielblady:
        if x + wielblady[x] in wielblady:
            if wielblady[x + wielblady[x]] + x + wielblady[x] == x:
                return 'YES'
    return 'NO'
print(czy_para())

Solution (Python):
n = int(input())
a = []
d = []
for i in range(n):
    (a_i, d_i) = map(int, input().split())
    a.append(a_i)
    d.append(d_i)
for i in range(n):
    for j in range(i + 1, n):
        x = a[i] + d[i]
        y = a[j] + d[j]
        if x == a[j] and y == a[i]:
            print('YES')
            exit(0)
print('NO')