提出 #71671887


ソースコード 拡げる

import sys
import string
import math
import bisect
import os
import heapq
import operator
from io import BytesIO, IOBase
from heapq import heappop,heappush
from functools import lru_cache,cache
from collections import deque,defaultdict,Counter,OrderedDict
from itertools import permutations,combinations
from array import array

INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = sys.stdin.buffer.readline

ask = lambda *x:print('?',*x,flush=True)
reply = lambda *x:print('!',*x,flush=True)

RI = lambda: int(sys.stdin.readline())
RF = lambda: float(sys.stdin.readline())
RS = lambda: sys.stdin.readline().strip()
RFF = lambda: map(float, sys.stdin.readline().split())
RII = lambda: map(int, sys.stdin.readline().split())
RSS = lambda: map(str, sys.stdin.readline().strip().split())
RIL = lambda: list(RII())
RFL = lambda: list(RFF())
RSL = lambda: list(RSS())

from types import GeneratorType
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break 
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

    
def main():
    n = RI()
    a = RIL()
    a = [x-1 for x in a]
    
    ans = 0
    vis = [False]*n
    
    p = [0]*n
    for i,x in enumerate(a):
        p[x] = i
        
    for i in range(n):
        if vis[i]==False:
            val = 0
            now = i
            while vis[now]==False:
                vis[now] = True
                val += 1
                now = p[now]
                
            ans += val*(val-1)//2
            #print(i,vis)
    
    print(ans)

    
    
        
        
        
        

if __name__ == '__main__':
    main()

提出情報

提出日時
問題 E - Minimum Swap
ユーザ x3x3
言語 Python (PyPy 3.11-v7.3.20)
得点 475
コード長 3724 Byte
結果 AC
実行時間 176 ms
メモリ 170988 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 475 / 475
結果
AC × 3
AC × 53
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 112 ms 109096 KiB
00_sample_01.txt AC 111 ms 109116 KiB
00_sample_02.txt AC 111 ms 109148 KiB
01_random_03.txt AC 164 ms 169464 KiB
01_random_04.txt AC 165 ms 169456 KiB
01_random_05.txt AC 165 ms 169524 KiB
01_random_06.txt AC 165 ms 169496 KiB
01_random_07.txt AC 165 ms 169568 KiB
01_random_08.txt AC 164 ms 169464 KiB
01_random_09.txt AC 164 ms 169384 KiB
01_random_10.txt AC 165 ms 169464 KiB
01_random_11.txt AC 165 ms 169548 KiB
01_random_12.txt AC 164 ms 169456 KiB
01_random_13.txt AC 165 ms 169448 KiB
01_random_14.txt AC 165 ms 169516 KiB
01_random_15.txt AC 165 ms 169428 KiB
01_random_16.txt AC 141 ms 141948 KiB
01_random_17.txt AC 137 ms 138516 KiB
01_random_18.txt AC 115 ms 111408 KiB
01_random_19.txt AC 122 ms 122708 KiB
01_random_20.txt AC 162 ms 164396 KiB
01_random_21.txt AC 166 ms 169536 KiB
01_random_22.txt AC 166 ms 169508 KiB
01_random_23.txt AC 167 ms 169108 KiB
01_random_24.txt AC 168 ms 169688 KiB
01_random_25.txt AC 167 ms 169328 KiB
01_random_26.txt AC 167 ms 169552 KiB
01_random_27.txt AC 167 ms 169120 KiB
01_random_28.txt AC 169 ms 169508 KiB
01_random_29.txt AC 170 ms 169672 KiB
01_random_30.txt AC 169 ms 169980 KiB
01_random_31.txt AC 169 ms 169948 KiB
01_random_32.txt AC 170 ms 170012 KiB
01_random_33.txt AC 170 ms 169844 KiB
01_random_34.txt AC 176 ms 170988 KiB
01_random_35.txt AC 173 ms 170728 KiB
01_random_36.txt AC 169 ms 170980 KiB
01_random_37.txt AC 141 ms 135748 KiB
01_random_38.txt AC 147 ms 139388 KiB
01_random_39.txt AC 152 ms 143700 KiB
01_random_40.txt AC 167 ms 164028 KiB
01_random_41.txt AC 130 ms 126852 KiB
01_random_42.txt AC 134 ms 131680 KiB
01_random_43.txt AC 123 ms 114216 KiB
01_random_44.txt AC 141 ms 138080 KiB
01_random_45.txt AC 130 ms 126704 KiB
01_random_46.txt AC 163 ms 170420 KiB
01_random_47.txt AC 166 ms 169420 KiB
01_random_48.txt AC 167 ms 169676 KiB
01_random_49.txt AC 165 ms 169520 KiB
01_random_50.txt AC 155 ms 169480 KiB
01_random_51.txt AC 155 ms 169676 KiB
01_random_52.txt AC 157 ms 169472 KiB