Problem:


Score : 600 points

Problem Statement

We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.

You will do the following operation N-1 times:

Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.

After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.

Find the maximum number of points you can gain.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_{3N}

Output

Print the maximum number of points you can gain.

Sample Input 1

2
1 2 1 2 2 1

Sample Output 1

2

Let us rearrange the five leftmost cards so that the integers written on the six cards will be 2\ 2\ 2\ 1\ 1\ 1 from left to right.

Then, remove the three leftmost cards, all of which have the same integer 2, gaining 1 point.

Now, the integers written on the remaining cards are 1\ 1\ 1.

Since these three cards have the same integer 1, we gain 1 more point.

In this way, we can gain 2 points - which is the maximum possible.

Sample Input 2

3
1 1 2 2 3 3 3 2 1

Sample Output 2

1

Sample Input 3

3
1 1 2 2 2 3 3 3 1

Sample Output 3

3



Sample input:
2
1 2 1 2 2 1


Sample output:
2

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter


def giveup(count, inheritable, thr):
    inh_thr = 3 - thr
    for i in range(n):
        if count[i] >= thr:
            count[i] = 3
        if count[i] >= 1:
            inheritable[i] -= inh_thr


n, *aaa = map(int, sys.stdin.buffer.read().split())
if n == 1:
    print(1)
    exit()

aaa = [a - 1 for a in aaa]
count = [0] * n
inheritable = [2] * n
ans = 0
for i in range(5):
    a = aaa[i]
    count[a] += 1
    if count[a] == 3:
        ans += 1

for j in range(n - 2):
    cnt = Counter(aaa[5 + j * 3:8 + j * 3])
    if len(cnt) == 1:
        count[aaa[5 + j * 3]] = 3
        ans += 1
    elif len(cnt) == 2:
        (a2, _), (a1, _) = cnt.most_common()
        if count[a2] == 1 and inheritable[a2] >= 1:
            ans += 1
            giveup(count, inheritable, 2)
        elif count[a1] == 2 and inheritable[a1] == 2:
            ans += 1
            giveup(count, inheritable, 1)
        count[a2] += 2
        count[a1] += 1
    else:
        for a in cnt:
            if count[a] == 2 and inheritable[a] == 2:
                ans += 1
                giveup(count, inheritable, 1)
            count[a] += 1

