Problem:


Score : 700 points

Problem Statement

There are N bags, each containing two white balls. The i-th box contains two balls with integers x_i and y_i written on them, respectively.

For each of these bags, you will paint one of the balls red, and paint the other blue.

Afterwards, the 2N balls will be classified according to color.

Then, we will define the following:

R_{max}: the maximum integer written on a ball painted in red

R_{min}: the minimum integer written on a ball painted in red

B_{max}: the maximum integer written on a ball painted in blue

B_{min}: the minimum integer written on a ball painted in blue

Find the minimum possible value of (R_{max} - R_{min}) \times (B_{max} - B_{min}).

Constraints

1 ≤ N ≤ 200,000

1 ≤ x_i, y_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum possible value.

Sample Input 1

3
1 2
3 4
5 6

Sample Output 1

15

The optimal solution is to paint the balls with x_1, x_2, y_3 red, and paint the balls with y_1, y_2, x_3 blue.

Sample Input 2

3
1010 10
1000 1
20 1020

Sample Output 2

380

Sample Input 3

2
1 1
1000000000 1000000000

Sample Output 3

999999998000000001



Sample input:
3
1 2
3 4
5 6


Sample output:
15

Submission status: Wrong Answer

Solution code:
n = int(input())
r_max, b_max = map(int, input().split())
r_min, b_min = r_max, b_max


def compaere(xy, _max, _min):
    if xy > _max:
        _x = xy - _min
        f = 1
    elif xy < _min:
        _x = _max - xy
        f = 2
    elif xy >= _min and xy <= _max:
        _x = _max - _min
        f = 0
    return _x, f


for _ in range(n - 1):
    x, y = map(int, input().split())

    r_x, r_x_f = compaere(x, r_max, r_min)
    r_y, r_y_f = compaere(y, r_max, r_min)
    b_x, b_x_f = compaere(x, b_max, b_min)
    b_y, b_y_f = compaere(y, b_max, b_min)

    if r_x * b_y <= b_x * r_y:
        if not r_x_f:
            if r_x_f == 1:
                r_max = x
            else:
                r_min = x
        if not b_y_f:
            if b_y_f == 1:
                b_max = y
            else:
                b_min = y

    else:
        if not b_x_f:
            if b_x_f == 1:
                b_max = x
            else:
                b_min = x
        if not r_y_f:
            if r_y_f == 1:
                r_max = y
            else:
                r_min = y

print((r_max - r_min) * (b_max - b_min))

Submission status: Wrong Answer

Solution code:
n=int(input())
xys=[sorted(map(int,input().split())) for _ in range(n)]
xs=[xy[0] for xy in xys]
ys=[xy[1] for xy in xys]
zs=sorted(x for xy in xys for x in xy)
print(min(
    (max(xs)-min(xs))*(max(ys)-min(ys)),
    (zs[-1]-zs[0])*(zs[-2]-zs[1])))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# 整数の入力
#a = int(raw_input())
# スペース区切りの整数の入力
import heapq
N = int(input())

Rmax = 0
Rmin = 1000000001
Bmax = 0
Bmin = 1000000001

data = sorted([sorted(list(map(int, input().split()))) for i in range(N)],key=lambda x:x[0])

tempBlues = sorted([d[1] for d in data])
tempReds = [d[0] for d in data]
heapq.heapify(tempReds)
print(tempReds)

Rmin = data[0][0]
Rmax = data[N-1][0]
Bmax = max(tempBlues)
Bmin = min(tempBlues)

minValue = (Rmax - Rmin)*(Bmax - Bmin)
Bmin = Rmin

for i in range(N):
    heapMin = heapq.heappop(tempReds)
    if heapMin == data[i][0]:
        heapq.heappush(tempReds, data[i][1])
        if  data[i][1] > Rmax:
            Rmax = data[i][1]
        if (Rmax - tempReds[0])*(Bmax - Bmin) < minValue:
            minValue = (Rmax - tempReds[0])*(Bmax - Bmin)
    else:
        break

