PAT甲级——A1046 Shortest Distance
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]), followed by N integer distances D1 D2 ⋯ DN, where Di is the distance between the i-th and the (-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 (≤), 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 1.
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 <vector>
using namespace std;
int N, M;
int main()
{
cin >> N;
int num, a, b;
vector<int>sum(N + , );
for (int i = ; i <= N; ++i)
{
cin >> num;
if (i == N)
sum[] = sum[N] + num;
else
sum[i + ] = sum[i] + num;
}
cin >> M;
for (int i = ; i < M; ++i)
{
cin >> a >> b;
if (a > b)
swap(a, b);
int d1 = sum[b] - sum[a];
int d2 = sum[] - sum[b] + sum[a] - sum[];
cout << (d1 < d2 ? d1 : d2) << endl;
}
return ;
}
最新文章
- AngularJS Resource:与 RESTful API 交互
- Spark 资源池简介
- Jetty官方文档翻译
- 一个简单的网页布局HTML+CSS
- projecteuler Problem 8 Largest product in a series
- quartz定时任务框架的使用
- E - Trees on the level
- java项目使用的DBhelper类
- hadoop2.2.0 MapReduce分区
- JS实现购物车特效
- JSP手动注入 全
- Centos运行Mysql因为内存不足进程被杀
- ng-book札记——HTTP
- JavaScript压缩工具JSA使用介绍
- linux的/etc/profile、~/.profile、~/.bashrc、~./bash_profile这几个配置文件
- linux卸载erlang
- c++ CreateProcess调用dos命令
- 使用 WebStorm IDE 调试 Pomelo 应用程序
- 普通用户使用docker命令
- centos7.6删除重新安装python和yum
热门文章
- SpringCloud学习笔记(六):Feign+Ribbon负载均衡
- 前端笔记:animate+easing用法(hexo next主题自定义动画)
- day 58 Django基础六之ORM中的锁和事务
- CCPC 2019 网络赛 HDU huntian oy (杜教筛)
- <;每日一题>;题目30:已知一个长度n的无序列表,元素均是数字,要求把所有间隔为d的组合找出来
- Traveling by Stagecoach /// 状压DP oj22914
- C# 字符串的日期比较
- golang 高效字符串拼接
- Python全栈开发:socket代码实例
- 【9.14NOIP模拟pj】wtaxi 题解