Problem:


Score : 500 points

Problem Statement

In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.

Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours.
Here, we are using 24-hour notation.
That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.

Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.

Find the maximum possible value of s.

Constraints

1 \leq N \leq 50

0 \leq D_i \leq 12

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the maximum possible value of s.

Sample Input 1

3
7 12 8

Sample Output 1

4

For example, consider the situation where it is 7, 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.

Sample Input 2

2
11 11

Sample Output 2

2

Sample Input 3

1
0

Sample Output 3

0

Note that Takahashi himself is also a participant.



Sample input:
3
7 12 8


Sample output:
4

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())
D = list(map(int, sys.stdin.buffer.readline().split()))

D.sort()


def dist(h, counts):
    i = 0
    while counts[(h - i) % 24] == counts[(h + i) % 24] == 0:
        i += 1
    return i


counts = [0] * 25
counts[0] = 1
counts[24] = 1
for i in range(N):
    if dist(D[i], counts) > dist(24 - D[i], counts):
        counts[D[i]] += 1
    else:
        counts[24 - D[i]] += 1

if max(counts) >= 2:
    print(0)
    exit()

cc = []
for i in range(len(counts)):
    if counts[i]:
        cc.append(i)

ans = 24
for a, b in zip(cc, cc[1:]):
    ans = min(ans, abs(a - b))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
l1=sorted(l)
import copy
if l1[0]==0:
  print(0)
else:
  d={}
  for i in range(1,13):
    d[i]=0
  for i in range(n):
    d[l1[i]]+=1
#12時を高橋くんの時刻とする
  d1=[]
  d2=[]
  d1.append(12)
  for i in range(1,13):
    if d[i]>=3:
      print(0)
      exit()
    elif d[i]==2:
      d1.append(12+i)
      d1.append(12-i)
      d[i]=0
    elif d[i]==1:
      d2.append(i)
  #ここまでで登場回数1のみが残る
  num=len(d2)
  num1=2**num-1
  bitlist=[]
  b=[]
  while num1!=0:
    b.append(num1%2)
    num1//=2
  bitlist.append(b)
  for i in range(2,2**num+1):
    num1=2**num-i
    b=[]
    while num1!=0 or len(b)<len(bitlist[0]):
      b.append(num1%2)
      num1//=2
    bitlist.append(b)
  #1なら12+i1 0なら12-i
  m=0
  for i in range(2**num):
    d3=copy.deepcopy(d1)
    b1=bitlist[i]
    m1=24
    for j in range(num):
      if b1[j]==1:
        d3.append(12+d2[j])
      else:
        d3.append(12-d2[j])
    d3=sorted(d3)
    for i in range(1,len(d3)):
      if m1>d3[i]-d3[i-1]:
        m1=d3[i]-d3[i-1]
    if m1>(24-d3[-1])+d3[0]:
      m1=(24-d3[-1])+d3[0]
    if m<m1:
      m=m1
  print(m)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int,input().split()))

DC = {}
for d in D:
  if d == 0:
    print(0)
    exit()
  if d in DC:
    DC[d] += 1
  else:
    DC[d] = 1

def Gap(T):
  res = 10**18
  mn = 0
  for i in range(1,len(T)):
    if T[i]:
      res = min(res,i-mn)
      mn = i
  return res

T = [True] + [False for _ in range(23)] + [True]
DD = []
for k,v in DC.items():
  if v == 1:
    DD.append(k)
  elif v == 2:
    T[k] = True
    T[24-k] = True
  else:
    print(0)
    exit()

if len(DD) == 0:
  print(Gap(T))
else:
  ans = 0
  for i in range(1<<len(DD)):
    TT = T[:]
    for j in range(len(DD)):
      if (i>>j)&1 == 1:
        TT[DD[j]] = True
      else:
        TT[24-DD[j]] = True
    ans = max(ans,Gap(TT))
  print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=[int(j) for j in input().split()]
d.sort()
d=[0]+[j if i&1 else 24-j for i,j in enumerate(d,1)]
d.sort()
ans=min(j-i for i,j in zip(d,d[1:]))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
D=list(map(int,input().split()))
c=Counter(D)
  
S=[]
D=[]
for i in c:
  if c[i]==1:
    S.append(i)
  elif c[i]==2:
    D.append(i)
    D.append(24-i)
  else:
    print(0)
    exit()

