結果

問題 No.2438 Double Least Square
ユーザー chineristACchineristAC
提出日時 2023-08-19 01:33:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 3,254 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 78,644 KB
最終ジャッジ日時 2024-11-28 14:03:01
合計ジャッジ時間 8,004 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
56,476 KB
testcase_01 AC 42 ms
55,816 KB
testcase_02 AC 39 ms
56,176 KB
testcase_03 AC 39 ms
57,028 KB
testcase_04 AC 38 ms
55,568 KB
testcase_05 AC 37 ms
55,888 KB
testcase_06 AC 38 ms
55,568 KB
testcase_07 AC 38 ms
55,920 KB
testcase_08 AC 40 ms
56,684 KB
testcase_09 AC 38 ms
56,424 KB
testcase_10 AC 316 ms
78,312 KB
testcase_11 AC 309 ms
77,952 KB
testcase_12 AC 321 ms
78,172 KB
testcase_13 AC 311 ms
78,064 KB
testcase_14 AC 320 ms
78,180 KB
testcase_15 AC 327 ms
78,052 KB
testcase_16 AC 334 ms
78,304 KB
testcase_17 AC 322 ms
78,012 KB
testcase_18 AC 323 ms
78,308 KB
testcase_19 AC 327 ms
78,308 KB
testcase_20 AC 308 ms
78,172 KB
testcase_21 AC 298 ms
77,960 KB
testcase_22 AC 279 ms
78,024 KB
testcase_23 AC 312 ms
78,400 KB
testcase_24 AC 331 ms
77,912 KB
testcase_25 AC 286 ms
77,896 KB
testcase_26 AC 312 ms
78,140 KB
testcase_27 AC 350 ms
78,196 KB
testcase_28 AC 361 ms
78,436 KB
testcase_29 AC 415 ms
78,644 KB
testcase_30 AC 38 ms
55,748 KB
testcase_31 AC 37 ms
56,564 KB
testcase_32 AC 37 ms
56,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
from heapq import *
from math import acos

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def solve(N,H,point):

    def calc_min(a,b,c):
        if a == 0:
            return 0
        t = -b/(2*a)
        return a*t*t+b*t+c

    arg = []
    for i in range(N):
        x,y = point[i]
        r = ((y-H/2)**2 + x**2)**.5
        arg.append(acos((H/2-y)/r))
    
    idx = [i for i in range(N)]
    idx.sort(key=lambda i:arg[i])

    i_to_pos = [-1] * N
    for i in range(N):
        i_to_pos[idx[i]] = i

    X_idx = [i for i in range(N)]
    X_idx.sort(key=lambda i:point[i][0])

    ans = 10**100

    for i in range(N+1):
        up = [idx[j] for j in range(i,N)]
        down = [idx[j] for j in range(i)]

        fp,fq,fr = 0,0,0
        gp,gq,gr = 0,0,0

        for j in up:
            x,y = point[j]
            gp += x*x
            gq += 2 * x * (-y)
            gr += y*y
        for j in down:
            x,y = point[j]
            fp += x*x
            fq += 2 * x * (H-y)
            fr += (H-y) * (H-y)
        
        tmp = calc_min(fp,fq,fr) + calc_min(gp,gq,gr)

        ans = min(ans,tmp)

        for j in X_idx:
            x,y = point[j]
            if i <= i_to_pos[j]:
                gp -= x*x
                gq -= 2 * x * (-y)
                gr -= y*y

                fp += x*x
                fq += 2 * x * (H-y)
                fr += (H-y) * (H-y)
            else:
                gp += x*x
                gq += 2 * x * (-y)
                gr += y*y

                fp -= x*x
                fq -= 2 * x * (H-y)
                fr -= (H-y) * (H-y)

            tmp = calc_min(fp,fq,fr) + calc_min(gp,gq,gr)
            ans = min(ans,tmp)
    
    return ans
    

def brute(N,H,point):
    def calc(f,g):
        res = 0

        a,b,c = 0,0,0
        for i in f:
            x,y = point[i]
            a += x*x
            b += 2*x*(H-y)
            c += (H-y)*(H-y)
        if a!=0:
            t = (-b/(2*a))
            
            res += a * t * t + b * t + c

        a,b,c = 0,0,0
        for i in g:
            x,y = point[i]
            a += x*x
            b -= 2 * x * y
            c += y*y
        if a!=0:
            t = (-b/(2*a))
            
            res += a * t * t + b * t + c

        return res

    ans = 10**100

    for S in range(1<<N):
        f = [i for i in range(N) if S>>i & 1]
        g = [i for i in range(N) if S>>i & 1 == 0]
        ans = min(ans,calc(f,g))
        #print(calc(f,g),f,g)


    return ans

import random
while False:
    N = random.randint(3,10)
    H = random.randint(1,10)
    point = []
    for i in range(N):
        x,y = [random.randint(1,10) for _ in range(2)]
        point.append((x,y))
    
    #N,H = 3,10
    #point = [(4,7),(2,7),(4,5)]
    
    res = solve(N,H,point)
    exp = brute(N,H,point)

    if abs(res-exp) > 10**-2:
        print("WA")
        print(N)
        print(H)
        for x,y in point:
            print(x,y)
        
        print("res:",res)
        print("exp:",exp)
        exit()

    else:
        print("AC")

N = int(input())
H = int(input())
point = [tuple(mi()) for i in range(N)]

print(solve(N,H,point))
0