if count[aaa[-1]] == 2 and inheritable[aaa[-1]] == 2:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
        X[a2] = [max(X[a2][j], MA[j]) for j in range(N+1)]
        ma = max(ma, X[a2][a2], max(X[a1]))
        MA = [max(MA[j], X[a1][j]) for j in range(N+1)]
        MA[a1] = max(MA[a1], max(X[a1]))
        MA[a2] = max(MA[a2], X[a2][a2])
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        X[a], X[b], X[c] = [max(X[a][j], MA[j]) for j in range(N+1)], [max(X[b][j], MA[j]) for j in range(N+1)], [max(X[c][j], MA[j]) for j in range(N+1)]
        MA[a] = max(MA[a], X[a][b], X[a][c])
        MA[b] = max(MA[b], X[a][b], X[b][c])
        MA[c] = max(MA[c], X[a][c], X[b][c])
        ma = max(ma, MA[a], MA[b], MA[c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter


def giveup(count, inheritable, thr):
    inh_thr = 3 - thr
    for i in range(n):
        if count[i] >= thr:
            count[i] = 3
        if count[i] >= 1:
            inheritable[i] -= inh_thr


n, *aaa = map(int, sys.stdin.buffer.read().split())
if n == 1:
    print(1)
    exit()

aaa = [a - 1 for a in aaa]
count = [0] * n
inheritable = [2] * n
ans = 0
for i in range(5):
    a = aaa[i]
    c = count[a]
    count[a] += 1
    if c == 2:
        ans += 1
        continue

for j in range(n - 2):
    cnt = Counter(aaa[5 + j * 3:8 + j * 3])
    if len(cnt) == 1:
        count[aaa[5 + j * 3]] = 3
        ans += 1
    elif len(cnt) == 2:
        (a2, _), (a1, _) = cnt.most_common()
        if count[a2] == 1 and inheritable[a2] >= 1:
            ans += 1
            giveup(count, inheritable, 2)
        elif count[a1] == 2 and inheritable[a1] == 2:
            ans += 1
            giveup(count, inheritable, 1)
        count[a2] += 2
        count[a1] += 1
    else:
        for a in cnt:
            if count[a] == 2:
                ans += 1
                giveup(count, inheritable, 1)
            count[a] += 1

if count[aaa[-1]] == 2 and inheritable[aaa[-1]] == 2:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
        ma = max(ma, X[a2][a2], max(X[a1]))
        MA = [max(MA[j], X[a1][j]) for j in range(N+1)]
        MA[a1] = max(MA[a1], max(X[a1]))
        MA[a2] = max(MA[a2], X[a2][a2])
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        X[a], X[b], X[c] = [max(X[a][j], MA[j]) for j in range(N+1)], [max(X[b][j], MA[j]) for j in range(N+1)], [max(X[c][j], MA[j]) for j in range(N+1)]
        MA[a] = max(MA[a], max(X[a]))
        MA[b] = max(MA[b], max(X[b]))
        MA[c] = max(MA[c], max(X[c]))
        ma = max(ma, MA[a], MA[b], MA[c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
        ma = max(ma, X[a2][a2], max(X[a1]))
        MA = [max(MA[j], X[a1][j]) for j in range(N+1)]
        MA[a1] = max(MA[a1], max(X[a1]))
        MA[a2] = max(MA[a2], X[a2][a2])
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        X[a], X[b], X[c] = [max(X[a][j], MA[j]) for j in range(N+1)], [max(X[b][j], MA[j]) for j in range(N+1)], [max(X[c][j], MA[j]) for j in range(N+1)]
        MA[a] = max(MA[a], X[a][b], X[a][c])
        MA[b] = max(MA[b], X[a][b], X[b][c])
        MA[c] = max(MA[c], X[a][c], X[b][c])
        ma = max(ma, max(X[a]), max(X[b]), max(X[c]))

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
        ma = max(ma, X[a2][a2], max(X[a1]))
        MA = [max(MA[j], X[a1][j]) for j in range(N+1)]
        MA[a1] = max(MA[a1], max(X[a1]))
        MA[a2] = max(MA[a2], X[a2][a2])
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        X[a], X[b], X[c] = [max(X[a][j], MA[j]) for j in range(N+1)], [max(X[b][j], MA[j]) for j in range(N+1)], [max(X[c][j], MA[j]) for j in range(N+1)]
        MA[a] = max(MA[a], X[a][b], X[a][c])
        MA[b] = max(MA[b], X[a][b], X[b][c])
        MA[c] = max(MA[c], X[a][c], X[b][c])
        ma = max(ma, X[a][b], X[a][c], X[b][c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.add(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.add(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if d in doubles:
        doubles.clear()
        if s in singles:
          doubles.add(s)
        singles.add(s)
        ans+=1
        continue
      else:
        doubles.clear()
        singles.remove(d)
        if s in singles:
          doubles.add(s)
        singles.add(s)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.add(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.add(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if d in doubles:
        doubles.remove(d)
        if s in singles:
          doubles.add(s)
        singles.add(s)
        ans+=1
        continue
      else:
        doubles.clear()
        singles.remove(d)
        if s in singles:
          doubles.add(s)
        singles.add(s)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.add(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.add(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if d in doubles:
        doubles.remove(d)
        if s in singles:
          doubles.add(s)
        singles.add(s)
        ans+=1
        continue
      else:
        doubles.clear()
        singles.remove(d)
        singles.add(s)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
        ma = max(ma, X[a2][a2], max(X[a1]))
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        ma = max(ma, X[a][b], X[a][c], X[b][c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.add(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.add(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if s in singles:
        doubles.add(s)
      singles.add(s)
      if d in doubles:
        doubles.remove(d)
        ans+=1
        continue
      else:
        singles.remove(d)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], ma), max(ma, X[a2][a2], X[a1][a1] + 1)
        ma = max(ma, X[a2][a2], max(X[a1]))
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        ma = max(ma, X[a][b], X[a][c], X[b][c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
ans=0
n=int(input())
a=list(map(int,input().split()))
temp=[-1 for i in range(n+1)]
x=[-1 for i in range(3*n)]
for i in range(3*n-1,-1,-1):
    num=a[i]
    x[i]=temp[num]
    temp[num]=i
b=[-1 for i in range(3*n)]
for i in range(3*n-1,-1,-1):
    if x[i]!=-1:
        b[i]=x[x[i]]
i=1
el=[0,1]
while(i<3*n):
    if(i+3>=3*n):
        el.append(3*n-1)
        break
    for j in range(3):
        i+=1
        el.append(i)
    q=float('inf')
    p=-1
    for i in el:
        if b[i]<q and b[i]!=-1:
            q=b[i]
            p=a[i]
    rem=[]
    for i in el:
        if a[i]!=p:
            rem.append(i)
    s=0
    if len(rem)==5:
        el.pop()
        el.pop()
        el.pop()
    elif len(rem)<3:
        ans+=1
        torem=[]
        for i in el:
            if a[i]==p:
                s+=1
                torem.append(i)
                if s==3:
                    for i in torem:
                        el.remove(i)
                    break
    else:
        q=float('inf')
        save=-1
        for i in rem:
            if b[i]<q:
                save=i
                q=b[i]
        torem=[]
        for i in rem:
            if i!=save:
                torem.append(i)
        for i in torem:
            el.remove(i)

if a[el[0]]==a[el[1]]==a[el[2]]:
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1][a2], X[a2][a2], X[a1] = max(X[a1][a2], ma), max(ma, X[a2][a2], X[a1][a1] + 1), [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1], ma) for j in range(N+1)]
        ma = max(ma, X[a2][a2], max(X[a1]))
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], X[b][a], ma, X[c][c] + 1), max(X[a][c], X[c][a], ma, X[b][b] + 1), max(X[b][c], X[c][b], ma, X[a][a] + 1)
        ma = max(ma, X[a][b], X[a][c], X[b][c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1][a2], X[a2][a2], X[a1] = ma, max(ma, X[a2][a2], X[a1][a1] + 1), [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)]
        ma = max(ma, X[a2][a2], max(X[a1]))
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], X[b][a], ma, X[c][c] + 1), max(X[a][c], X[c][a], ma, X[b][b] + 1), max(X[b][c], X[c][b], ma, X[a][a] + 1)
        ma = max(ma, X[a][b], X[a][c], X[b][c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.add(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.add(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if s in singles:
        doubles.add(s)
      if d in doubles:
        #print('d in doubles')
        doubles.clear()
        if s in singles:
          doubles.add(s)
        singles.add(s)
        ans+=1
        continue
      else:
        doubles.clear()
        if s in singles:
          doubles.add(s)
        singles.remove(d)
        singles.add(s)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
for i in range(N):
    B = A[i*3+2:i*3+5]
    cnt = len(set(B))
    if cnt == 1:
        ans += 1
    elif cnt == 2:
        a1 = B[0]^B[1]^B[2]
        a2 = (sum(B) - a1) // 2
        X[a1][a2], X[a2][a2], X[a1] = ma, max(ma, X[a2][a2], X[a1][a1] + 1), [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)]
        ma = max(ma, X[a2][a2], max(X[a1]))
    else:
        a, b, c = B
        X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
        ma = max(ma, X[a][b], X[a][c], X[b][c])

print(ans + max(ma, max(max(x) for x in X)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.add(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.add(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if s in singles:
        doubles.add(s)
      singles.add(s)
      if d in doubles:
        doubles.clear()
        ans+=1
        continue
      else:
        doubles.clear()
        singles.remove(d)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Submission status: Wrong Answer

Solution code:

##main#######################################

## INPUT ####################################
N=int(input())
A = list(map(int,list(input().split())))

## PROCESSING ###############################
#print("++++PROC++++")

ans=0

###for idx in range(N*3):
#print("first:")
#print(A)
while len(A)>0:
	#print(AA)
	AAAA= A[0:5]
	AAAAS = sorted(AAAA)

	
	c=0
	i=0
	deletieNumFlg=0
	while i < len(AAAAS):
		NUM1=AAAAS[i]
		c=AAAA.count(NUM1)

		#print('i:{}  len:{}'.format(i,len(AAAAS)))
		#print("dddd1")
		#print(CHAR1)
		#print("c:" + str(c) +"// CHAR1:" + CHAR1)
		#print("dddd2")

		if c>=3:
			###AAAA.remove(NUM1)
			###AAAA.remove(NUM1)
			###AAAA.remove(NUM1)
			ans=ans+1
			deletieNumFlg=1
			i=99
		else :
			i=i+c
			deletieNumFlg=0

	
	#print("flg:" + str(deletieNumFlg))
	#print("next(bef):")
	#print(A)
	if deletieNumFlg==1:
		A.remove(NUM1)
		A.remove(NUM1)
		A.remove(NUM1)
		###AA = ''.join(AAAA) + AA[5:]
	else:
		A.pop(0)
		A.pop(0)
		A.pop(0)
		###AA = AA[3:]

	#print("flg:" + str(deletieNumFlg) )
	#print("next(aft):")
	#print(A)

## OUTPUT ###################################

#print("++++OUTPUT++++")
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

ans = 0
i = 0
cnt = [0] * (N + 1)
cnt[A[0]] += 1
cnt[A[1]] += 1
A = A[2:]

while i < N:
    # print(cnt, ans)

    cur = [cnt[A[j]] for j in range(3 * i, 3 * i + 3)]
    a = [A[j] for j in range(3 * i, 3 * i + 3)]
    # print("", cur)

    for j in range(3 * i, 3 * i + 3):
        cnt[A[j]] += 1

    ok = False

    for j in range(3):
        if cnt[A[3 * i + j]] >= 3 and cur[j] == 0:
            # print("1")
            ans += 1
            ok = True
            for k in range(N + 1):
                if k == A[3 * i + j]:
                    cnt[k] -= 3
                    continue
                cnt[k] = min(2, cnt[k])
            i += 1
            break

    if ok: continue

    for j in range(3):
        if cnt[A[3 * i + j]] >= 3 and cur[j] == 1:
            # print("2")
            ans += 1
            ok = True
            for k in range(N + 1):
                if k == A[3 * i + j]:
                    cnt[k] -= 3
                    continue
                if k in a:
                    cnt[k] = min(2, cnt[k])
                else:
                    cnt[k] = min(1, cnt[k])
            i += 1
            break

    if ok: continue

    for j in range(3):
        if cnt[A[3 * i + j]] >= 3 and cur[j] == 2:
            # print("3", j)
            ans += 1
            ok = True
            for k in range(N + 1):
                if k == A[3 * i + j]:
                    cnt[k] -= 3
                    continue
                if k in a:
                    cnt[k] = min(2, cur.count(k))
                else:
                    cnt[k] = 0
            break
    i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

ima = [0 for i in range(n)]
for i in range(3*n):
    a[i] -= 1

count = 0
data = a[0:5]
data.sort()
po = s(data)
for ab in po:
    if ab[1] >= 3:
        ima[ab[0]] += ab[1]-3
        count += 1
    else:
        ima[ab[0]] += ab[1]

for i in range(n-2):
    data = a[5+2*i:5+2*i+3]
    data.sort(reverse = True)
    po = s(data)
    for ab in po:
        if ab[1] + ima[ab[0]] >= 3:
            use = 3 - ab[1]
            count += 1
            break
    else:
        for ab in po:
            ima[ab[0]] += ab[1]
            ima[ab[0]] = min(2,ima[ab[0]])
if ima[a[-1]] == 2:
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
num = list(map(int, input().split()))
ans = 0
frag = 0
A = 0
B = 0
C = 0
for i in range(n - 1):
    for a in range(5):
        for b in range(a, 5):
            for c in range(b, 5):
                if num[a] == num[b] == num[c]:
                    frag = 1
                    A = a
                    B = b
                    C = c
                    ans += 1
                    break
            if frag == 1:
                break
        if frag == 1:
            break
    if frag == 1:
        del num[A]
        del num[B]
        del num[C]
        frag = 0
if num[0] == num[1] == num[2]:
    ans += 1
print(ans)