n=len(S)
ans=-1
for i in range(2**n):
  L=[d for d in D]
  for j in range(n):
    if (i>>j)&1==0:
      L.append(S[j])
    else:
      L.append(24-S[j])
    
  L.append(0)
  L.sort()
  m=25
  for i in range(N+1):
    d=abs(L[i]-L[i-1])
    m=min(d,24-d,m)
  ans=max(ans,m)
  
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
d.sort()
for i in range(n):
    if i%2:
        d[i]=24-d[i]
d.insert(0,0)
d.sort()
ans=12
for i in range(1,n+1):
    if ans>d[i]-d[i-1]:
        ans=d[i]-d[i-1]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, D):
    D.append(0)
    C = Counter(D)
    if C[0] > 1 or C[12] > 1:
        return 0
    

    K = []
    F = []
    for k in C:
        if C[k] > 2:
            return 0
        elif C[k] == 2:
            F.append(24-k)
            F.append(k)
        else:
            K.append(k)
    M = len(K)
    ans = 0
    for S in product([1, -1], repeat=M):
        tmp = []
        for s, v in zip(S, K):
            tmp.append(s*v)
        tmp.extend(F)
        tmp.sort()
        cand = INF
        for i in range(len(tmp)):
            d = abs(tmp[i] - tmp[i-1])
            cand = min(cand, min(24-d, d))

        ans = max(ans, cand)

    return ans

def main():
    N = read_int()
    D = read_int_n()
    print(slv(N, D))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque

N = int(input())

D = list(map(int,input().split()))

ans = 0

dic = {}
dic[0] = 1

for i in D:

    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

q = deque([[]])

for i in dic:
    nq = deque([])

    if dic[i] >= 3 or (i in [0,12] and dic[i] >= 2):
         print (0)
         sys.exit()

    elif dic[i] == 2:

        for j in q:
            nj = j.copy()
            nj.append(i)
            nj.append(24-i)
            nq.append(nj)

    else:

        for j in q:

            nj = j.copy()
            nj.append(i)
            nq.append(nj)

            if i not in [0,12]:
                nj = j.copy()
                nj.append(24-i)
                nq.append(nj)

    q = nq

#print (q)

while len(q) > 0:

    nlis = q.pop()
    nlis.sort()

    nlis.append(nlis[0])
    nmin = float("inf")

    for i in range(len(nlis)-1):
        
        nmin = min(nmin, abs(nlis[i] - nlis[i+1]) , 24 - abs(nlis[i] - nlis[i+1]))

    ans = max(ans,nmin)

print (ans)

Submission status: Accepted

Solution code:
def main():
    from collections import Counter
    def check(l):
        ll=[]
        for i in range(24):
            if l[i]:
                ll.append(i)
                ll.append(i+24)
        ll.sort()
        return min([ll[i+1]-ll[i] for i in range(len(ll)-1)])

    def dfs(d_dfs,l_dfs):
        if len(d_dfs)==0:
            return check(l_dfs)
        l_dfs1=[i for i in l_dfs]
        l_dfs2=[i for i in l_dfs]
        d_dfs1=[i for i in d_dfs]
        i=d_dfs1.pop()
        l_dfs1[i]=True
        l_dfs2[24-i]=True
        return max(dfs(d_dfs1,l_dfs1),dfs(d_dfs1,l_dfs2))

    n=int(input())
    a=list(map(int,input().split()))
    a=Counter(a)
    d=[]
    now=[False]*24
    now[0]=True
    if a[0]>0 or a[12]>1:
        print(0)
        return 0
    if a[12]==1:
        now[12]=True
    for i in range(1,12):
        if a[i]>2:
            print(0)
            return 0
        elif a[i]==2:
            now[i]=True
            now[24-i]=True
        elif a[i]==1:
            d.append(i)
    print(dfs(d,now))
main()

Submission status: Accepted

Solution code:
#time gap
n=int(input())
lists=list(map(int,input().split()))
if n>24:
    print(0)
