PAT 1046 Shortest Distance
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]), followed by N integer distances D1 D2 ⋯ DN, where Diis the distance between the i-th and the (-st exits, and DN 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 (≤), 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 1.
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100005 int a[MAXN];
ll dis[MAXN] = {}; int main(){
int n;cin >> n;
ll sum = ;
for(int i=;i < n;i++){
cin >> a[i];sum+=a[i];
dis[i+] = sum;
}
int t;cin >> t;
int cnt = ;
while(t--){ int x,y;cin >> x >> y;
if(x == y){cout << << endl;continue;}
if(x > y)swap(x,y);
ll sum1 = dis[y-] - dis[x-]; ll sum2 = dis[x-] - dis[] + dis[n] - dis[y-]; cout << min(sum1,sum2) << endl; }
return ;
}
两点之间距离是距0的距离之差,线段树和树状数组差不多就这个性质吧。
PAT 1046 Shortest Distance的更多相关文章
- PAT 1046 Shortest Distance[环形][比较]
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- pat 1046 Shortest Distance(20 分) (线段树)
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- 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 ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
随机推荐
- 小文笔记 - phantomjs
小文笔记 - phantomjs 视频推荐: http://www.intalesson.com/compedium/phantom 2017-05-13 第一节:安装 Windows安装: 下载解压 ...
- python实现八皇后问题
import random def judge(state, nextX): #判断是否和之前的皇后状态有冲突 nextY = len(state) for i in range(nextY): if ...
- 前端js实现 blob转base64位 和 base64位转blob
//**dataURL to blob** function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0]. ...
- React Native组件之BackAndroid !安卓手机的物理返回键的使用
ok!在安卓手机上,当我们用物理返回键的时候,会以一次性的将程序退出来,这样是很不好的体验,所以就需要使用RN的物理返回键组件:BackAndroid,其原理也就是 分析路由,然后pop()这样! o ...
- JSONObject的parseArray方法作用。
该方法将字符串数据转换成集合对象. String dep_tree = JedisUtils.getInstance().get(CacheConstant.DEP_TREE, user.getId( ...
- base64 加密原理 解密原理
假设需要加密的字符串是Jasmine 具体转换步骤: 第一步 将待转换的字符串转为一个个字符第二步 计算每一个字符对应的ASCII码十进制第三步 计算出十进制对应的二进制,若不足8位,在前面添加0进行 ...
- 导入dmp文件时,需要删除原有ORACLE数据库实例
导入dmp文件时,对于已存在的数据库实例及表处理方式:删除实例. 1.以管理员身份登录 sqlplus / as sysdba 2.停止实例 shutdown abort; 执行结果:ORACLE i ...
- vue中兄弟组件间通讯
vue2.0中兄弟组件间的通讯是通过eventBus(事件发布订阅)实现的. eventBus.js import Vue from 'vue' const eventBus = new Vue() ...
- download 属性
1.使用场景 对浏览器 识别/不识别 的文件下载 2.核心代码 var eleTextarea = document.querySelector('textarea'); var eleButton ...
- css设置字体单行,多行超出省略号显示
单行: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 多行 display: -webkit-box; -webkit- ...