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:
n, *d = map(int, open(0).read().split())
d.sort()

a = [24 - b if i & 1 else b for i, b in enumerate(d)]
a.append(0)


ans = 24
print(min([abs(a[i] - a[j]) for i in range(n + 1)
           for j in range(i + 1, n + 1)]))

Submission status: Accepted

Solution code:
import sys,math,collections,itertools,copy
input = sys.stdin.readline

N=int(input())
D=list(map(int,input().split()))
time_list = [0]*13
time_list[0] = 1 #-高橋君-#
#-時差の精査 同じ時差が3つあれば0-#
for d in D:
    time_list[d] += 1
    if time_list[d]>=3:
        print(0)
        exit()

#- 時差が2種類あるものから先に置いていく -#
time24list=[]
singlelist=[]
for jisa,cnt in enumerate(time_list):
    if cnt == 0:
        continue
    elif cnt==2:
        time24list.append(jisa)
        time24list.append(24-jisa)
    elif jisa ==0 or jisa == 12:
        time24list.append(jisa)
    else:
        singlelist.append(jisa)

#- singleリストを全探索 -#
lst = len(singlelist)
bit = 2**lst
ans = 0
for bi in range(bit):
    tmp = copy.deepcopy(time24list)
    min_jisa = 12 
    for j in range(lst):
        if bi>>j & 1:
            tmp.append(singlelist[j])
        else:
            tmp.append(24-singlelist[j])
    tmp.sort()
    tmp.append(24)
    for i in range(len(tmp)-1):
        dtime = tmp[i+1]-tmp[i]
        min_jisa = min(dtime,min_jisa)
    ans = max(ans,min_jisa)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    n = ni()
    a = na()
    a.sort()

    tmp = []
    for i, ai in enumerate(a):
        if i % 2:
            tmp.append(ai)
        else:
            tmp.append(24 - ai)
    tmp.append(0)
    tmp.append(24)
    tmp.sort()
    ans = INF
    for i in range(len(tmp) - 1):
        ans = min(ans, tmp[i + 1] - tmp[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import combinations, product
from collections import Counter
def calc(a):
    ret = 10 ** 20
    for comb in combinations(a, 2):
        tmp = abs(comb[0] - comb[1])
        ret = min(ret, min(tmp, 24-tmp))
    return ret

def main():
    N = int(input())
    D = list(map(int, input().split()))
    # 0時間がある場合 高橋君の必ずかぶるので 0
    if D.count(0):
        return 0
    cnt = Counter(D)
    # 3つ以上のかぶりがある場合 0
    if max(cnt.values()) >= 3: return 0
    # 12時間が複数ある場合 0
    if cnt[12] >= 2: return 0
    d = []
    d2 = [0]
    single_cnt = 0
    for i, c in cnt.items():
        if i == 12:
            d2.append(12)
        elif c == 1:
            d.append((i, 24-i))
            single_cnt += 1
        elif c == 2:
            d2.append(i)
            d2.append(24-i)
    ans = 0
    for prod in product(range(2), repeat=single_cnt):
        tmp = []
        for i, p in enumerate(prod):
            tmp.append(d[i][p])
        ans = max(ans, calc(tmp + d2))
    return ans
            
print(main())

Submission status: Accepted

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

if N >= 24:
    print(0)

elif N == 23:
    d = [0 for _ in range(13)]
    d[0] = 1
    for i in range(N):
        d[D[i]] += 1
    if d[0] > 1 or d[12] > 1:
        print(0)
    else:
        for i in range(1, 12):
            if d[i] > 2:
                print(0)
                break
        else:
            print(1)

else:
    res = 0
    for bit in range(2**N):
        d = [0 for _ in range(24)]
        d[0] = 1
        for i in range(N):
            if (bit >> i) & 1:
                d[D[i]] += 1
            else:
                d[(24 - D[i]) % 24] += 1
        s = 25
        tmp = 0
        if d[0] > 1:
            s = 0
        else:
            for i in range(1, 24):
                if d[i] > 1:
                    s = 0
                    break
                elif d[i] == 1:
                    s = min(s, i - tmp)
                    tmp = i
            s = min(s, (24 - tmp) % 24)
        res = max(res, s)
    print(res)

Submission status: Accepted

Solution code:
n = int(input())
D = map(int, input().split())
cnt = [0]*13
for d in D:
    cnt[d] += 1

if any(2 < x for x in cnt[1:12]) or cnt[0] != 0 or cnt[12] == 2:
    print(0)
else:
    world = [0]*24
    world[0] = 1
    flag = 1
    for i, c in enumerate(cnt[1:], 1):
        if c == 0:
            continue
        if c == 2:
            world[i] = 1
            world[-i] = 1
        elif c == 1:
            if flag:
                world[i] = 1
            else:
                world[-i] = 1
            flag ^= 1
    print(
        min(map(lambda x: len(x)+1, (''.join(map(str, world[1:])).split('1')))))

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    N = int(input())
    D = list(map(int, input().split()))
    d = Counter(D)
    # 前処理
    d[0] += 1
    for i in range(13):
        if (i == 0 or i == 12) and d[i] == 2:
            print(0)
            exit()
        elif d[i] >= 3:
            print(0)
            exit()
    ans = 0
    for i in range(2**11):
        time = [0, 24]
        if d[12] == 1:
            time.append(12)
        for j in range(1,12):
            if d[j] == 1:
                if i >> j & 1:
                    time.append(24-j)
                else:
                    time.append(j)
            elif d[j] == 2:
                time.append(24-j)
                time.append(j)
        time.sort()
        ret = 25
        for i in range(1,len(time)):
            ret = min(ret, time[i]-time[i-1])
        ans = max(ans, ret)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
dcnt=[0 for i in range(13)]
for i in d:
  dcnt[i]+=1

zf=False
for i in range(1,12):
  if dcnt[i]>=3:
    zf=True
if dcnt[0]:
  zf=True
if dcnt[12]>=2:
  zf=True

if zf:
  print(0)
else:
  ison=[0 for i in range(24)]
  oned=[]
  ison[12]=1
  ison[0]=dcnt[12]
  for i in range(1,12):
    if dcnt[i]==1:
      oned.append(i)
    elif dcnt[i]==2:
      ison[12-i]=1
      ison[12+i]=1
  maxd=0
  for i in range(2**len(oned)):
    for j in range(len(oned)):
      if i&(2**j):
        ison[12+oned[j]]=1
        ison[12-oned[j]]=0
      else:
        ison[12-oned[j]]=1
        ison[12+oned[j]]=0
        
    mind=24
    p=100
    s=23
    while True:
      if ison[s]:
        mind=min(mind,p-s)
        p=s
        if s<0:
          break
      s-=1
    maxd=max(maxd,mind)
  print(maxd)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from copy import copy
import itertools

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    D = in_nl() + [0]

    cnt = Counter(D)

    det = []
    non_det = []

    # 0 and 12
    for i in (0, 12):
        if cnt[i] >= 2:
            print(0)
            exit()
        elif cnt[i] == 1:
            det.append(i)

    # 1 - 11
    for i in range(1, 12):
        if cnt[i] >= 3:
            print(0)
            exit()
        elif cnt[i] >= 2:
            det.append(i)
            det.append(24 - i)
        elif cnt[i] == 1:
            non_det.append(i)

    # print(det)
    # print(non_det)

    non_det_len = len(non_det)
    comb = itertools.product([0, 1], repeat=non_det_len)

    ans = 0

    def solve(city):

        comb = itertools.combinations(city, 2)

        ret = 12
        for c in comb:
            t1, t2 = c[0], c[1]
            diff = abs(t2 - t1)
            ret = min(ret, min(diff, 24 - diff))

        return ret

    for com in comb:

        city = copy(det)
        for i in range(non_det_len):
            if com[i] == 0:
                city.append(non_det[i])
            else:
                city.append(24 - non_det[i])

        ans = max(ans, solve(city))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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

times = [0] * 24
times[0] = 1
for d in D:
    times[d] += 1

if max(times) > 2 or times[0] == 2 or times[12] == 2:
    print(0)
else:
    ans = 0
    # 1 - 11時間の時差の場合、Diと24 - Diの両方を試す
    for i in range(2**11):
        positions = [0]
        for j in range(1, 12):
            if times[j] == 2:
                positions.append(j)
                positions.append(24 - j)
            elif times[j] == 1:
                if (i >> j) & 1:
                    positions.append(24 - j)
                else:
                    positions.append(j)
        if times[12] == 1:
            positions.append(12)
        positions.sort()
        # print(positions)
        lp = len(positions)
        d = float("inf")
        for p in range(lp):
            # 0をまたぐ場合がある
            if p == lp -1:
                diff = 24 - positions[p] + positions[0]
            else:
                diff = positions[p+1] - positions[p]
            d = min(d, diff if diff != 24 else 0)
        ans = max(ans, d)

    print(ans)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = getN()
D = getList()
D = Counter(D)

l_1 = []
l_2 = []
for i in D.items():
    if i[0] == 0:
        print(0)
        exit()
    if i[1] >= 3:
        print(0)
        exit()
    if i[1] == 2:
        l_2.append(i[0])
        l_2.append(24 - i[0])
    elif i[1] == 1:
        l_1.append(i[0])

def judge(array):
    res = min(24 - array[-1], array[-1])
    for i in range(len(array) - 1):
        res = min(res, array[i + 1] - array[i])
    return res

ans = 0
# Dの要素の個数が１個の時のみ分岐する（最大2 ** 12)
N_1 = len(l_1)
for bit in range(1 << N_1):
    opt = copy.deepcopy(l_2)
    opt.append(0)
    for i in range(N_1):
        if bit & (1 << i):
            opt.append(l_1[i])
        else:
            opt.append(24 - l_1[i])
    opt.sort()
    ans = max(ans, judge(opt))
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()
dic = DD(int)
dic[0] += 1
for a in A:
  dic[a] += 1
  
if dic[0] >= 2:
  print(0)
  exit()
if dic[12] >= 2:
  print(0)
  exit()
  
kaku = [0,24]
if dic[12]:
  kaku.append(12)
pending = []
for i in range(1,12):
  if dic[i] >= 3:
    print(0)
    exit()
  elif dic[i] == 2:
    kaku.append(i)
    kaku.append(24-i)
  elif dic[i] == 1:
    pending.append(i)
data = [0]
n = len(pending)
for k in range(1<<n):
  temp = set(kaku)
  for i in range(n):
    if (k>>i)&1:
      temp.add(pending[i])
    else:
      temp.add(24-pending[i])
  temp = list(temp)
  temp.sort()
  nn = len(temp)
  data.append(min(temp[i+1]-temp[i] for i in range(nn-1)))

print(max(data)%24)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import Counter

def main():
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)
    D = Counter(D)
    for i in range(13):
        if (i==0 or i==12) and D[i]==2:
            print(0)
            exit()
        elif D[i]>=3:
            print(0)
            exit()
    ret = 0
    for i in range(2**11):
        l = [0,24]
        if D[12]==1:
            l.append(12)
        for j in range(1,12):
            if D[j]==1:
                if i>>j & 1:
                    l.append(24 - j)
                else:
                    l.append(j)
            elif D[j]==2:
                l.append(24 - j)
                l.append(j)
        l.sort()
        mi = 24
        for i in range(1,len(l)):
            mi = min(mi, l[i]-l[i-1])
#        print(l,mi)
        ret = max(ret, mi)
    print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
'''
研究室PCでの解答
'''
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    dn = [0]*13
    dn[0] = 1
    for i in ipt().split():
        dn[int(i)] += 1
    st = set()
    if dn[12] > 1:
        print(0)
        exit()
    elif dn[12] == 1:
        st.add(12)
    if dn[0] > 1:
        print(0)
        exit()
    elif dn[0] == 1:
        st.add(0)
    p = 1

    for i,di in enumerate(dn[1:12:]):
        if di > 2:
            print(0)
            exit()
        elif di == 2:
            st.add(i+1)
            st.add(24-i-1)
        elif di == 1:
            if p:
                st.add(i+1)
                p = 0
            else:
                st.add(24-i-1)
                p = 1

    ma = 25
    st = sorted(st)
#    print(st)
    for i,si in enumerate(st):
        if ma > abs(si-st[i-1]):
            ma = abs(si-st[i-1])
    print(ma)

    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

def dist(x,y):
    return min(abs(x-y), abs(24-abs(x-y)))

n = readint()
d = readints()

d.sort()

a = [0]*24
a[0]=1
for i in range(n):
    if d[i]==0:
        a[0]+=1
        continue
    if d[i]==12:
        a[12]+=1
        continue
    x = d[i]
    y = 24-d[i]
    ansx = 100
    ansy = 100
    for j in range(24):
        if a[j]:
            ansx = min(ansx, dist(x,j))
            ansy = min(ansy,dist(y,j))
    if ansx >= ansy:
        a[x]+=1
    else:
        a[y]+=1

ans = 100
for x in a:
    if x>1:
        ans = 0

if ans:
    b = []
    for i,x in enumerate(a):
        if x:
            b.append(i)
    for i in range(len(b)):
        ans = min(ans,dist(b[i],b[i-1]))
print(ans)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=int(input())
d=list(map(int,input().split()))
d.sort()
res=24
if n==1 or 0 in d:
  print(d[0])
  exit()
for i in range(0,n-2):
    res=min(res,d[i+2]-d[i])
for i in range(n-1):
    res=min(res,d[i]+d[i+1],24-d[i]-d[i+1])
ans=min(d[0],res)
print(ans)

Submission status: Accepted

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

if N == 1:
    print(D[0])
    exit()

time = [0]
for i in range(N):
    max_24 = 24
    max_0 = 24
    for j in range(len(time)):
        max_0 = min(abs(time[j] - D[i]), max_0)
        max_24 = min(abs(time[j] - (24-D[i])), max_24)
    if max_0 >= max_24:
        time.append(D[i])
    else:
        time.append(24 - D[i])

time.append(24)
ans = 12
for i in range(len(time)):
    for j in range(i+1, len(time)):
        ans = min(ans, abs(time[i] - time[j]))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = list(map(int, input().split()))
li = [0]*13
for i in d:
    li[i] += 1
if li[0]>0 or li[12]>1:
    print(0)
    exit(0)
l1 = []
l2 = []
for i in range(1, 12):
    if li[i]>2:
        print(0)
        exit(0)
    if li[i]==1:
        l1.append(i)
    elif li[i]==2:
        l2.extend([i, 24-i])
if li[12]:
    l2.append(12)
ans = 0
for i in range(2**len(l1)):
    tl = l2[:]
    for j in range(len(l1)):
        if i>>j&1:
            tl.append(l1[j])
        else:
            tl.append(24-l1[j])
    tl.sort()
    tmp = 24-tl[-1]
    pre = 0
    for j in tl:
        tmp = min(tmp, j-pre)
        pre = j
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

N = int(readline())

D = list(map(int,readline().split()))
cnt = [0]*13
for d in D:
    cnt[min(24-d,d)] += 1
cnt[0] += 1
for i in range(1,12):
    if cnt[i] >= 3:
        print(0)
        exit()
if cnt[0] >= 2 or cnt[12] >= 2:
    print(0)
    exit()

where = [0]*24
where[0] = 1
b = 0
if cnt[12]:
    where[12] = 1
for i in range(1,12):
    c = cnt[i]
    if c == 0:
        continue
    if c == 2:
        where[i] = 1
        where[24-i] = 1
        b = 0
    else:
        if b:
            where[i] = 1
            b = 0
        else:
            where[24-i] = 1
            b = 1

ans = INF
a = 1
for i in range(1,24):
    if where[i]:
        ans = min(ans,a)
        a = 1
    else:
        a += 1
ans = min(ans,a)
print(ans)

Submission status: Accepted

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

ans = 12
while ans > 1:
  Fix = [0]
  for i in range(N):
    MIN = []
    for dotti in range(2):
      if dotti == 0:
        nxt = D[i]
      else:
        nxt = 24-D[i]
      now = 100
      for t in Fix:
        now = min(now,abs(nxt-t),24-abs(nxt-t))
      MIN.append(now)
    if MIN[0] <= MIN[1]:
      Fix.append(24-D[i])
    else:
      Fix.append(D[i])
  #print(Fix)
  break
ans = 100
for i in range(N+1):
  for j in range(i+1,N+1):
    #print(Fix[i],Fix[j])
    ans = min(ans,abs(Fix[i]-Fix[j]),24-abs(Fix[i]-Fix[j]))
    #print(ans)
print(ans)

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    D=LI()
    #同じ時差の都市に対して3人以上いる場合は0
    if 0 in D:
        print(0)
        exit()
    if D.count(12)>=2:
        print(0)
        exit()
    
    temp=[0]*24
    temp[0]=1
    if 12 in D:
        temp[12]=1
    
    d=[]
    cnt=0
    for i in range(1,12):
        if D.count(i)==1:
            d.append(i)
            cnt+=1
        elif D.count(i)==2:
            temp[i]=1
            temp[24-i]=1
        elif D.count(i)>=3:
            print(0)
            exit()
            
            
    ans=[]
    
    for i in range(1<<cnt):
        temp2=[0]*24
        
        #コピー
        for k in range(24):
            temp2[k]=temp[k]
        
        for j in range(cnt):
            if i>>j & 1:
                temp2[d[j]]=1
            else:
                temp2[24-d[j]]=1
                
        aaa=24
        for ii in range(24):
            for jj in range(ii+1,24):
                if temp2[ii]==1 and temp2[jj]==1:
                    diff=min(jj-ii,24-(jj-ii))
                    if diff<aaa:
                        aaa=diff
        ans.append(aaa)
    print(max(ans))
            
    
        

main()

Submission status: Accepted

Solution code:
from itertools import permutations, combinations,combinations_with_replacement,product
n = int(input())
d = list(map(int,input().split()))
time = [0 for i in range(13)]
for i in range(n):
    time[d[i]] += 1
    if d[i] == 0:
        print(0)
        exit()

kouho = [0]
doti = []
for i in range(13):
    if time[i] >= 3:
        print(0)
        exit()
    elif time[i] == 2:
        kouho.append(i)
        kouho.append(24-i)
    elif time[i] == 1:
        doti.append((i,24-i))


ans = 0
for i in range(2**len(doti)):
    kar = []
    for j in range(len(doti)):
        kar.append(doti[j][i>>j&1])
    c = kouho[:] + kar[:]
    now = 12
    p = list(combinations(c,2))
    for a , b in p:
        now = min(now,min(abs(a-b),24-abs(a-b)))
        
    ans = max(ans,now)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    D = list(map(int,input().split()))
    D.sort()

    T = [0]
    ans = 24
    for i,d in enumerate(D):
        if i%2 == 0:
            t = d
        else:
            t = 24 - d
        T.append(t)

    T.sort()
    MIN = 24
    for i in range(n):
        diff = T[i+1] - T[i]
        MIN = min(diff,MIN)

    print(MIN)

main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.append(0)
from collections import Counter
ca=Counter(a)
if max(ca.values())>2 or ca[0]>1 or ca[12]>1:
  print(0)
  exit()
elif ca[12]>0 and ca[11]>0:
  print(1)
  exit()
a.sort()
ans=a[0]
for i in range(2**n):
  aa=[0]
  for j in range(n):
    if (i>>j)&1:
      aa.append((24-a[j+1])%24)
    else:
      aa.append(a[j+1]%24)
  tmp=float('inf')
  aa.sort()
  for j in range(n):
    tmp=min(tmp,aa[j+1]-aa[j])
  tmp=min(tmp,24-aa[-1])
  #print(tmp,aa)
  ans=max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int,input().split()))
