您的位置:首页 > 其它

Codeforce Circle Line 环形数据操作

2014-04-30 16:19 169 查看
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations:

d1 is
the distance between the 1-st and the 2-nd
station;

d2 is
the distance between the 2-nd and the 3-rd
station;

...

dn - 1 is
the distance between the n - 1-th and the n-th
station;

dn is
the distance between the n-th and the 1-st
station.

The trains go along the circle line in both directions. Find the shortest distance between stations with numbers sand t.

Input

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.

Output

Print a single number — the length of the shortest path between stations number s and t.

Sample test(s)

input
4
2 3 4 9
1 3


output
5


操作环形数据,不小心就会出错。

大于周期数的时候,记得要模操作。

#include <iostream>
using namespace std;

void CircleLine()
{
	int n;
	cin>>n;
	int *A = new int
;

	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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: