n个节点围成一个环,每个节点之间的距离已知。输入n并给出n个节点的距离,输入m组节点编号(两个),求这两个节点编号间的最短距离。

1 建立dis[]数组,记录V1点到每一个点的顺时针距离,sum计算环的总距离。

2 输入m组节点编号,如果左边的值大于右边的值,则使用swap()函数将其交换。

3 计算temp=dis[right-1]-dis[left-1],比较temp和sum-temp,输出最小的即为最短距离。

注1:swap()函数即将两个变量交换:swap(a,b);

注2:#include<algorithm>:包含swao()和min()两个函数的函数库,当要使用这两个函数的时候需要写在开头。

 #include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=;
int dis[MAXN];
int main(){
int sum=,tem,query,n,left, right;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&tem);
sum+=tem;
dis[i]=sum;
}
scanf("%d",&query);
for(int i=;i<query;i++){
scanf("%d%d",&left,&right);
if(left>right) swap(left,right);//交换left和right
int temp=dis[right-]-dis[left-];
printf("%d\n", min(temp,sum-temp));
} return ;
}

------------恢复内容结束------------

A1046的更多相关文章

  1. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  2. A1046. Shortest Distance

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

  3. A1046. Shortest Distance(20)

    17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...

  4. A1046 Shortest Distance (20)(20 分)

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

  5. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  6. PAT甲级——A1046 Shortest Distance

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

  7. PAT/简单模拟习题集(二)

    B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...

  8. 1046 Shortest Distance (20 分)

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

  9. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

随机推荐

  1. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jeewx.weixin_account_user_relation' doesn't exist

    [INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------- ...

  2. eslint+prettier 的 VSCode配置项

    { "files.autoSave": "off", "editor.fontSize": 12, "terminal.integ ...

  3. python中的装饰器练习

    一:编写装饰器,为多个函数加上认证的功能(用户的账号密码) 要求登录成功一次,后续的函数都无需输入用户名和密码FLAG=False#此时还未登录 全局变量 写这个步骤的意义在于:方便 知道已经登录成功 ...

  4. 如何面对这个残酷的世界?——Java模拟

    1,问题引入: 房间里有100个人,每人都有100元钱,他们在玩一个游戏.每轮游戏中,每个人都要拿出一元钱随机给另一个人,最后这100个人的财富分布是怎样的? 2,问题思考: 今天有幸看到这道题目,起 ...

  5. zimg 服务器配置文件

    --zimg server config --server config --是否后台运行 is_daemon = --绑定IP ip = '0.0.0.0' --端口 port = --运行线程数, ...

  6. python每日一练:0012题

    第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好 ...

  7. Golang基本类型整理

    总是用的用的就模糊了,不知道基本的类型有哪些,看来要反反复复弄几次. Golang基本类型整理 基本类型以及定义变量需要注意的 对于基本类型的介绍,感觉这个博客讲的比较透彻,基本上都是从源码的角度来入 ...

  8. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  9. Java常用的日志框架

    1.Java常用日志框架对比 https://www.jianshu.com/p/bbbdcb30bba8 2.Log4j,Log4j2,Logback日志框架性能对比 https://bbs.hua ...

  10. Java8---函数式编程-示例

    // Java8函数式编程示例—(Predicate.Stream.Optional) https://blog.csdn.net/weixin_41950473/article/details/84 ...