torus711 のアレ

主に競技プログラミングの問題について書きます.PC 以外だと数式が表示されないかもしれないです

Codeforces #170, Division 2, A : Circle Line

概要

電車の環状線に於ける各駅間の距離の情報が与えられる。
ある駅から別のある駅へ行く場合の移動距離の最小値を求めよ。

解法

外回りの距離と内回りの距離の小さいほうが答えです。
片方求めれば、他方は全体から引くことで求められますので、境界( n ⇔ 1 )をまたぐ必要はありません。

コード

typedef vector<int> VI;

#define FOR( v, c ) for ( auto &v : c )
#define ALL( c ) (c).begin(), (c).end()

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	int n;
	cin >> n;

	VI ds( n );
	FOR( d, ds )
	{
		cin >> d;
	}

	int s, t;
	cin >> s >> t;

	s--;
	t--;

	int sum = accumulate( ALL( ds ), 0 ), dist = accumulate( ds.begin() + min( s, t ), ds.begin() + max( s, t ), 0 );
	cout << min( dist, sum - dist ) << endl;
	
	return 0;
}