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.

Input Specification:

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 ⋯ D**N, where D**i is the distance between the i-th and the (i+1)-st exits, and D**N 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.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output:
3
10
7
思路
  • 如果设起点、终点分别为x,y ,顺时针的距离就是dis(x,y),逆时针的距离就是dis(y,x),比较就好了。如果是对每一组测试点都手动模拟的话会TLE
  • 所以需要优化,这题的本质是区间和的比较,我们可以另开一个数组记录区间和,那么每次查询的时候只要直接相减就好了
代码
#include<bits/stdc++.h>
using namespace std;
int a[100010] = {0};
int length[100010] = {0};
int main()
{
int n;
scanf("%d", &n);
int sum = 0;
for(int i=1;i<=n;i++)
{
scanf("%d", &a[i]);
sum += a[i];
length[i] = sum;
}
int m;
scanf("%d", &m);
int l, r;
int t; //暂时存储距离
while(m--)
{
scanf("%d %d", &l, &r);
if(l > r) swap(l, r);
t = length[r-1] - length[l-1];
printf("%d\n", min(t, sum - t));
}
return 0;
}
引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424

PTA(Advanced Level)1046.Shortest Distance的更多相关文章

  1. PAT (Advanced Level) 1046. Shortest Distance (20)

    处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

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

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

  3. PAT 1046 Shortest Distance

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

  4. 1046 Shortest Distance (20 分)

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

  5. 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 ...

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

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

  7. 1046 Shortest Distance (20 分)

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

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

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

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

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

随机推荐

  1. java怎样实现重载一个方法

    重载(重新载选方法): java允许在一个类中,存在多个方法拥有相同的名字,但在名字相同的同时,必须有不同的参数,这就是重载,编译器会根据实际情况挑选出正确的方法,如果编译器找不到匹配的参数或者找出多 ...

  2. Ubuntu完全删除nginx

    1.删除nginx,-purge包括配置文件 sudo apt-get --purge remove nginx 2.移除全部不使用的软件包 sudo apt-get autoremove 3.罗列出 ...

  3. Python3学习笔记(十八):文件上传和下载

    文件上传 以人人网上传头像为例,用Fiddler抓取的上传头像接口报文如下 上传头像图片代码: import requests upload_url = 'http://upload.renren.c ...

  4. Topics(主题模式)

    引言 topic exchange和direct exchange类似,都是通过routing key和binding key进行匹配,不同的是topic exchange可以为routing key ...

  5. Android_(自动化)获取手机存储卡的容量

    手机上的存储卡是可以随时插拔的,每次插拔时会像操作系统总发送Action广播事件. 使用StatFs文件系统来获取MicroSD存储卡的剩余容量,在使用前先判断是否插入了存储卡,如果不存在则不于计算 ...

  6. 【Nginx】 linux环境下安装nginx步骤

    开始前,请确认gcc g++开发类库是否装好,默认已经安装. centos平台编译环境使用如下指令 安装make: yum -y install gcc automake autoconf libto ...

  7. 阿里云maven镜像

    <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...

  8. 【python / mxnet / gluoncv / jupyter notebook】变换场景的同一行人多重识别

    程序环境为高性能集群:CPU:Intel Xeon Gold 6140 Processor * 2(共36核心)内存:512GB RAMGPU:Tesla P100-PCIE-16GB * 2   数 ...

  9. 【黑马Javaweb】1.1Junit单元测试

    黑马第一天学习 今日内容 Junit单元测试: 测试分类: Junit使用:白盒测试 今日内容 1.1Junit单元测试 1.2.反射 1.3.注解 Junit单元测试: 测试分类: 1.黑盒测试:不 ...

  10. selenium爬虫使用

    1. 网页的打开 from selenium import webdriver import time driver = webdriver.Chrome(executable_path=r" ...