码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforce Circle Line 环形数据操作

时间:2014-07-22 23:05:14      阅读:302      评论:0      收藏:0      [点我收藏+]

标签: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:

  • 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[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

Codeforce Circle Line 环形数据操作

标签:style   blog   color   os   数据   width   

原文地址:http://blog.csdn.net/kenden23/article/details/24798993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!