else:
    timelist=[0 for i in range(13)]
    #timelist[i]ってところでi=0,1,2,....12に代入する
    for i in lists:
        timelist[i]+=1
    flag=True
    for j in range(13):
        if j==0:
            if timelist[j]>0:
                flag=False
        else:
            if timelist[j]>2:
                flag=False
    if flag:
        uselist1=[]
        uselist2_=[]
        for i in range(13):
            if timelist[i]==1:
                uselist1.append((i))
            if timelist[i]==2:
                uselist2_.append((i))
            else:
                continue
        uselist2=[]
        for j in uselist2_:
            uselist2.append(j)
            uselist2.append(24-j)
        uselist2=list(set(uselist2))
        lens=len(uselist1)
        minilist=[]
        for i in range(2**lens):
            sublist=[0]
            mini=100
            sublist=sublist+uselist2
            j=format(i,"0%ib"%lens)
            for k in range(lens):
                if j[k]=="0":
                    sublist.append(uselist1[k])
                else:
                    sublist.append(24-uselist1[k])
            sublists=sorted(sublist)
            lenss=len(sublists)
            for k in range(1,lenss):
                mini=min(mini,sublists[k]-sublists[k-1])
            mini=min(mini,24-sublists[-1],sublists[-1])
            minilist.append(mini)  
        else:
            print(max(minilist))
    if not flag:
        print(0)

Submission status: Accepted

Solution code:
#time gap
n=int(input())
lists=list(map(int,input().split()))
if n>24:
    print(0)
else:
    timelist=[0 for i in range(13)]
    #timelist[i]ってところでi=0,1,2,....12に代入する
    for i in lists:
        timelist[i]+=1
    flag=True
    for j in range(13):
        if j==0:
            if timelist[j]>0:
                flag=False
        else:
            if timelist[j]>2:
                flag=False
    if flag:
        uselist1=[]
        uselist2_=[]
        for i in range(13):
            if timelist[i]==1:
                uselist1.append((i))
            if timelist[i]==2:
                uselist2_.append((i))
            else:
                continue
        uselist2=[]
        for j in uselist2_:
            uselist2.append(j)
            uselist2.append(24-j)
        uselist2=list(set(uselist2))
        lens=len(uselist1)
        minilist=[]
        for i in range(2**lens):
            sublist=[0]
            mini=100
            sublist=sublist+uselist2
            j=format(i,"0%ib"%lens)
            for k in range(lens):
                if j[k]=="0":
                    sublist.append(uselist1[k])
                else:
                    sublist.append(24-uselist1[k])
            sublists=sorted(sublist)
            lenss=len(sublists)
            for k in range(1,lenss):
                mini=min(mini,sublists[k]-sublists[k-1])
            mini=min(mini,24-sublists[-1],sublists[-1])
            minilist.append(mini)  
        else:
            print(max(minilist))
    if not flag:
        print(0)

Submission status: Accepted

Solution code:
import sys
import heapq
input = sys.stdin.readline
N = int(input())
d = list(map(int, input().split()))
rres = 0
if N == 1:
  print(d[0])
  exit(0)
for t in range(13):
  res = True
  q = sorted(d)
  heapq.heapify(q)
  l = [0, 24]
  i = 0
  c = 0
  while len(q) and c < 2 * N ** 2:
    p = heapq.heappop(q)
    if abs(p - l[i]) < t:
      heapq.heappush(q, 24 - p)
    else:
      i += 1
      l.append(p)
      l.sort()
    c += 1
  if c >= 2 * N ** 2:
    res = False
  l.sort()
  for i in range(1, len(l)):
    if abs(l[i - 1] - l[i]) < t:
      res = False
      break
  #print(l)
  if res:
    rres = t
  else:
    break
 #print(res, t)
print(rres)

Submission status: Accepted

Solution code:
from collections import Counter
inf = float('inf')

N = int(input())
D = list(map(int,input().split())) + [0]
N += 1

def cal(a):
    n = len(a)
    res = inf
    for i in range(n):
        for j in range(i+1, n):
            d = abs(a[i] - a[j])
            d = min(d, 24 - d)
            res = min(res, d)
    return res

c = Counter(D)
ans = -inf
for S in range(1 << 13):
    D = []
    if c[0] > 1 or c[12] > 1:
        print(0)
        exit()
    if c[0] == 1:
        D.append(0)
    if c[12] == 1:
        D.append(12)
    for j in range(1, 12):
        if c[j] == 0:
            continue
        elif c[j] == 1:
            if (S >> j) & 1:
                D.append(j)
            else:
                D.append(24-j)
        elif c[j] == 2:
            D.append(j)
            D.append(24-j)
        else:
            print(0)
            exit()
    ans = max(ans, cal(D))

print(ans)

Submission status: Accepted

Solution code:
inf = float('inf')

N = int(input())
D = sorted(map(int,input().split()), reverse=True) + [0]
N += 1

