题意:给出一个环和结点之间的距离,求任意两结点之间的最近距离。如图:

思路:令数组dis[i]表示1号结点逆时针至i号结点的距离,初始化dis[1]=0,其他值在输入是确定,即

dis[i] 0 1 3 7 21
i 1 2 3 4 5

这样,起点start和终点end之间逆时针方向的距离即为 d=abs(dis[end]-dis[start]) ,注意要加绝对值,不然,比如起点4到终点2的距离是dis[2]-dis[4]=-6,就为负了。同时用total记录环的总距离,起点start和终点end之间逆时针方向的距离即为 total-d 。对于任意两结点u,v之间的最短距离,就是其顺时针和逆时针距离最较小值。

代码:

#include <cstdio>
#include <cstdlib>
#define MIN(x,y) (x)<(y)?(x):(y)
;
};
int main()
{
    ;//total表示总的距离
    scanf("%d",&n);
    ;i<=n;i++){
        scanf("%d",&d);
        total+=d;
        dis[i+]=d+dis[i];
    }
    int m,s,e;
    scanf("%d",&m);
    ;i<m;i++){
        scanf("%d%d",&s,&e);
        d=abs(dis[e]-dis[s]);
        printf("%d\n",MIN(d,total-d));
    }   return 0;
}

1046 Shortest Distance的更多相关文章

  1. PAT 1046 Shortest Distance

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  2. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  3. PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  4. PAT 1046 Shortest Distance[环形][比较]

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  5. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  6. pat 1046 Shortest Distance(20 分) (线段树)

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  7. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  8. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  9. [A] 1046 Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  10. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

随机推荐

  1. Node.js小白开路(一)-- 全局变量篇

    全局内容是有点类似于我们在浏览器编程的时候的window对象的,当时在node之中虽然我们编写的变量会自动的给出上下文环境(模块),但是全局变量的存在还是大大的帮助了我们编程时候的便捷性.我们可以在任 ...

  2. Could not publish server configuration for Tomcat v6.0 Server at localhost.错误问题解决

    经常在使用tomcat服务器的时候 总会发生一些莫名其妙的错误. 就像下面这个错误: 在配置文件中存在多个/MyWeb的配置,导致不能发布服务. 错误信息: Could not publish ser ...

  3. 团队作业:第五周 Alpha版本测试与发布

    团队:你吼辣么大声干什么嘛 Alpha版本测试报告:    bug: 修复的bug:     不能重现的bug:  无 产品设计,非bug: 在双人对战模式中,撞到墙壁会从对面的墙壁穿出,不会死 没能 ...

  4. IE6&IE7 bug

    IE6 Bugs 1 .不支持用样式设置 <abbr> 元素 2 .不支持以连字符和下划线开头的 class 和 ID 名 3 . <select> 元素总是出现在堆叠最上面, ...

  5. react:reducer-creator

    function createReducer(initialState, handlers) { return function reducer(state = initialState, actio ...

  6. html的meta总结,html标签中meta属性使用介绍(转)

    html的meta总结,html标签中meta属性使用介绍 2014年11月5日 5928次浏览 引子 之前的我的博客中对于meta有个介绍,例如:http://www.haorooms.com/po ...

  7. git pull VS git fetch&merge

    使用git fetch和git pull都可以更新远程仓库的代码到本地,但是它们之间还是有区别. git fetch  git fetch origin master git log -p maste ...

  8. MySQL 福利彩票业务 如何存储毫秒微秒

    朋友在做福利彩票业务,遇到一个存储毫秒微秒数据的需求,问我mysql里面有何解决方案.我脑中一搜索,以前没有关注到,于是去官网查看,找到11.3.6 Fractional Seconds in Tim ...

  9. [置顶] 【机器学习PAI实践三】雾霾成因分析

    一.背景 如果要人们评选当今最受关注话题的top10榜单,雾霾一定能够入选.如今走在北京街头,随处可见带着厚厚口罩的人在埋头前行,雾霾天气不光影响了人们的出行和娱乐,对于人们的健康也有很大危害.本文通 ...

  10. VSCode安装jshint插件报错

    Mac电脑上使用VSCode安装jshint插件时提示如下错误: Failed to load jshint library. Please install jshint in your worksp ...