Submission #9582313


Source Code Expand

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

import itertools

N = int(readline())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))

class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)
        
    def build(self,seq):
        data = seq
        size = self.size
        for i,x in enumerate(data):
            j = i+(i&(-i))
            if j < size:
                data[j] += data[i]
        self.data = data
        
    def __repr__(self):
        return self.data.__repr__()
        
    def get_sum(self,i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        data = self.data
        size = self.size
        while i < size:
            data[i] += x
            i += i & -i
    
    def find_kth_element(self,k):
        data = self.data; size = self.size
        x,sx = 0,0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x,sx = y,sy
        return x + 1

def Inversion(seq):
    # seqは、1,2,...,Nの順列
    N = len(seq)
    bit = BinaryIndexedTree([0] * (N+1))
    inv = N*(N-1)//2
    for x in seq:
        inv -= bit.get_sum(x)
        bit.add(x,1)
    return inv

INF = 10 ** 9
answer = INF
for I in itertools.combinations(range(N),(N+1)//2):
    J = [j for j in range(N) if j not in I]
    ODD = [(B[i] if i&1 else A[i],i) for i in I]
    EV = [(A[i] if i&1 else B[i],i) for i in J]
    ODD.sort()
    EV.sort()
    ind = [0] * N
    seq = [0] * N
    for i in range(0,N,2):
        seq[i], ind[i] = ODD[i//2]
    for i in range(1,N,2):
        seq[i], ind[i] = EV[i//2]
    if not all(x<= y for x,y in zip(seq,seq[1:])):
        continue
    ind = [x+1 for x in ind]
    n = Inversion(ind)
    if answer > n:
        answer = n

if answer == INF:
    answer = -1
print(answer)

Submission Info

Submission Time
Task D - Swap and Flip
User maspy
Language PyPy3 (2.4.0)
Score 700
Code Size 2297 Byte
Status AC
Exec Time 938 ms
Memory 58716 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 5
AC × 44
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, sample-05.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, sample-05.txt
Case Name Status Exec Time Memory
01.txt AC 164 ms 38256 KB
02.txt AC 185 ms 39792 KB
03.txt AC 366 ms 51036 KB
04.txt AC 813 ms 53212 KB
05.txt AC 755 ms 47708 KB
06.txt AC 645 ms 51164 KB
07.txt AC 670 ms 49884 KB
08.txt AC 777 ms 52700 KB
09.txt AC 754 ms 51932 KB
10.txt AC 652 ms 52316 KB
11.txt AC 705 ms 45020 KB
12.txt AC 747 ms 49020 KB
13.txt AC 871 ms 57564 KB
14.txt AC 692 ms 44508 KB
15.txt AC 708 ms 45148 KB
16.txt AC 817 ms 52188 KB
17.txt AC 731 ms 52060 KB
18.txt AC 861 ms 54364 KB
19.txt AC 898 ms 58716 KB
20.txt AC 851 ms 55076 KB
21.txt AC 800 ms 53596 KB
22.txt AC 606 ms 52188 KB
23.txt AC 163 ms 38256 KB
24.txt AC 856 ms 55260 KB
25.txt AC 829 ms 52828 KB
26.txt AC 790 ms 52188 KB
27.txt AC 822 ms 52956 KB
28.txt AC 856 ms 56156 KB
29.txt AC 845 ms 53596 KB
30.txt AC 843 ms 54236 KB
31.txt AC 818 ms 51676 KB
32.txt AC 770 ms 49756 KB
33.txt AC 842 ms 50012 KB
34.txt AC 938 ms 54876 KB
35.txt AC 873 ms 52828 KB
36.txt AC 894 ms 53780 KB
37.txt AC 856 ms 53596 KB
38.txt AC 866 ms 52440 KB
39.txt AC 450 ms 52056 KB
sample-01.txt AC 163 ms 38256 KB
sample-02.txt AC 162 ms 38256 KB
sample-03.txt AC 165 ms 38256 KB
sample-04.txt AC 165 ms 38256 KB
sample-05.txt AC 164 ms 38256 KB