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. SIRIM上海,http://www.sirim-global.com

    SIRIM上海 http://www.sirim-global.com

  2. [ARC101E]Ribbons on Tree(容斥,dp)

    Description 给定一棵有 \(n\) 个节点的树,满足 \(n\) 为偶数.初始时,每条边都为白色. 现在请你将这些点两两配对成 \(\frac{n}{2}\) 个无序点对.每个点对之间的的 ...

  3. webpack打包错误 ERROR in multi ./src/main.js ./dist/bundle.js

    webpack打包错误 ERROR in multi ./src/main.js ./dist/bundle.js:https://www.jianshu.com/p/a55fb5bf75e1

  4. C++中操作符重载的概念

    1,下面的复数解决方案是否可行? 1,代码示例: class Comples { public: int a; int b; }; int main() { Complex c1 = {, }; Co ...

  5. so easy(并查集+unordered_map)

    There are nn points in an array with index from 11 to nn, and there are two operations to those poin ...

  6. kernel编译

    Linux内核编译与安装 Linux内核介绍 Linux内核是一个用C语言写成的,符合POSIX标准的类Unix操作系统.内核是操作系统中最基本的一部分,提供了众多应用程序访问计算机硬件的机制.Lin ...

  7. linux-导入python自定义模块的使用方法

    #!/usr/bin/python # -*- coding:utf -8 -*- import os import sys sys.path.append("/h/s/compare_f& ...

  8. org.apache.httpcomponents:httpclient 工具类

    基于httpclient 版本4.4.1 因为http连接需要三次握手,在需要频繁调用时浪费资源和时间 故采用连接池的方式连接 根据实际需要更改  连接池最大连接数.路由最大连接数 另一个需要注意的是 ...

  9. TMS320F28335——下载程序到flash中

    一.让CCS软件支持Flash烧写 添加F28335.cmd文件 如图屏蔽掉25335_RAM_lnk.cmd 2.支持从Flash中拷贝文件到RAM中 添加DSP2832x_MemCopy.c 在主 ...

  10. 为什么要用消息队列 及 自己如何设计一个mq架构

    1. 解耦:如左图, 系统a因为业务需求需要调用系统b,后续因为业务需求可能需要改代码调用系统c,甚至还要考虑被调用的系统挂了访问超时的问题.耦合性太高! 如右图, 系统a产生一条数据发送到消息队列里 ...