結果

問題 No.2438 Double Least Square
ユーザー chineristAC
提出日時 2023-08-19 01:00:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,590 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 78,596 KB
最終ジャッジ日時 2024-11-28 13:16:16
合計ジャッジ時間 5,607 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 WA * 12
権限があれば一括ダウンロードができます

ソースコード

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(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

    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])

    

    ans = calc([i for i in range(N)],[])
    for i in range(N):
        ans = min(ans,calc([idx[j] for j in range(i+1,N)],[idx[j] for j in range(i+1)]))
        ans = min(ans,calc([idx[j] for j in range(i+1)],[idx[j] for j in range(i+1,N)]))

    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))


    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,5
    point = [(7,5),(8,7),(8,2)]
    
    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