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

思路:令数组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. Java容器_01

    1. HashTable 和 HashMap 区别? 2.

  2. int(3)与int(11)的区别

    注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的.其实当我们在选择使用int的类型的时候 ...

  3. 智课雅思词汇---二十四、形容词后缀-al-ial-ar-ary-ic-id-ish-ile-ine-oid-ory

    智课雅思词汇---二十四.形容词后缀-al-ial-ar-ary-ic-id-ish-ile-ine-oid-ory 一.总结 一句话总结: 1.形容词后缀-al? autumnal 英 [ɔː'tʌ ...

  4. c#实现验证某个IP地址是否能ping通

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  5. 【sparkStreaming】将DStream保存在MySQL

    package SparkDemo import java.sql.{Connection, DriverManager, PreparedStatement} import org.apache.s ...

  6. 服务器证书安装配置指南(IIS7.0)

    一.  生成证书请求 1.    进入IIS控制台   进入IIS控制台,并选择服务器的服务器证书设置选项.  2.    添加证书请求   进入服务器证书配置页面,并选择“创建证书申请”  3.   ...

  7. Activity的基本概念与Activity的生命周期

    一.Activity的基本概念 Activity是Android的四大组件之一,它是一种可以包含用户界面的组件,主要用于和用户进行交互,比如打电话,照相,发送邮件,或者显示一个地图!Activity用 ...

  8. js 取任意两个数之间的随机整数

    function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Mat ...

  9. gethostbyname()函数

    gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件    #include <netdb.h>    #include <sys/socket.h> ...

  10. js将json数据动态生成表格

    今天开发中遇到需要展示动态数据的问题, 具体要求是后端传来的json字符串,要在前端页面以table表格的形式展示, 其实没啥难的,就是拼接table标签,纯属体力活,于是自己写了个呆萌,保存起来,以 ...