for i in range(N):
    if i % 2 == 0:
        D[i] = 24 - D[i]

ans = inf
for i in range(N):
    for j in range(i+1, N):
        d = abs(D[i] - D[j])
        d = min(d, 24 - d)
        ans = min(ans, d)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
if n>=24:
    print(0)
    exit()
if n>=22:
    t=[]
    d.append(0)
    d.sort()
    f=1
    for i in range(n):
        if d[i]==d[i+1]:
            f+=1
        else:
            if f>2:
                print(0)
                exit()
            f=1
    print(1)
    exit()

op=2**n-1
ans=0
for i in range(op):
    t=[0]
    for j in range(n):
        if i&(2**j)>0:
            t.append(d[j])
        else:
            t.append(24-d[j])
    a=10000
    for p in range(1,1+n):
        for q in range(p):
            b=abs(t[p]-t[q])
            a=min(a,min(b,24-b))
    ans=max(ans,a)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = list(map(int, input().split()))
d.sort()
for i in range(1, n, 2): d[i] = 24 - d[i]
d.sort()
d = [0] + d + [24]
ans = 24
for i in range(1, n+2):
    ans = min(ans, d[i] - d[i-1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = [0] + [24] + list(map(int, input().split()))
D.sort()
for i in range(1, N+1, 2):
    D[i] = 24 - D[i]
D.sort()
answer = 24
for i in range(N+1):
    answer = min(answer, abs(D[i+1]-D[i]))
print(answer)

Submission status: Accepted

Solution code:
N = int(input()) + 1
D = list(map(int, input().split())) + [0] + [24]
D.sort()
for i in range(1, N, 2):
    D[i] = 24 - D[i]
D.sort()
ans = 24
for i in range(N):
    ans = min(ans, abs(D[i+1]-D[i]))
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 23:
    print(0)
    exit()
if D.count(0) >= 2:
    print(0)
    exit()
for i in range(1, 13):
    if D.count(i) >= 3:
        print(0)
        exit()
for i in range(13):
    if (D.count(i) + D.count(i+1)) == 4:
        print(1)
        exit()

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 23:
    print(0)
    exit()
for i in range(13):
    if D.count(i) >= 3:
        print(0)
        exit()
    elif D.count(i) == 2 and D.count(i+1) == 2:
        print(1)
        exit()

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Accepted

Solution code:
import sys
import collections

N = int(input())
D = list(map(int, input().split()))
D.append(0)
D.sort()
count = collections.Counter(D)

E = []
F = []
for k, v in count.items():
  if v >= 3:
    print(0)
    sys.exit(0)
  if v == 2:
    F.append(24 - k)
    F.append(k)
  if v == 1:
    E.append(k)
E.sort()
for i in range(len(E)):
  if i % 2 == 0:
    F.append(E[i])
  else:
    F.append(24 - E[i])
F.append(24)
F.sort()
ans = 24
for i in range(len(F) - 1):
    ans = min(ans, F[i+1] - F[i])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = [0]+[int(x) for x in input().split()]
D.sort()
li = []
for ind, d in enumerate(D):
    if(ind%2==0):
        li.append(d)
    else:
        li.append(24-d)
li.sort()
li.append(24)
ans = 24
for ind, d in enumerate(li[:-1]):
    ans = min(li[ind+1]-d, ans)
print(ans if ans!=24 else 0)

Submission status: Accepted

Solution code:
import sys
import collections
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")

n=int(input())
d=list(map(int,input().split()))

c=collections.Counter(d)
if c.most_common()[0][1]>2:
    print(0)
    quit()
if 0 in c:
    if c[0]==1:
        print(0)
        quit()

li=list(map(int,c.keys()))
li.append(0)
ans=0
# print(li)
# print(len(li))
for i in range(2**len(li)):
    hour=[]
    for j in range(len(li)):
        if c[li[j]]==2:
            hour.append(li[j])
            hour.append(24-li[j])
            continue
        if i&(1<<j):
            hour.append(li[j])
        else:
            hour.append(24-li[j])
    hour.sort()
    # print(hour)
    tmp=24+hour[0]-hour[-1]
    for j in range(len(hour)-1):
        tmp=min(tmp,hour[j+1]-hour[j])
    # print(tmp)
    ans=max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())+1
D = list(map(int,input().split()))+[0]+[24]
D.sort()
for i in range(1,N,2):
    D[i] = 24-D[i]
D.sort()
ans = 24
for i in range(N):
    ans = min(ans,abs(D[i+1]-D[i]))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter 
import itertools

N = int(input())
D = list(map(int, input().split()))
D.append(12)
M = Counter(D)
if max(M.values()) > 2:
    print(0)
elif 0 in D:
    print(0)
elif N > 11:
    print(1)
else:
    D.remove(12)
    D = [[x, 24-x] for x in D]
    generator = list(itertools.product([0, 1], repeat=N))
    D_gen = [[D[i][g[i]] for i in range(N)] for g in generator]
    ans = 0
    for d in D_gen:
        if len(set(d)) < N:
            pass
        else:
            tmp = [0] + sorted(d) + [24]
            s = 13
            for i in range(N+1):
                s = min(s, tmp[i+1]-tmp[i])
            ans = max(ans, s)
    print(ans)

Submission status: Accepted

Solution code:
# cf17-final
def main():
    N, *D = map(int, open(0).read().split())
    D.sort()
    D = [0] + [d if i & 1 else 24 - d for i, d in enumerate(D, 1)]
    D.sort()
    ans = min(j - i for i, j in zip(D, D[1:]))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# cf17-final
def main():
    N, *D = map(int, open(0).read().split())
    D.append(0)
    D.sort()
    for i in range(1, N + 1):
        if i & 1 == 0:
            D[i] = 24 - D[i]
    D.sort()
    ans = min(j - i for i, j in zip(D, D[1:]))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
import copy

N = int(input())
D = list(map(int, input().split()))
if N == 1:
    print(min(D[0], 24-D[0]))
    exit()

D.append(0)
D.append(24) # 24-0のパターン

c = Counter(D)

times = []
p = []
for num, cnt in c.items():
    if (num == 0 or num == 12) and cnt >= 2:
        # D==24-D になるかつ時差０の組がいるので時差の最小は０で確定
        print(0)
        exit()
    elif cnt >= 3:
        # 時差０の組み合わせがある
        print(0)
        exit()
    elif cnt == 2:
        # Dと24-Dに一人ずつ
        times.append(num)
        times.append(24-num)
    elif cnt == 1:
        # D or 24-D のどちらか一方に一人だけいる状態なので別枠で確保
        p.append((num, 24-num))

num_b = len(p)
ans = 0
# D or 24-Dの可能性があるものについて全パターン試すために、2進表記を利用
for b in range(2**num_b):
    b = bin(b)[2:].zfill(num_b)
    time = copy.deepcopy(times)
    for i in range(num_b):
        if b[i] == "0":
            time.append(p[i][0])
        else:
            time.append(p[i][1])
    tmp = 24
    for i in range(len(time)):
        for j in range(i+1, len(time)):
            tmp = min(tmp, abs(time[i] - time[j]))

    ans = max(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import copy

N = int(input())
D = list(map(int, input().split()))
if N == 1:
    print(min(D[0], 24-D[0]))
    exit()

D.append(0)
D.append(24)

c = Counter(D)

times = []
p = []
for num, cnt in c.items():
    if (num == 0 or num == 12) and cnt >= 2:
        # D==24-D になるかつ時差０の組がいるので時差の最小は０で確定
        print(0)
        exit()
    elif cnt >= 3:
        # 時差０の組み合わせがある
        print(0)
        exit()
    elif cnt == 2:
        # Dと24-Dに一人ずつ
        times.append(num)
        times.append(24-num)
    elif cnt == 1:
        # D or 24-D のどちらか一方に一人だけいる状態なので別枠で確保
        p.append((num, 24-num))

num_b = len(p)
ans = 0
# D or 24-Dの可能性があるものについて全パターン試すために、2進表記を利用
for b in range(2**num_b):
    b = bin(b)[2:].zfill(num_b)
    time = copy.deepcopy(times)
    for i in range(num_b):
        if b[i] == "0":
            time.append(p[i][0])
        else:
            time.append(p[i][1])
    tmp = 24
    for i in range(len(time)):
        for j in range(i+1, len(time)):
            tmp = min(tmp, abs(time[i] - time[j]))

    ans = max(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int, input().split()))
D.append(0)
D.sort()
min_v = D[1]
for n in range(1,N+1,2):
  D[n] = 24 - D[n]
D.sort()
for i in range(1, N+1):
  min_v = min(min_v, D[i] - D[i - 1])
print(min_v)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int, input().split()))
D.append(0)
D.sort()
min_v = D[1]
for i in range(1,N+1):
  D[i]=D[i] if i&1 else 24-D[i]
