• 题目描述
    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.
    一个包含N个节点的环,求任意两个节点之间的最短距离.
  • 输入
    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.
    每个测试用例:

    • 第一行包含一个整数N,同行空格间隔,是N个整数,代表环上N个相邻节点之间的距离.
    • 第二行也是一个整数M,表示下面要输入几个节点对
    • M个节点对.
  • 输出
    For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
    两节点之间的最短距离(顺时针,逆时针)
  • 样例输入
    5 1 2 4 14 9
    3
    1 3
    2 5
    4 1
  • 样例输出
    3
    10
    7

a[N+1],存储相邻节点之间的距离,从a[1]开始,a[1]:节点1到节点2的距离.a[i]:distance(i, i+1)
这个简单图是一个环,所以任意两节点之间之存在两条路径,顺时针路径和逆时针路径,
1.计算出环总长度sum;
2.计算任意节点之间的顺时针距离dist_1;
3.逆时针距离 = sum - dist_1 = dist_2;
4.比较dist_1和dist_2的大小;

#include <stdio.h>
int main() {int N;while (scanf("%d",&N) != EOF){int a[N+1];int sum = 0;for (int i = 1; i < N+1; i++) {scanf("%d",&a[i]);sum += a[i];}int m;scanf("%d",&m);while(m--) {int c, d,dist_1=0, dist_2;scanf("%d %d",&c, &d);int temp;if (c > d) {temp = d;d = c;c = temp;}for(int i = c; i < d; i++)dist_1 += a[i];dist_2 = sum - dist_1;if(dist_1 < dist_2) printf("%d\n",dist_1);else printf("%d\n",dist_2);}}return 0;
}

解法二:上个解法时间复杂度较高.
方法二:不同地方是用dis存储从起始点到第i+1点的距离
顺时针距离 = dis[r-1] - dis[l-1]
逆时针距离 = sum - 顺时针距离

#include <iostream>
#include <vector>
using namespace std;
int main() {int n;cin >> n;vector<int> dis(n+1);int sum = 0, l, r, m;for (int i = 1; i <= n; i++) {int temp;cin >>temp;sum += temp;dis[i] = sum;}cin >> m;while(m--) {scanf("%d %d",&l, &r);if (l > r) swap(l, r);int dist = dis[r - 1] - dis[l -1];cout <<min(dist, sum - dist) <<endl;}return 0;
}

C/C++[Shortest Distance]相关推荐

  1. LeetCode 613. Shortest Distance in a Line --SQL

    LeetCode 613. Shortest Distance in a Line --SQL LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部 ...

  2. Leetcode PHP题解--D49 821. Shortest Distance to a Character

    D49 821. Shortest Distance to a Character 题目链接 821. Shortest Distance to a Character 题目分析 给定一个字符串s和一 ...

  3. Shortest Distance from All Buildings

    Shortest Distance from All Buildings 题目链接:https://leetcode.com/problems... 这道题要求最短的距离,一般这种要求可以到的地方的距 ...

  4. PAT甲级1046 Shortest Distance:[C++题解]前缀和

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 用前缀和快速求出一段的和.注意求两段,取最小值. ac代码 #include<bits/stdc++.h> using ...

  5. 【测试点2超时问题】1046 Shortest Distance (20 分)_21行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 The task is really simple: given N exits on a highway which forms ...

  6. 【Leetcode_easy】821. Shortest Distance to a Character

    problem 821. Shortest Distance to a Character 参考 1. Leetcode_easy_821. Shortest Distance to a Charac ...

  7. [PAT A1046]Shortest Distance

    题目 The task is really simple: given N exits on a highway which forms a simple cycle, you are suppose ...

  8. LeetCode Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  9. 【PAT甲级 环最短距离】1046 Shortest Distance (20 分) Java、C++

    题目 这题是给你一个环,让你计算两点之间最短距离. 环的任意两点就两条路,只要算出环长和任意一条路的大小,另一条就出来了 时间复杂度O(N) 提前计算前缀长度和即可 题解 C++ 全部测试点通过 #i ...

  10. (C++)1046 Shortest Distance

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...

最新文章

  1. iOS病毒XcodeGhost批量检测工具,开源Github(检测ipa文件)
  2. python教程是什么课文_新手快速入门Python必看这篇文章
  3. VS2017 安装程序清单签名验证失败
  4. python 整合excel_Python将多个excel文件合并为一个文件
  5. 【学习心得】当程序员思路被打断
  6. Jquery中正确使用trim方法以及避免遇到坑
  7. Python nose单元测试框架的安装与使用
  8. opencv中的椭圆拟合
  9. Java判断两个Date是不是同一天
  10. python技术文档_Python技术文档最佳实践
  11. 京东在港上市,尘埃落定!
  12. 方法Method(Java)
  13. Centos 6.3中安装KVM
  14. Java中String类的常用方法
  15. 软件工程专业英语专用名词翻译
  16. gis插入的文本怎么搞成两行_PPT脱白教程09期手把手带你了解“文本框”(03)...
  17. 个人号微信二次开发,微信ipad协议
  18. 哈罗单车再获10亿融资,摩拜、ofo难合并!
  19. 猫咪藏在哪个房间python作业_猫咪生气躲进房间,众人找到后,猫咪一脸疑问:听说你们在找我...
  20. 微信小程序开发学习—Day1

热门文章

  1. c语言和测绘程序设计,测绘程序设计(C语言版)
  2. 平均聚类系数_聚类方法排除CPU用量误报警
  3. ue4缓存位置怎么改_[UE4]动态液体材质浅谈
  4. Spring Could+Ant Design Pro表格数据加载
  5. java块语句_Java™ 教程(表达式、语句和块)
  6. sun键盘没有stop键_【转帖】SUN基础知识
  7. Error running ‘Tomcat x.x.xx‘: Address localhost:xxxx is already in use
  8. element-ui中分页 跳转页面时出现刷新网站的问题
  9. 再次总结一下压缩与解压缩
  10. vue插件开发练习--实用弹窗