結果

問題 No.2594 Mix shake!!
ユーザー SumitacchanSumitacchan
提出日時 2023-12-17 16:10:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 4,584 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2023-12-20 01:05:16
合計ジャッジ時間 5,719 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
11,520 KB
testcase_01 AC 40 ms
11,520 KB
testcase_02 AC 38 ms
11,520 KB
testcase_03 AC 38 ms
11,520 KB
testcase_04 AC 37 ms
11,520 KB
testcase_05 AC 37 ms
11,520 KB
testcase_06 AC 38 ms
11,520 KB
testcase_07 AC 38 ms
11,520 KB
testcase_08 AC 38 ms
11,520 KB
testcase_09 AC 38 ms
11,520 KB
testcase_10 AC 37 ms
11,520 KB
testcase_11 AC 38 ms
11,520 KB
testcase_12 AC 37 ms
11,520 KB
testcase_13 AC 37 ms
11,520 KB
testcase_14 AC 38 ms
11,520 KB
testcase_15 AC 39 ms
11,520 KB
testcase_16 AC 38 ms
11,520 KB
testcase_17 AC 38 ms
11,520 KB
testcase_18 AC 38 ms
11,520 KB
testcase_19 AC 38 ms
11,520 KB
testcase_20 AC 38 ms
11,520 KB
testcase_21 AC 38 ms
11,520 KB
testcase_22 AC 38 ms
11,520 KB
testcase_23 AC 38 ms
11,520 KB
testcase_24 AC 38 ms
11,520 KB
testcase_25 AC 39 ms
11,520 KB
testcase_26 AC 38 ms
11,520 KB
testcase_27 AC 38 ms
11,520 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 39 ms
11,520 KB
testcase_33 AC 39 ms
11,520 KB
testcase_34 AC 51 ms
11,520 KB
testcase_35 WA -
testcase_36 AC 50 ms
11,520 KB
testcase_37 WA -
testcase_38 AC 52 ms
11,520 KB
testcase_39 WA -
testcase_40 AC 50 ms
11,520 KB
testcase_41 WA -
testcase_42 AC 50 ms
11,520 KB
testcase_43 WA -
testcase_44 AC 51 ms
11,520 KB
testcase_45 WA -
testcase_46 AC 52 ms
11,520 KB
testcase_47 WA -
testcase_48 AC 50 ms
11,520 KB
testcase_49 WA -
testcase_50 AC 53 ms
11,520 KB
testcase_51 WA -
testcase_52 AC 52 ms
11,520 KB
testcase_53 WA -
testcase_54 AC 39 ms
11,520 KB
testcase_55 AC 38 ms
11,520 KB
testcase_56 AC 37 ms
11,520 KB
testcase_57 AC 37 ms
11,520 KB
testcase_58 AC 37 ms
11,520 KB
testcase_59 AC 38 ms
11,520 KB
testcase_60 AC 37 ms
11,520 KB
testcase_61 AC 37 ms
11,520 KB
testcase_62 AC 37 ms
11,520 KB
testcase_63 AC 37 ms
11,520 KB
testcase_64 AC 37 ms
11,520 KB
testcase_65 AC 38 ms
11,520 KB
testcase_66 AC 39 ms
11,520 KB
testcase_67 AC 37 ms
11,520 KB
testcase_68 AC 38 ms
11,520 KB
testcase_69 AC 38 ms
11,520 KB
testcase_70 AC 39 ms
11,520 KB
testcase_71 AC 38 ms
11,520 KB
testcase_72 AC 37 ms
11,520 KB
testcase_73 AC 37 ms
11,520 KB
testcase_74 AC 37 ms
11,520 KB
testcase_75 AC 38 ms
11,520 KB
testcase_76 AC 37 ms
11,520 KB
testcase_77 AC 50 ms
11,520 KB
testcase_78 AC 38 ms
11,520 KB
testcase_79 AC 38 ms
11,520 KB
testcase_80 AC 40 ms
11,520 KB
testcase_81 AC 39 ms
11,520 KB
testcase_82 AC 40 ms
11,520 KB
testcase_83 AC 38 ms
11,520 KB
testcase_84 AC 38 ms
11,520 KB
testcase_85 AC 38 ms
11,520 KB
testcase_86 AC 38 ms
11,520 KB
testcase_87 AC 38 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
n-1本の条件を、離れた3点を2本で結べるかしか見ていない
"""
from fractions import Fraction
import sys
from typing import Sequence

def validate(n: int, a: Sequence[int], b: Sequence[int], c: Sequence[int], d: Sequence[int]) -> None:
    MIN_N = 2
    MAX_N = 50
    MIN_VAL = 1
    MAX_VAL = 10 ** 17
    
    assert MIN_N <= n <= MAX_N
    for seq in [a, b, c, d]:
        assert len(seq) == n
        assert all(MIN_VAL <= x <= MAX_VAL for x in seq)

    assert sum(a) == sum(c)
    assert sum(b) == sum(d)

def output_ans(ans: bool) -> None:
    if ans:
        print("Yes")
    else:
        print("No")
    sys.exit()

def compute_slope(n: int, a: Sequence[int], b: Sequence[int]) -> tuple[list[tuple[int, int]], list[int]]:
    """b[i]/a[i] の昇順に加算した (aの累積和,bの累積和) と、昇順のソートした際の index の列を返す"""
    p: list[int] = sorted(list(range(n)), key=lambda i: Fraction(b[i], a[i]))
    slope: list[tuple[int, int]] = [(0, 0)]
    for i in p:
        x, y = slope[-1]
        slope.append((x + a[i], y + b[i]))
    return slope, p

def evaluate(slope: Sequence[tuple[int, int]], x: int) -> Fraction:
    for i in range(len(slope) - 1):
        if slope[i][0] <= x <= slope[i + 1][0]:
            x0, y0 = slope[i]
            x1, y1 = slope[i + 1]
            return Fraction((x1 - x) * y0 + (x - x0) * y1, x1 - x0)
    assert False


if __name__ == "__main__":
    n: int = int(input())
    a: list[int] = list(map(int, input().split()))
    b: list[int] = list(map(int, input().split()))
    c: list[int] = list(map(int, input().split()))
    d: list[int] = list(map(int, input().split()))
    validate(n, a, b, c, d)
    
    ls, lp = compute_slope(n, a, b)
    us, up = compute_slope(n, c, d)
    assert ls[0] == us[0] == (0, 0)
    assert len(ls) == len(us) == n + 1 and ls[n] == us[n]

    # us が ls の上側にない場合は不可
    for x, y in us[1: n]:
        if y < evaluate(ls, x):
            output_ans(False)
    for x, y in ls[1: n]:
        if y > evaluate(us, x):
            output_ans(False)
    # 以下、us は ls の上側にあることが保証されている

    # 濃度の順番が同じならば可能
    if lp == up:
        output_ans(True)
    
    # 2つの容器で濃度が同一であるような中間状態が必要
    # us を越さないように ls 上の点を貪欲に通りながら n-1 本以下の線分で左下から右上に行けるか判定する
    for j in range(n - 1):
        x_c: Fraction = Fraction(ls[j][0], 1)
        y_c: Fraction = Fraction(ls[j][1], 1)
        for i in range(j, n - 1):
            if i >= j + 2:
                # この解法では離れた3点を2本で結べるかしか見ていないので
                break

            # us の上側を通らずに (x_c, y_c) から ls[i+2] まで引ければ OK
            x_t, y_t = ls[i + 2]
            
            # us の上側を通らないような傾きを求める
            # us と ls の上下関係は保証されているので、us における開区間 (x_c, x_t) 内の点のみを項考慮すればよい
            # (この区間の点と外側の点を結んだ線分によって初めて条件が破れる、といったケースはない)
            slopes: list[Fraction] = [Fraction(y - y_c, x - x_c) for x, y in us if x_c < x < x_t]
            if len(slopes) == 0:
                # 邪魔者はいない
                output_ans(True)

            # x=x_t まで進めればよいという条件のもとでの最大の傾き
            max_slope: Fraction = min(slopes)
            # c -> t への傾き
            slope_ct: Fraction = Fraction(y_t - y_c, x_t - x_c)

            if slope_ct <= max_slope:
                # (x_t, y_t) まで進める
                output_ans(True)
            else:
                # (x_t, y_t) までは進めないので可能な限り進む
                # その点は線分 ls[i+1] - ls[i+2] 上にある
                x_u, y_u = ls[i + 1]
                # (x_c, y_c) を通る傾き max_slope の直線と上記線分の交点を求める
                slope_ut: Fraction = Fraction(y_t - y_u, x_t - x_u)

                x_next = Fraction(max_slope * x_c - slope_ut * x_u - y_c + y_u, max_slope - slope_ut)
                y_next = max_slope * (x_next - x_c) + y_c
                assert x_u <= x_next < x_t
                assert y_next == slope_ut * (x_next - x_u) + y_u

                x_c, y_c = x_next, y_next

    output_ans(False)    
0