print(minValue)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
r=0
if xy[0][1]>xy[n-1][0]:r=1
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(n-1-r):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=x
    bmx=max(bmx,xy[max(0,i-1)][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(n-1):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
r=0
if xy[0][1]>xy[n-1][0]:r=1
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(n-1-r):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
r=0
if xy[0][1]>xy[n-1][0]:r=1
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n-1-r):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
ymn=[10**12]*n
rmn=bmn;bmn=min(xy[n-1][0],xy[0][1]);bmx=max(xy[n-1][0],xy[0][1])
for i in xrange(1,n):
    x,y=xy[i]
    ymn[i]=min(ymn[i-1],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n-1):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=min(bmn,x)  
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n-1):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n):
    x,y=xy[i]
    if ymn[i]<x:
        bmn=max(bmn,ymn[i-1])
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n-1):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
bmn=max(bmn,ymn[i-1])
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n-1):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
bmn=min(bmn,ymn[i-1])
ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def input_str(row):
    res = []
    for _ in range(row):
        res.append(raw_input().split())
    return res

N = int(raw_input())
balls = input_str(N)
balls = sorted([sorted([int(x), int(y)]) for x, y in balls], key=lambda x: x[0])
#rmin == min, bmax == max
xmax = max(balls, key=lambda x: x[0])[0]
xmin = min(balls, key=lambda x: x[0])[0]
ymax = max(balls, key=lambda x: x[1])[1]
ymin = min(balls, key=lambda x: x[1])[1]
res1 = (xmax - xmin) * (ymax - ymin)
print res1
"""
maxdiff = max(xmax, ymax) - min(xmin, ymin)
#rmin == min, rmax == max
diff = xmax - xmin
mina = xmax

for i in range(N)[:-1]:
    mina = min(balls[i][1], mina)
    xmax = max(balls[i][1], xmax)
    xmin = min(balls[i+1][1], mina)
    diff = min(diff, xmax - xmin)

res2 = maxdiff * diff

print min(res1, res2)
"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def input_str(row):
    res = []
    for _ in range(row):
        res.append(raw_input().split())
    return res

N = int(raw_input())
balls = input_str(N)
balls = sorted([sorted([int(x), int(y)]) for x, y in balls], key=lambda x: x[0])
#rmin == min, bmax == max
xmax = max(balls, key=lambda x: x[0])[0]
xmin = min(balls, key=lambda x: x[0])[0]
ymax = max(balls, key=lambda x: x[1])[1]
ymin = min(balls, key=lambda x: x[1])[1]
res1 = (xmax - xmin) * (ymax - ymin)

maxdiff = max(xmax, ymax) - min(xmin, ymin)
#rmin == min, rmax == max
diff = xmax - xmin
mina = xmax

for i in range(N)[:-1]:
    mina = min(balls[i][1], mina)
    xmax = max(balls[i][1], xmax)
    xmin = min(balls[i+1][1], mina)
    diff = min(diff, xmax - xmin)

res2 = maxdiff * diff

print min(res1, res2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def input_str(row):
    res = []
    for _ in range(row):
        res.append(raw_input().split())
    return res

N = int(raw_input())
balls = input_str(N)
balls = sorted([sorted([int(x), int(y)]) for x, y in balls], key=lambda x: x[0])
#rmin == min, bmax == max
xmax = max(balls, key=lambda x: x[0])[0]
xmin = min(balls, key=lambda x: x[0])[0]
ymax = max(balls, key=lambda x: x[1])[1]
ymin = min(balls, key=lambda x: x[1])[1]
res1 = (xmax - xmin) * (ymax - ymin)

maxdiff = max(xmax, ymax) - min(xmin, ymin)
#rmin == min, rmax == max
diff = xmax - xmin
mina = xmax
for i in range(N)[:-1]:
    balls[i].reverse()
    mina = min(balls[i][0], mina)
    xmax = max(balls[i][0], xmax)
    xmin = min(balls[i+1], mina)
    if xmax - xmin < diff:
        diff = xmax - xmin

res2 = maxdiff * diff

print min(res1, res2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n-1):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
mn=10**12;mx=0
for i in xrange(n):
    x,y=xy[i]
    mn=min(mn,x)
    mx=max(mx,y)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if mx==y:cntmx+=1
    if mn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==mn and xy[0][1]==mx:
    rmn=xy[0][1];rmx=xy[0][1];bmn=xy[0][0];bmx=xy[0][0]
    for i in xrange(1,n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
    print (rmx-rmn)*(bmx-bmn)
    exit()
else:
    rmn=10**12;rmx=0;bmn=10**12;bmx=0
    for i in xrange(n):
        x,y=xy[i]
        rmn=min(rmn,y)
        rmx=max(rmx,y)
        bmn=min(bmn,x)
        bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
rmn=10**12;rmx=0;bmn=10**12;bmx=0
for i in xrange(n):
    x,y=xy[i]
    rmn=min(rmn,y)
    rmx=max(rmx,y)
    bmn=min(bmn,x)
    bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if rmx==y:cntmx+=1
    if bmn==x:cntmn+=1
if cntmx==1 and cntmn==1 and xy[0][0]==bmn and xy[0][1]==rmx:
    print ans1
    exit()
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
rmn=10**12;rmx=0;bmn=10**12;bmx=0
for i in xrange(n):
    x,y=xy[i]
    rmn=min(rmn,y)
    rmx=max(rmx,y)
    bmn=min(bmn,x)
    bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
cntmx=0;cntmn=0
for i in xrange(n):
    x,y=xy[i]
    if rmx==y:cntmx+=1
    if bmn==x:cntmx+=1
if cntmx==1 and cntmn==1 and xy[0][0]==bmn and xy[0][1]==rmx:
    print ans1
    exit()
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
rmn=10**12;rmx=0;bmn=10**12;bmx=0
for i in xrange(n):
    x,y=xy[i]
    rmn=min(rmn,y)
    rmx=max(rmx,y)
    bmn=min(bmn,x)
    bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
ans2=(rmx-rmn)*(bmx-bmn)
for i in xrange(1,n):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x
    bmx=max(bmx,xy[i-1][1])
    ans2=min(ans2,(rmx-rmn)*(bmx-bmn))
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
xy=[]
for i in xrange(n):
    x,y=map(int,raw_input().split())
    if x>y:x,y=y,x
    xy.append((x,y))
xy.sort()
rmn=10**12;rmx=0;bmn=10**12;bmx=0
for i in xrange(n):
    x,y=xy[i]
    rmn=min(rmn,y)
    rmx=max(rmx,y)
    bmn=min(bmn,x)
    bmx=max(bmx,x)
ans1=(rmx-rmn)*(bmx-bmn)
xy[0]=(xy[0][1],xy[0][0])
xy.sort()
ymn=[10**12]*n
rmn=bmn;bmn=xy[0][0];bmx=xy[n-1][0]
for i in xrange(n):
    x,y=xy[i]
    ymn[i]=min(ymn[max(0,i-1)],y)
for i in xrange(n):
    x,y=xy[i]
    if ymn[i]<x:
        break
    bmn=x   
ans2=(rmx-rmn)*(bmx-bmn)
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
def calc(x, y):
    global rmax, rmin, bmax, bmin
    xrmax = max(rmax, x)
    xrmin = min(rmin, x)
    yrmax = max(rmax, y)
    yrmin = min(rmin, y)
    xbmax = max(bmax, x)
    xbmin = min(bmin, x)
    ybmax = max(bmax, y)
    ybmin = min(bmin, y)
    xr = (xrmax - xrmin) * (ybmax - ybmin)
    yr = (yrmax - yrmin) * (xbmax - xbmin)
    if xr <= yr:
        rmax, rmin, bmax, bmin = xrmax, xrmin, ybmax, ybmin
    else:
        rmax, rmin, bmax, bmin = yrmax, yrmin, xbmax, xbmin


n = int(input())
x1, y1 = map(int, input().split())
rmax, rmin, bmax, bmin = x1, x1, y1, y1
for _ in range(n - 1):
    calc(*map(int, input().split()))
print((rmax - rmin) * (bmax - bmin))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# 整数の入力
#a = int(raw_input())
# スペース区切りの整数の入力
import numpy as np
import itertools

N = int(input())

Rmax = 0
Rmin = 1000000001
Bmax = 0
Bmin = 1000000001

for i in range(N):
    x,y = list(map(int, input().split()))
    if x > y:
        if x > Rmax:
            Rmax = x
            if y > Bmax:
                Bmax = y
        if y < Bmin:
            Bmin = y
            if x < Rmin:
                Rmin = x
    else:
        if y > Rmax:
            Rmax = y
            if x > Bmax:
                Bmax = x
        if x < Bmin:
            Bmin = x
            if y < Rmin:
                Rmin = y
if Rmin < Bmax:
    print((Rmax - Bmin)*(Bmax - Rmin))
else:
    print((Rmax - Rmin)*(Bmax - Bmin))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter

inf = 1 << 30

def solve():
    n = int(sys.stdin.readline())

    x = []
    y = []

    # r_max = MAX, b_min = MIN にしたとき

    r_max = 0
    r_min = inf
    b_max = 0
    b_min = inf

    for i in range(n):
        xi, yi = map(int, sys.stdin.readline().split())
        x.append(xi)
        y.append(yi)

        a = max(xi, yi)
        b = min(xi, yi)

        r_max = max(r_max, a)
        r_min = min(r_min, a)
        b_max = max(b_max, b)
        b_min = min(b_min, b)

    ans1 = (r_max - r_min) * (b_max - b_min)

    # r_max = MAX, r_min = MIN にしたとき

    xy = []

    for i in range(n):
        if x[i] < y[i]:
            xy.append((x[i], y[i]))
        else:
            xy.append((y[i], x[i]))
            
    xy.sort(key=itemgetter(0))

    ans2 = (r_max - b_min)

    tmp = xy[-1][0] - xy[0][0]

    y_min = inf
    y_max = 0

    for i in range(n):
        y_min = min(y_min, xy[i][1])
        y_max = max(y_max, xy[i][1])

        min_ = min(xy[i][0], y_min)
        max_ = max(xy[-1][0], y_max)

        tmp = min(tmp, max_ - min_)

    ans2 *= tmp

    ans = min(ans1, ans2)

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter

inf = 1 << 30

def solve():
    n = int(sys.stdin.readline())

    x = []
    y = []

    # r_max = MAX, b_min = MIN にしたとき

    r_max = 0
    r_min = inf
    b_max = 0
    b_min = inf

    for i in range(n):
        xi, yi = map(int, sys.stdin.readline().split())
        x.append(xi)
        y.append(yi)

        a = max(xi, yi)
        b = min(xi, yi)

        r_max = max(r_max, a)
        r_min = min(r_min, a)
        b_max = max(b_max, b)
        b_min = min(b_min, b)

    ans1 = (r_max - r_min) * (b_max - b_min)

    # r_max = MAX, r_min = MIN にしたとき

    xy = list(zip(x, y))
    xy.sort(key=itemgetter(0))

    ans2 = (r_max - b_min)

    tmp = inf

    y_min = inf
    y_max = 0

    for i in range(n):
        y_min = min(y_min, xy[i][1])
        y_max = max(y_max, xy[i][1])

        min_ = min(xy[i][0], y_min)
        max_ = max(xy[-1][0], y_max)

        tmp = min(tmp, max_ - min_)

    ans2 *= tmp

    ans = min(ans1, ans2)

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y
ret = (ymax-ymin) * (xmax-xmin)


p.sort()
dx = p[-1][0] - p[0][0]
tymin = float('inf')
tymax = 0
for i in range(N-1):
    # print(i, dx, xmax,xmin)
    tymin = min(tymin, p[i][1])
    xmax = max(xmax, p[i][1])
    xmin = min(tymin, p[i+1][1])
    ymin = min(ymin, p[i][0])
    dx = min(dx, xmax - xmin)

# print(ret, (ymax-ymin) * (xmax-xmin) ,(ymax,ymin), (xmax,xmin))
print(min(ret, (ymax-ymin) * dx))

Submission status: Wrong Answer

Solution code:
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y
ret = (ymax-ymin) * (xmax-xmin)


p.sort()
dx = p[-1][0] - p[0][0]
tymin = float('inf')
tymax = 0
for i in range(N-1):
    # print(i, dx, xmax,xmin)
    tymin = min(tymin, p[i][1])
    xmax = max(xmax, p[i][1])
    xmin = min(tymin, p[i+1][1])
    ymin = min(ymin, p[i][0])
    dx = min(dx, xmax - xmin)
# print(ret, (ymax-ymin) * (xmax-xmin) ,(ymax,ymin), (xmax,xmin))
print(min(ret, (ymax-ymin) * (xmax-xmin)))

Submission status: Wrong Answer

Solution code:
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y

ret = (ymax-ymin) * (xmax-xmin)
ymin = xmin
p.sort()
xmin = p[0][0]
xmax = p[-1][0]
for i in range(N-1):
    if p[i][0] == xmin and p[i][1] <= xmax:
        xmin = min(p[i][1], p[i+1][0])
    else:
        break
print(min(ret, (ymax-ymin) * (xmax-xmin)))

Submission status: Wrong Answer

Solution code:
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y

ret = (ymax-ymin) * (xmax-xmin)

p.sort()
ymin = xmin
for i in range(N-1):
    if p[i][0] == xmin and p[i][1] < xmax:
        xmin = min(p[i][1], p[i+1][0])
    else:
        break
print(min(ret, (ymax-ymin) * (xmax-xmin)))

Submission status: Wrong Answer

Solution code:
from bisect import insort

n=int(input())
x,y,p=[],[],[]
for i in range(n):
    a,b=map(int,input().split())
    if a > b: a,b=b,a
    x.append(a)
    y.append(b)
    p.append((a,i,True))
    p.append((b,i,False))
p.sort()
ans=(max(x)-min(x))*(max(y)-min(y))
if p[0][1] != p[-1][1]:
    a=p[-1][0] - p[0][0]
    bs=[]
    bs.append((y[p[0][1]] if p[0][2] else x[p[0][1]], p[0][1]))
    bs.append((y[p[-1][1]] if p[-1][2] else x[p[-1][1]], p[-1][1]))
    for i in range(n):
        if i in (p[0][1], p[-1][1]):
            continue
        bs.append((x[i],i))
    bs.sort()
    b=bs[-1][0]-bs[0][0]
    for i in range(n):
        if x[bs[0][1]] == bs[0][0]:
            break
        v,j=bs.pop(0)
        insort(bs,(y[j],j))
        b=min(b, bs[-1][0]-bs[0][0])
    ans=min(ans, a*b)
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import insort

n=int(input())
x,y,p=[],[],[]
for i in range(n):
    a,b=map(int,input().split())
    if a > b: a,b=b,a
    x.append(a)
    y.append(b)
    p.append((a,i,True))
    p.append((b,i,False))
p.sort()
ans=(max(x)-min(x))*(max(y)-min(y))
if p[0][1] != p[-1][1]:
    a=p[-1][0] - p[0][0]
    bs=[]
    bs.append((y[p[0][1]] if p[0][2] else x[p[0][1]], p[0][1]))
    bs.append((y[p[-1][1]] if p[-1][2] else x[p[-1][1]], p[-1][1]))
    for i in range(n):
        if i in (p[0][1], p[-1][1]):
            continue
        bs.append((x[i],i))
    bs.sort()
    b=bs[-1][0]-bs[0][0]
    for i in range(n):
        if bs[i][1] in (p[0][1], p[-1][1]):
            continue
        if x[bs[0][1]] == bs[0][0]:
            break
        v,j=bs.pop(0)
        insort(bs,(y[j],j))
        b=min(b, bs[-1][0]-bs[0][0])
    ans=min(ans, a*b)
print(ans)