1046. Shortest Distance (20)
2016-07-10 19:46
591 查看
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 <stdio.h> #include <math.h> int main() { int N,i,temp,p1,p2; int a[100010]={0},sum; scanf("%d",&N); for(i=2;i<N+1;i++) //a[i]存储从1到达i节点的成本 { scanf("%d",&temp); a[i] = a[i-1]+temp; } scanf("%d",&temp); sum = a[--i] +temp; //sum存储整个cycle的长 scanf("%d",&N); for(i=0;i<N;i++) { scanf("%d%d",&p1,&p2); temp = abs(a[p2] - a[p1]); if( temp >= sum-temp) { printf("%d\n",sum-temp); } else { printf("%d\n",temp); } } return 0; }
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中进制知识汇总
- C语言判断一个数是否是2的幂次方或4的幂次方
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- C语言实现选择排序、冒泡排序和快速排序的代码示例