标签:style blog color os 数据 width
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations:
...
The trains go along the circle line in both directions. Find the shortest distance between stations with numbers sand t.
The first line contains integer n (3?≤?n?≤?100) — the number of stations on the circle line. The second line containsn integers d1,?d2,?...,?dn (1?≤?di?≤?100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1?≤?s,?t?≤?n) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same.
The numbers in the lines are separated by single spaces.
Print a single number — the length of the shortest path between stations number s and t.
4 2 3 4 9 1 3
5
大于周期数的时候,记得要模操作。
#include <iostream> using namespace std; void CircleLine() { int n; cin>>n; int *A = new int[n]; for (int i = 0; i < n; i++) { cin>>A[i]; } int a, b; cin>>a>>b; if (a > b) swap(a, b); int dist1 = 0; for (int i = a-1; i < b-1; i++) { dist1 += A[i]; } int dist2 = 0; for (int i = b-1; i < n+a-1; i++) { dist2 += A[i%n]; } dist1 < dist2? cout<<dist1 : cout<<dist2; delete [] A; }
Codeforce Circle Line 环形数据操作,码迷,mamicode.com
标签:style blog color os 数据 width
原文地址:http://blog.csdn.net/kenden23/article/details/24798993