ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1020. Rope

Test #5 WA, Python
Posted by Kitchen Tong 10 Dec 2013 01:29
The following is my code.
First come a special case where n is 1.
If n is not 1, more work should be done.

First, the nails are sorted based on their x-coordination.
The leftmost nail is accessed, and compared to the second leftmost nail.
With some boundary case, all the nails can be sorted along the clockwise or counter-clockwise direction.

Finally, I measure the distances between adjacent nails with pythagorean theorm.
Together with 2*pi*r, which accounts for the round edges, should be the answer.
What exactly does the code fail Test #5?


import math

def nail(n, r):
    return 2 * math.pi * r

def pyth(a, b):
    return math.sqrt(a**2 + b**2)

def perimeter(coord):
    ans = 0.0
    for i in range(len(coord)):
        ans += pyth((coord[i][0] - coord[i-1][0]), (coord[i][1] - coord[i-1][1]))
    return ans

if __name__ == "__main__":
    n, r = raw_input().split()
    n = int(n)
    r = float(r)
    coord = []
    for each in range(n):
        i, j = raw_input().split()
        coord += [(float(i), float(j))]

    if n == 1:
        print "%.2f" % (2 * math.pi * r)

    else:
        coord.sort()
        directed_coord = []
        directed_coord += [coord.pop(0)]
        if coord[0][1] > directed_coord[0][1]:
            clockwise = True
            directed_coord += [coord.pop(0)]
        else:
            clockwise = False
            directed_coord += [coord.pop(0)]

        if clockwise == True:
            for i in coord[:]:
                if i[1] > directed_coord[-1][1]:
                    coord.remove(i)
                    directed_coord += [i]
            for i in coord[:]:
                if i[1] >= coord[-1][1]:
                    coord.remove(i)
                    directed_coord += [i]
            coord.reverse()
            directed_coord += coord
        else:
            for i in coord[:]:
                if i[1] < directed_coord[-1][1]:
                    coord.remove(i)
                    directed_coord += [i]
            for i in coord[:]:
                if i[1] <= coord[-1][1]:
                    coord.remove(i)
                    directed_coord += [i]
            coord.reverse()
            directed_coord += coord

        ans = nail(n, r) + perimeter(directed_coord)
        print "%.2f" % ans