PAT A Shortest Distance (20)
来源:互联网 发布:快乐十分装机软件 编辑:程序博客网 时间:2024/05/01 17:59
题目
The task is really simple: given N exits on a highway which forms a simplecycle, you are supposed to tell the shortest distance between any pair ofexits.
Input Specification:
Each input file contains one test case. For each case, the first linecontains an integer N (in [3, 105]), followed by N integer distancesD1 D2 ... DN, where Di is thedistance between the i-th and the (i+1)-st exits, and DN is betweenthe 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 linesfollow, each contains a pair of exit numbers, provided that the exits arenumbered from 1 to N. It is guaranteed that the total round trip distance is nomore than 107.
Output Specification:
For each test case, print your results in M lines, each contains theshortest 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
保存各个点到原点的距离(不考虑环)。
任意两个点间的距离即为min(这个距离的差,总距离减这个距离)
代码:
#include <iostream>#include <algorithm>using namespace std;int main(){int n,*dis,tot_dis=0,temp;//数量,各个点到原点的距离(不考虑环),总距离,临时数据int i;cin>>n;dis=new int[n];for(i=0;i<n;i++)//输入距离,转换为到第一个点的总距离(不考虑环){dis[i]=tot_dis;scanf("%d",&temp);tot_dis+=temp;}int m;cin>>m;int c1,c2,d_dis;for(i=0;i<m;i++)//输入测试数据{scanf("%d",&c1);scanf("%d",&c2);if(c1>c2)swap(c1,c2);d_dis=dis[c2-1]-dis[c1-1];//计算差值if(tot_dis-d_dis<d_dis)//考虑环线d_dis=tot_dis-d_dis;printf("%d\n",d_dis);}delete [] dis;return 0;}
- PAT A Shortest Distance (20)
- PAT-A 1046. Shortest Distance (20)
- 【PAT】1046.Shortest Distance (20)
- 【PAT】1046. Shortest Distance (20)
- PAT 1046. Shortest Distance (20)
- PAT 1046. Shortest Distance (20)
- PAT 1046. Shortest Distance (20)
- pat 1046. Shortest Distance (20)
- PAT 1046. Shortest Distance (20)
- PAT A1046 Shortest Distance (20)
- 【PAT】A1046. Shortest Distance (20)
- 【PAT】1046. Shortest Distance (20)
- PAT A1046. Shortest Distance (20)
- PAT 1046. Shortest Distance (20)
- 1046. Shortest Distance (20)—PAT
- PAT (Advanced) 1046. Shortest Distance (20)
- PAT甲级.1046. Shortest Distance (20)
- 1046. Shortest Distance (20) PAT 甲级
- 使用CreateFile()打开COM10及以上串行口
- 狙击Google Glass?三星将推基于Tizen系统的Gear Glass
- WEB实时聊天 comet推技术
- 发短信
- MVC jsp+servlet+javabean 连接Mysql数据库测试demo
- PAT A Shortest Distance (20)
- ubuntu 调节屏幕亮度
- Framer:开源原型设计工具,巨头们的心头好
- csdn博客积分详解
- Windows环境利用Vmware7.1.3 搭建iPhone开发环境
- URAL 1635. Mnemonics and Palindromes(简单区间dp)
- 黑马程序员_IO_字符流
- Android-我自己的网络图片下载缓存类
- MFC中控件的TAB顺序