D.sort()
for i in range(1, N+1):
  min_v = min(min_v, D[i] - D[i - 1])
print(min_v)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
now_time = [0, 24]
D = list(map(int, input().split()))
c = Counter(D)
candidate = []
if N == 1:
    print(min(D[0], 24-D[0]))
    exit()

for num, amount in c.items():
    if num == 0:
        print(0)
        exit()
    if amount > 2:
        print(0)
        exit()
    if amount == 2:
        # 確定した時間
        now_time.append(num)
        now_time.append(24-num)
    if amount == 1:
        candidate.append((num, 24-num))  # 選択の余地がある時間
ans = 0
# 確定した時間に選択の余地がある組を追加していく
for mask in range(1 << len(candidate)):
    list_0_1 = []
    time_list = now_time.copy()  # 確定している時間
    for i in range(len(candidate)):
        list_0_1.append(int((mask >> i) & 1))
    # print(list_0_1)
    for i, num in enumerate(list_0_1):
        time_list.append(candidate[i][num])
    # exit()
    tmp = 100
    for i in range(len(time_list)):
        for j in range(i+1, len(time_list)):
            tmp = min(tmp, abs(time_list[i] - time_list[j]))
    ans = max(tmp, ans)


print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    D = list(map(int, input().split()))

    de = []
    count = [0] * 12
    for d in D:
        if d == 0 or d == 12:
            de.append(d)
        else:
            count[d] += 1
    
    ind = []
    for i in range(12):
        if count[i] == 1:
            ind.append(i)
        elif count[i] == 2:
            de.append(i)
            de.append(24-i)
        elif count[i] >= 3:
            print(0)
            return
    
    for i in range(len(ind)):
        if i&1:
            de.append(ind[i])
        else:
            de.append(24-ind[i])

    de = sorted(de)

    # print(count)
    # print(de)
    # print(ind)

    ans = de[0] - 0
    for i in range(1, N):
        ans = min(ans, de[i] - de[i-1])
    ans = min(ans, 24-de[-1])
    print(ans)
    



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().split()))

