提出 #53669546


ソースコード 拡げる

# https://github.com/tatyam-prime/SortedSet/blob/main/SortedSet.py
import math
from bisect import bisect_left, bisect_right
from typing import Generic, Iterable, Iterator, List, Tuple, TypeVar, Optional
T = TypeVar('T')

class SortedSet(Generic[T]):
    BUCKET_RATIO = 16
    SPLIT_RATIO = 24
    
    def __init__(self, a: Iterable[T] = []) -> None:
        "Make a new SortedSet from iterable. / O(N) if sorted and unique / O(N log N)"
        a = list(a)
        n = self.size = len(a)
        if any(a[i] > a[i + 1] for i in range(n - 1)):
            a.sort()
        if any(a[i] >= a[i + 1] for i in range(n - 1)):
            a, b = [], a
            for x in b:
                if not a or a[-1] != x:
                    a.append(x)
        bucket_size = int(math.ceil(math.sqrt(n / self.BUCKET_RATIO)))
        self.a = [a[n * i // bucket_size : n * (i + 1) // bucket_size] for i in range(bucket_size)]

    def __iter__(self) -> Iterator[T]:
        for i in self.a:
            for j in i: yield j

    def __reversed__(self) -> Iterator[T]:
        for i in reversed(self.a):
            for j in reversed(i): yield j
    
    def __eq__(self, other) -> bool:
        return list(self) == list(other)
    
    def __len__(self) -> int:
        return self.size
    
    def __repr__(self) -> str:
        return "SortedSet" + str(self.a)
    
    def __str__(self) -> str:
        s = str(list(self))
        return "{" + s[1 : len(s) - 1] + "}"

    def _position(self, x: T) -> Tuple[List[T], int, int]:
        "return the bucket, index of the bucket and position in which x should be. self must not be empty."
        for i, a in enumerate(self.a):
            if x <= a[-1]: break
        return (a, i, bisect_left(a, x))

    def __contains__(self, x: T) -> bool:
        if self.size == 0: return False
        a, _, i = self._position(x)
        return i != len(a) and a[i] == x

    def add(self, x: T) -> bool:
        "Add an element and return True if added. / O(√N)"
        if self.size == 0:
            self.a = [[x]]
            self.size = 1
            return True
        a, b, i = self._position(x)
        if i != len(a) and a[i] == x: return False
        a.insert(i, x)
        self.size += 1
        if len(a) > len(self.a) * self.SPLIT_RATIO:
            mid = len(a) >> 1
            self.a[b:b+1] = [a[:mid], a[mid:]]
        return True
    
    def _pop(self, a: List[T], b: int, i: int) -> T:
        ans = a.pop(i)
        self.size -= 1
        if not a: del self.a[b]
        return ans

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0: return False
        a, b, i = self._position(x)
        if i == len(a) or a[i] != x: return False
        self._pop(a, b, i)
        return True
    
    def lt(self, x: T) -> Optional[T]:
        "Find the largest element < x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] < x:
                return a[bisect_left(a, x) - 1]

    def le(self, x: T) -> Optional[T]:
        "Find the largest element <= x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] <= x:
                return a[bisect_right(a, x) - 1]

    def gt(self, x: T) -> Optional[T]:
        "Find the smallest element > x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] > x:
                return a[bisect_right(a, x)]

    def ge(self, x: T) -> Optional[T]:
        "Find the smallest element >= x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] >= x:
                return a[bisect_left(a, x)]
    
    def __getitem__(self, i: int) -> T:
        "Return the i-th element."
        if i < 0:
            for a in reversed(self.a):
                i += len(a)
                if i >= 0: return a[i]
        else:
            for a in self.a:
                if i < len(a): return a[i]
                i -= len(a)
        raise IndexError
    
    def pop(self, i: int = -1) -> T:
        "Pop and return the i-th element."
        if i < 0:
            for b, a in enumerate(reversed(self.a)):
                i += len(a)
                if i >= 0: return self._pop(a, ~b, i)
        else:
            for b, a in enumerate(self.a):
                if i < len(a): return self._pop(a, b, i)
                i -= len(a)
        raise IndexError
    
    def index(self, x: T) -> int:
        "Count the number of elements < x."
        ans = 0
        for a in self.a:
            if a[-1] >= x:
                return ans + bisect_left(a, x)
            ans += len(a)
        return ans

    def index_right(self, x: T) -> int:
        "Count the number of elements <= x."
        ans = 0
        for a in self.a:
            if a[-1] > x:
                return ans + bisect_right(a, x)
            ans += len(a)
        return ans


n, m = map(int, input().split())
a = list(map(int, input().split()))
# a.sort()
if max(a) == n or min(a) == 1:
    print(-1)
    exit()
a = set(a)

ss = SortedSet(list(range(1, n+1)))
ans = [-1]*n
for i in range(n):
    if i+1 in a:
        ans[i] = i+2
        ss.discard(i+2)
    else:
        ans[i] = ss.pop(0)
print(*ans)

提出情報

提出日時
問題 A - Good Permutation 2
ユーザ noriaoki
言語 Python (PyPy 3.10-v7.3.12)
得点 400
コード長 5400 Byte
結果 AC
実行時間 518 ms
メモリ 151984 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 4
AC × 50
セット名 テストケース
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 01-sample-04.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 01-sample-04.txt, 02-min-01.txt, 03-two-three-01.txt, 03-two-three-02.txt, 03-two-three-03.txt, 03-two-three-04.txt, 03-two-three-05.txt, 03-two-three-06.txt, 03-two-three-07.txt, 03-two-three-08.txt, 03-two-three-09.txt, 03-two-three-10.txt, 04-large-rand-ok-01.txt, 04-large-rand-ok-02.txt, 04-large-rand-ok-03.txt, 04-large-rand-ok-04.txt, 04-large-rand-ok-05.txt, 05-large-rand-ng-01.txt, 05-large-rand-ng-02.txt, 05-large-rand-ng-03.txt, 05-large-rand-ng-04.txt, 05-large-rand-ng-05.txt, 06-large-rand-bias-01.txt, 06-large-rand-bias-02.txt, 06-large-rand-bias-03.txt, 06-large-rand-bias-04.txt, 06-large-rand-bias-05.txt, 06-large-rand-bias-06.txt, 06-large-rand-bias-07.txt, 06-large-rand-bias-08.txt, 06-large-rand-bias-09.txt, 06-large-rand-bias-10.txt, 06-large-rand-bias-11.txt, 06-large-rand-bias-12.txt, 06-large-rand-bias-13.txt, 06-large-rand-bias-14.txt, 06-large-rand-bias-15.txt, 06-large-rand-bias-16.txt, 06-large-rand-bias-17.txt, 06-large-rand-bias-18.txt, 06-large-rand-bias-19.txt, 06-large-rand-bias-20.txt, 07-large-rand-alternating-01.txt, 07-large-rand-alternating-02.txt, 07-large-rand-alternating-03.txt, 07-large-rand-alternating-04.txt, 07-large-rand-alternating-05.txt
ケース名 結果 実行時間 メモリ
01-sample-01.txt AC 119 ms 85912 KiB
01-sample-02.txt AC 119 ms 85828 KiB
01-sample-03.txt AC 119 ms 85712 KiB
01-sample-04.txt AC 120 ms 85464 KiB
02-min-01.txt AC 121 ms 85832 KiB
03-two-three-01.txt AC 119 ms 85524 KiB
03-two-three-02.txt AC 121 ms 85876 KiB
03-two-three-03.txt AC 119 ms 85652 KiB
03-two-three-04.txt AC 121 ms 85724 KiB
03-two-three-05.txt AC 119 ms 85908 KiB
03-two-three-06.txt AC 120 ms 85864 KiB
03-two-three-07.txt AC 120 ms 85708 KiB
03-two-three-08.txt AC 120 ms 85680 KiB
03-two-three-09.txt AC 120 ms 85556 KiB
03-two-three-10.txt AC 118 ms 85536 KiB
04-large-rand-ok-01.txt AC 475 ms 118776 KiB
04-large-rand-ok-02.txt AC 458 ms 118384 KiB
04-large-rand-ok-03.txt AC 462 ms 118212 KiB
04-large-rand-ok-04.txt AC 469 ms 118352 KiB
04-large-rand-ok-05.txt AC 453 ms 118164 KiB
05-large-rand-ng-01.txt AC 130 ms 100492 KiB
05-large-rand-ng-02.txt AC 130 ms 100456 KiB
05-large-rand-ng-03.txt AC 131 ms 100312 KiB
05-large-rand-ng-04.txt AC 132 ms 100532 KiB
05-large-rand-ng-05.txt AC 130 ms 100172 KiB
06-large-rand-bias-01.txt AC 518 ms 151604 KiB
06-large-rand-bias-02.txt AC 506 ms 151760 KiB
06-large-rand-bias-03.txt AC 142 ms 117056 KiB
06-large-rand-bias-04.txt AC 143 ms 116984 KiB
06-large-rand-bias-05.txt AC 140 ms 113640 KiB
06-large-rand-bias-06.txt AC 515 ms 151984 KiB
06-large-rand-bias-07.txt AC 512 ms 151880 KiB
06-large-rand-bias-08.txt AC 140 ms 113636 KiB
06-large-rand-bias-09.txt AC 140 ms 113472 KiB
06-large-rand-bias-10.txt AC 140 ms 113572 KiB
06-large-rand-bias-11.txt AC 440 ms 99968 KiB
06-large-rand-bias-12.txt AC 435 ms 99376 KiB
06-large-rand-bias-13.txt AC 124 ms 86528 KiB
06-large-rand-bias-14.txt AC 122 ms 86436 KiB
06-large-rand-bias-15.txt AC 122 ms 86824 KiB
06-large-rand-bias-16.txt AC 431 ms 97616 KiB
06-large-rand-bias-17.txt AC 419 ms 97080 KiB
06-large-rand-bias-18.txt AC 119 ms 85684 KiB
06-large-rand-bias-19.txt AC 119 ms 85460 KiB
06-large-rand-bias-20.txt AC 119 ms 85660 KiB
07-large-rand-alternating-01.txt AC 469 ms 118612 KiB
07-large-rand-alternating-02.txt AC 470 ms 118488 KiB
07-large-rand-alternating-03.txt AC 464 ms 118388 KiB
07-large-rand-alternating-04.txt AC 443 ms 118068 KiB
07-large-rand-alternating-05.txt AC 426 ms 117808 KiB