博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1046. Shortest Distance (20)
阅读量:4493 次
发布时间:2019-06-08

本文共 1669 字,大约阅读时间需要 5 分钟。

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 D1D2 ... 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 931 32 54 1
Sample Output:
3107
来源: <>
 
 
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdio.h>
  4. #pragma warning(disable:4996)
  5. using namespace std;
  6. vector<int> num;
  7. int main(void) {
  8. int n,sum = 0;
  9. cin >> n;
  10. for (int i = 0; i < n; i++) {
  11. int roadtemp;
  12. scanf("%d", &roadtemp);
  13. sum += roadtemp;
  14. num.push_back(sum);
  15. }
  16. int m;
  17. cin >> m;
  18. for (int i = 0; i < m; i++) {
  19. int start, end, change;
  20. // cin >> start >> end;
  21. scanf("%d %d", &start, &end);
  22. if (start > end) {
  23. change = end;
  24. end = start;
  25. start = change;
  26. }
  27. int road = 0;
  28. if (start == 1) {
  29. road = num[end-2];
  30. }
  31. else {
  32. road = num[end-2] - num[start-2];
  33. }
  34. if (road > num[n-1] - road)
  35. road = num[n-1] - road;
  36. //cout << road << endl;
  37. printf("%d\n", road);
  38. }
  39. return 0;
  40. }

转载于:https://www.cnblogs.com/zzandliz/p/5023159.html

你可能感兴趣的文章
STL 之map解决 Message Flood(原字典树问题)
查看>>
Spring Maven——pom.xml及settings.xml配置
查看>>
软件测试基本知识
查看>>
nodejs项目windows下开机自启动
查看>>
1136 - Division by 3
查看>>
1_基本语法之关键字和保留字_标识符_注释
查看>>
Git知识总览(二) git常用命令概览
查看>>
新的移动端框架
查看>>
最近的总结
查看>>
NHiberante的优缺点
查看>>
SQLite 入门教程(二)创建、修改、删除表
查看>>
gzip: stdin: unexpected end of file tar: Unexpected EOF in archive
查看>>
文件系统中的权限含义
查看>>
Crossin 8-3;8-4
查看>>
[编织消息框架][JAVA核心技术]动态代理应用5-javassist
查看>>
关于X系统
查看>>
OC 观察者模式(通知中心,KVO)
查看>>
【BZOJ2134】单选错位 概率DP
查看>>
git 冲突解决办法
查看>>
lintcode 二叉树后序遍历
查看>>