cnt_memo = {}
for num in s:
    if num in cnt_memo:
        cnt_memo[num] += 1
    else:
        cnt_memo[num] = 1

kakutei = [0]
mikakutei = []
for i in cnt_memo:
    if i == 0:
        print(0)
        exit()
    elif i == 12:
        if cnt_memo[i] == 1:
            kakutei.append(12)
        else:
            print(0)
            exit()
    else:
        if cnt_memo[i] == 1:
            mikakutei.append(i)
        elif cnt_memo[i] == 2:
            kakutei.append(i)
            kakutei.append(24 - i)
        else:
            print(0)
            exit()
tmp_ans1 = 25
for num1 in kakutei:
    for num2 in kakutei:
        if num1 != num2:
            tmp_ans1 =  min(tmp_ans1, min(abs(num1 - num2), 24 -abs(num1 - num2)))

len_mikakutei = len(mikakutei)
mikakutei_pattern = 2**len_mikakutei
ans = 0
for i in range(mikakutei_pattern):
    tmp = []
    num = i
    cnt = 0
    for _ in range(len_mikakutei):
        if num % 2 == 1:
            tmp.append(mikakutei[cnt])
        else:
            tmp.append(24 - mikakutei[cnt])
        num //= 2
        cnt += 1
    tmp_ans = 25
    for num1 in tmp:
        for num2 in kakutei:
            tmp_ans = min(tmp_ans, min(abs(num1 - num2), 24 -abs(num1 - num2)))
    for num1 in tmp:
        for num2 in tmp:
            if num1 != num2:
                tmp_ans = min(tmp_ans, min(abs(num1 - num2), 24 -abs(num1 - num2)))
    tmp_ans = min(tmp_ans, tmp_ans1)
    ans = max(ans, tmp_ans)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int, input().split()))


D.sort()
ans = [0]
ans = [D[i]
       for i in range(N) if i % 2 == 0] + [24 - D[i] for i in reversed(range(N)) if i % 2 == 1]

ans = [0] + ans + [0]

ans = [abs(ans[i+1] - ans[i]) for i in range(N+1)]

print(min(ans))

Submission status: Accepted

Solution code:
import collections
N = int(input())
D = list(map(int, input().split()))
D.append(0)
ans = 0  
    
