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 D​1​​ D​2​​ ⋯ D​N​​, where D​i​​ is the distance between the i-th and the (-st exits, and D​N​​ 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 ;
}

最新文章

  1. AngularJS Resource:与 RESTful API 交互
  2. Spark 资源池简介
  3. Jetty官方文档翻译
  4. 一个简单的网页布局HTML+CSS
  5. projecteuler Problem 8 Largest product in a series
  6. quartz定时任务框架的使用
  7. E - Trees on the level
  8. java项目使用的DBhelper类
  9. hadoop2.2.0 MapReduce分区
  10. JS实现购物车特效
  11. JSP手动注入 全
  12. Centos运行Mysql因为内存不足进程被杀
  13. ng-book札记——HTTP
  14. JavaScript压缩工具JSA使用介绍
  15. linux的/etc/profile、~/.profile、~/.bashrc、~./bash_profile这几个配置文件
  16. linux卸载erlang
  17. c++ CreateProcess调用dos命令
  18. 使用 WebStorm IDE 调试 Pomelo 应用程序
  19. 普通用户使用docker命令
  20. centos7.6删除重新安装python和yum

热门文章

  1. SpringCloud学习笔记(六):Feign+Ribbon负载均衡
  2. 前端笔记:animate+easing用法(hexo next主题自定义动画)
  3. day 58 Django基础六之ORM中的锁和事务
  4. CCPC 2019 网络赛 HDU huntian oy (杜教筛)
  5. &lt;每日一题&gt;题目30:已知一个长度n的无序列表,元素均是数字,要求把所有间隔为d的组合找出来
  6. Traveling by Stagecoach /// 状压DP oj22914
  7. C# 字符串的日期比较
  8. golang 高效字符串拼接
  9. Python全栈开发:socket代码实例
  10. 【9.14NOIP模拟pj】wtaxi 题解