Codeforces: A. Circle Line


The problem is from codeforces: http://www.codeforces.com/problemset/problem/278/A

278A Codeforces: A. Circle Line beginner codeforces implementation math programming languages python

The problem can be simplified as the following: in a double-direction link, given two nodes, compute the shortest distance between them.

There are only two distances, forward and backward.  Simple as that.

#!/usr/bin/env python

n = int(raw_input())
d = map(int, raw_input().split())
s, t = map(int, raw_input().split())

if s > t:
    s, t = t, s

d1 = sum(d[s-1:t-1])
d2 = sum(d[t-1:]) + sum(d[:s-1])

print min(d1, d2)

Same idea can be rewritten in other ways.

n = input()
d = list(map(int,raw_input().split()))
s,t =map(lambda x:int(x)-1,raw_input().split())
p1,p2=0,0
if s>t:s,t=t,s
ans = 0
for i in range(s,t):
    ans+=d[i]
print(min(ans,sum(d)-ans))

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
240 words
Last Post: VBScript at Window Script Host, Check Folder Exists
Next Post: Using Dictionary Object in VBScript

The Permanent URL is: Codeforces: A. Circle Line

Leave a Reply