HackerRank - "Stars"

Not hard, but with some amount of coding. Brutal-force would work: iterate each unique pair of points. And I used "y=ax+b" to check each point which side it resides.

import math

EPSI = 0.00000001

# y = ax + b
def calcAB(p0, p1):
    if (p0[1] == p1[1]):
        return 0, p0[1]
    a = (p0[1] - p1[1])/(p0[0] - p1[0])
    b = p0[1] - a * (p0[0])
    return a, b

def splitPV(coords, ws, xs, wpp, ni, nj):
    left = right = 0
    for i in range(len(ws)):
        if (i == ni or i == nj): continue
        p = coords[i]
        w = ws[i]
        if (p[0] < xs):
            left += w
        elif (p[0] > xs):
            right += w

    if (left < right):
        left += wpp
    else:
        right += wpp
    return abs(left - right), min(left, right)    

def splitP(coords, vs, ca, cb, wpp, ni, nj):
    left = right = 0
    for i in range(len(ws)):
        if (i == ni or i == nj): continue
        p = coords[i]
        w = ws[i]
        func_v = p[0] * ca + cb
        if (func_v < p[1]):
            left += w
        elif (func_v > p[1]):
            right += w

    if (left < right):
        left += wpp
    else:
        right += wpp
    return abs(left - right), min(left, right)

# Get Input
coords = []
ws = []
n = int(input())
for _ in range(n):
    x, y, w = map(int, input().split())
    coords.append([x, y])
    ws.append(w)

# Brutal Force
diff = 99999999999999
minw = 0
plen = len(ws)
for i in range(plen):
    for j in range(i + 1, plen):
        p0 = coords[i]
        p1 = coords[j]
        wpp = ws[i] + ws[j]

        min_diff = minw = 0
        # Case 1: vertical
        if (abs(p0[0]-p1[0]) <= EPSI):
            xs = p0[0]
            min_diff, minw = splitPV(coords, ws, xs, wpp, i, j)
        # Case 2: any other
        else:
            a, b = calcAB(p0, p1)
            min_diff, minw = splitP(coords, ws, a, b, wpp, i, j)

        if (min_diff < diff):
            diff = min_diff
            grp = minw

print (grp)
时间: 2024-08-10 23:26:23

HackerRank - "Stars"的相关文章

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

POJ 2352 Stars(树状数组)

                                                                 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44309   Accepted: 19236 Description Astronomers often examine star maps where stars are represented by points on a pla

HDU 5533 Dancing Stars on Me 计算几何瞎暴力

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1184    Accepted Submission(s): 651 Problem Description The sky was brushed clean by the wind and the stars were cold in a b

Bonetrousle HackerRank 数学 + 思维题

https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n. 首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi.最大值,后b个数相加,记为mx 注意到一个东西:如果mi <= n <= mx.那么是绝对可行的.因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解. 所以首先从mi开始枚举,

hackerrank maxsum mod

https://www.hackerrank.com/challenges/maximise-sum/submissions/code/12028158 hackerrank 子数组和模m的最大值 时间复杂度nlgn, 主要是证明一点,presum[i]-presum[j] 对于0<j<i的j,最大的是第一个大于presum[i]的presum[j] 证明如下 Quro的讨论,感觉还是证明的过程不够细致 http://www.quora.com/What-is-the-logic-used-i

房价预测(HackerRank)

从今天开始要多做一些关于机器学习方面的竞赛题目,题目来源主要是Hackerrank和Kaggle.链接如下 Hackerrank:https://www.hackerrank.com/ Kaggle:https://www.kaggle.com/ 在Hackerrank中提交源代码,这就使得很多库都需要自己写,限制比较多.而Kaggle只需要提交数据,所以随便怎么搞都行.现在来讲第一道题,房价预测,这是Andrew Ng课程里的比较经典的例子.题目描述如下 题目:https://www.hack

ZOJ 2352 Stars

Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22870 Accepted: 9967 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be

【POJ2482】【线段树】Stars in Your Window

Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroo

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given