x = collections.Counter(D)
y = x.most_common(1)
#print(x,y,y[0][1])

if y[0][1] >= 3:
  print(ans)
  quit()
         
if y[0][1] == 2:
  if (y[0][0] == 0) or (y[0][0] == 12):
    print(ans)
    quit()
 
day = [0] * 24 
ilist = [0] * 13
for i in range(13):
  if (i == 0) or (i == 12):
    if i in D:
      day[i] = 1
      ilist[i] = 1
  else:# i = 1 ~ 11
    if D.count(i) == 2:
      day[i] = 1
      day[24 - i] = 1
      ilist[i] = 2
    elif D.count(i) == 1:
      #print(i)
      p = 0
      l = 1
      while p == 0:
        p = ilist[i - l]
        if p == 0:
          l += 1
        if (p == 0) and (l == i):
          p = 3
      #print(i,p,ilist)
      if p == 3:
        day[i] = 1
      if p == 2:
        day[i] = 1
      if p == 1:
        #print(i, l, day)
        if day[i - l] == 1:
          day[24-i] = 1
        else:
          day[i] = 1
      ilist[i] = 1
      

day2 = day * 2
ans = 25
ans2 = 1
xp = 1
#print(day2)
for i in range(len(day2)):
  if day2[i] == 0:
    ans2 += 1
    xp = 0
  elif (day2[i] == 1) and (xp == 0):
    #print(ans, ans2)
    ans = min(ans, ans2)
    ans2 = 1

if ans > 24:
  ans = ans - 24
print(ans)

Submission status: Accepted

Solution code:

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions

import sys
def input():
	return sys.stdin.readline().strip()

N = int(input())
D = list(map(int, input().split()))

times = [0]*13
times[0] = 1
for i in range(N):
	times[D[i]] += 1
	if times[D[i]] == 3:
		print(0)
		exit()
if times[0] > 1 or times[12] > 1:
	print(0)
	exit()

base_time = [0]
add_time = []
for i in range(1, 12):
	if times[i] == 2:
		bisect.insort(base_time, -i)
		bisect.insort(base_time, i)
	elif times[i] == 1:
		add_time.append(i)
if times[12] == 1:
	bisect.insort(base_time, 12)

def min_diff(A):
	min_times = 50
	for i in range(len(A) - 1):
		min_times = min(min_times, A[i + 1] - A[i])
	min_times = min(min_times, A[0] + 24 - A[-1])
	return min_times

if len(add_time) == 0:
	print(min_diff(base_time))
	exit()

ans = 0
for i in range(2**len(add_time)):
	base_time2 = copy.deepcopy(base_time)
	for bit in range(len(add_time)):
		mask = ((2**bit)&i)//(2**bit)
		bisect.insort(base_time2, add_time[bit]*(1 - 2*mask))
	ans = max(ans, min_diff(base_time2))
	#print(base_time2)

print(ans)

Submission status: Accepted

Solution code:
def solve(n, d):
    if n >= 24:
        return 0

    cnt = {i: 0 for i in range(12 + 1)}
    cnt[0] = 1
    for dd in d:
        cnt[dd] += 1

    # 同じ時刻の都市が存在する場合、
    # 時刻の差の最小値は、0になる
    # 鳩ノ巣
    if cnt[0] > 1:
        return 0
    elif cnt[12] > 1:
        return 0
    else:
        for i in range(1, 12):
            if cnt[i] > 2:
                return 0

    bit_cnt = 0
    for i in range(1, 12):
        if cnt[i] == 1:
            bit_cnt += 1

    res = -1
    for mask in range(1 << bit_cnt):
        t = [False] * 24
        t[0] = True

        cur = 0
        for i in range(1, 12):
            if cnt[i] == 1:
                if (mask >> cur) & 1 == 1:
                    t[i] = True
                else:
                    t[24 - i] = True
                cur += 1
            elif cnt[i] == 2:
                t[i] = True
                t[24 - i] = True

        if cnt[12] == 1:
            t[12] = True

        # print(*map(lambda x: 1 if x else '-', t))

        dist = 24
        frm = 0
        for i in range(1, 48 + 1):
            if t[i % 24]:
                dist = min(dist, (i - frm) % 24)
                frm = i

        res = max(res, dist)

    return res


if __name__ == '__main__':
    n = int(input())
    d = tuple(map(int, input().split()))

    res = solve(n, d)
    print(res)