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. luogu【模板】线性筛素数 (Miller-Rabin素数测试模板)

    这个感觉还是挺好理解的,就是复杂度证明看不懂~ Code: #include <cstdio> #include <algorithm> #include <cstrin ...

  2. 实现同时将一批.bmp文件转换成.mat格式

    %% 功能:实现同时对一批.bmp文件的转换成.mat格式PicFormat = {'*.bmp','Bitmap image (*.bmp)';... '*.jpg','JPEG image (*. ...

  3. vue-cli3项目打包后,在自己搭的服务器上访问打包好的页面空白,处理方法

    我用vue开发的一个小项目,项目开发出雏形,想打包一下放测试,但是我想放测试前自己检查一下经过npm run build打包生成的项目,在本地服务器能不能正常跑,于是,经过网上大哥的帮忙,写了个简易的 ...

  4. LightGBM GPU python版本安装

    失败的安装尝试 1.官方Guide https://lightgbm.readthedocs.io/en/latest/GPU-Windows.html 生成在windows下可执行的exe程序,但是 ...

  5. python3笔记二十四:Mysql数据库操作命令

    一:学习内容 Mysql操作命令:启动服务.停止服务.连接数据库.退出数据库.查看版本.显示当前时间.远程连接 数据库操作命令:创建数据库.删除数据库.切换数据库.查看当前选择的数据库 表操作命令:查 ...

  6. html上传图片后,在页面显示上传的图片

    html上传图片后,在页面显示上传的图片 1.html <form class="container" enctype="multipart/form-data&q ...

  7. Python中列表的使用

    python中的列表与java中的数组非常类似,但使用方法比java中数组简单很多,python中的数据类型不需要显示声明,但在使用时必须赋值,列表元素下标从0开始 初始化列表(初始化一个包含五个元素 ...

  8. koa 路由模块化(一)

    1.项目目录 2.入口文件 根目录/app.js /** * koa 路由模块化 */ const Koa = require('koa'); const router = require('koa- ...

  9. LC 683. K Empty Slots 【lock,hard】

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  10. NGUI中UILabel使用url标签的一个bug

    在NGUI里,UILabel控件可以支持一些简单功能的标签,使文本显示更丰富及实现类似超链接的功能.但是在使用的时候发现了NGUI3.5.9版本里存在着一个bug.不过还好修复这个bug也很简单. 在 ...