1046. Shortest Distance (20)

原创 2018年04月14日 22:35:56

1046. Shortest Distance (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:

5 1 2 4 14 9 3 1 3 2 5 4 1

Sample Output:

3 10 7


水题。
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
#define inf 10000000

int main()
{
	//ifstream fin("Text.txt");
	int N,i,j;
	cin >> N;
	vector<int>dis(N,0);
	int sum = 0,tmp;
	
	for (i = 1; i < N; i++)
	{
		cin >> tmp;
		sum += tmp;
		dis[i] = sum;
	}
	cin >> tmp;
	sum += tmp;
	int M,k;
	char c;
	cin >> M;
	for (k = 0; k < M; k++)
	{
		cin >> i  >> j;
		tmp = abs(dis[i - 1] - dis[j - 1]);
		cout << min(tmp, sum - tmp) << endl;

	}
	
	return 0;

}

1046. Shortest Distance (20)【贪心】——PAT (Advanced Level) Practise

题目信息1046. Shortest Distance (20)时间限制100 ms 内存限制65536 kB 代码长度限制16000 BThe task is really simple: gi...
  • xianyun2009
  • xianyun2009
  • 2015-12-06 21:15:03
  • 936

【PAT】1046. Shortest Distance (20)

1046. Shortest Distance (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Stand...
  • tiantangrenjian
  • tiantangrenjian
  • 2013-10-31 11:37:11
  • 2010

1046. Shortest Distance (20)-PAT甲级真题

1046. Shortest Distance (20) The task is really simple: given N exits on a highway which forms a s...
  • liuchuo
  • liuchuo
  • 2016-08-03 23:09:59
  • 188

PAT 1046. Shortest Distance (20)【卡的是超时】

1046. Shortest Distance (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standa...
  • u010069101
  • u010069101
  • 2014-02-27 22:59:38
  • 509

【C++】PAT(advanced level)1046. Shortest Distance (20)

1046. Shortest Distance (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B ...
  • artemisrj
  • artemisrj
  • 2014-03-03 11:19:15
  • 612

1046. Shortest Distance (20) 坑我

不能一个个加起来,不然测试点2超时 #define  _CRT_SECURE_NO_WARNINGS  #include #include #include #include #inclu...
  • huiyuanai222
  • huiyuanai222
  • 2015-07-17 21:48:18
  • 195

1046. Shortest Distance (20)(动态规划)

1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 1600...
  • shizheng163
  • shizheng163
  • 2016-02-18 22:48:39
  • 127

PAT 1046. Shortest Distance (20)

1046. Shortest Distance (20) 浙大PAT The task is really simple: given N exits on a highway which forms...
  • ZJFCLH
  • ZJFCLH
  • 2014-03-17 16:21:37
  • 857

PAT-A 1046. Shortest Distance (20)

PAT甲级 1046 Shortest Distance
  • Daniel960601
  • Daniel960601
  • 2017-02-12 20:03:22
  • 255

PAT_A 1046. Shortest Distance (20)

1046. Shortest Distance (20) 城市之间高速依次连成一个圈,给出相邻距离,求给定城市的最短距离。...
  • scylhy
  • scylhy
  • 2017-08-31 22:13:56
  • 95
收藏助手
不良信息举报
您举报文章:1046. Shortest Distance (20)
举报原因:
原因补充:

(最多只允许输入30个字)