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
output
操作环形数据,不小心就会出错。
大于周期数的时候,记得要模操作。
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; }
相关文章推荐
- codeforce : C. Line(扩展欧几里得)
- 扩展欧几里得,逆元初识(poj 1061+codeforce 7C line+hdu 1576 A/B)
- 【CodeForce #239 Div2】408A——Line to Cashier
- CodeForce Round 219 Div2 E Watching Fireworks is Fun 单调队列DP
- Codeforce - 361 - B. Levko and Permutation
- codeforce A. Design Tutorial: Learn from Math
- codeforce 439D 二分或者三分
- codeforce Looksery Cup 2015 H Degenerate Matrix
- Codeforce 546 B. Soldier and Badges
- codeforce比赛规则介绍(转)
- codeforce round 399# C
- Codeforce - 402C - Searching for Graph
- codeforce 493B. Vasya and Wrestling(多重条件比较)
- codeforce 329B Biridian Forest(bfs)
- Codeforce 721C(DAG上dp)
- MVC - Code First Migration Command line
- codeforce 7A. Kalevitch and Chess(粉刷)
- codeforceB. Mishka and trip
- codeforce 55 D. Beautiful numbers(数位dp,好题)
- codeforce 375_2_b_c