Ta = [0]*13
Ta[0] = 1
for i in range(N):
    Ta[min(D[i],24-D[i])] += 1

if max(Ta)>=3:
    print(0)
elif Ta[0]>=2:
    print(0)
elif Ta[12]>=2:
    print(0)
else:
    L1 = []
    L2 = []
    for i in range(13):
        if Ta[i] ==1:
            if i==0 or i==12:
                L2.append(i)
            else:
                L1.append(i)
        elif Ta[i]==2:
            L2.append(i)
            L2.append(24-i)
    NL1 = len(L1)
    from itertools import product,combinations
    LS = list(product([0,1], repeat=NL1))
    NLS = len(LS)
    out = 0
    for i in range(NLS):
        L = LS[i]
        L3 = L2[:]
        score = 100
        for j in range(NL1):
            if L[j]==1:
                L3.append(L1[j])
            elif L[j]==0:
                L3.append(24-L1[j])
        CS = list(list(combinations(L3,2)))
        NCS = len(CS)
        for k in range(NCS):
            score = min(score,min(abs(CS[k][0]-CS[k][1]),24-abs(CS[k][0]-CS[k][1])))
        out=max(out,score)

    print(out)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

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

c = [0]*13
c[0] = 1
for d in D:
    c[d] += 1

if c[0] >= 2 or c[12] >= 2:
    print(0)
    exit()
for i in range(1,12):
    if c[i] >= 3:
        print(0)
        exit()

bitsearchlist = []
notbitsearch = [False]*24
notbitsearch[0] = True
if c[12]:
    notbitsearch[12] = True


for i in range(1,12):
    if c[i] == 1:
        bitsearchlist.append(i)
    elif c[i] == 2:
        notbitsearch[i] = True
        notbitsearch[24-i] = True

def count(List):
    anss = INF
    ret = INF
    for b in notbitsearch:
        if b:
            anss = min(anss,ret)
            ret = 0
        else:
            ret += 1
    anss = min(anss,ret)
    anss += 1
    return anss



L = len(bitsearchlist)

if L == 0:
    print(count(notbitsearch))
    exit()

ans = 0
for i in range(2**L):
    for j in range(L):
        if ((i >> j) & 1):
            notbitsearch[bitsearchlist[j]] = True
        else:
            notbitsearch[24-bitsearchlist[j]] = True
    ans = max(ans,count(notbitsearch))
    for j in range(L):
        if ((i >> j) & 1):
            notbitsearch[bitsearchlist[j]] = False
        else:
            notbitsearch[24-bitsearchlist[j]] = False

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from itertools import product, combinations
from copy import deepcopy

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    D = list(map(int, input().split()))

    cnt = Counter(D)
    S_init = [0]
    T = []
    for k, v in cnt.items():
        if k == 0:
            print(0)
            exit()
        elif k == 12:
            if v >= 2:
                print(0)
                exit()
            else:
                S_init.append(k)
        else:
            if v == 1:
                T.append(k)
            elif v == 2:
                S_init.append(k)
                S_init.append(24 - k)
            else:
                print(0)
                exit()

    res = 0
    for pattern in product([0, 1], repeat=len(T)):
        S = deepcopy(S_init)
        for idx, p in enumerate(pattern):
            if p == 1:
                S.append(24 - T[idx])
            else:
                S.append(T[idx])
        score = f_inf
        for i, j in combinations(S, 2):
            gap = abs(i - j)
            score = min(score, gap, 24 - gap)
        res = max(res, score)

    print(res)


if __name__ == '__main__':
    resolve()