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 ...
随机推荐
- Python实现机器学习算法:逻辑回归
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets.samples_generator import ma ...
- Images之Dockerfile中的命令1
Dockerfile reference Docker can build images automatically by reading the instructions from a Docker ...
- Wordpress搭建
Install Environment apt install apache2 php mysql-server apt install php-mysql php-fpm Config mysql ...
- bean之间的属性是怎么维护的
spring对象[bean]之间的属性是通过什么维护的: 1.构造方法:标签:constructor-arg 2.set方法:标签:property <beans xmlns="htt ...
- 解决github网站打开慢的问题
一.前言 作为一名合格的程序员,github打开速度太慢怎么能容忍.但是可以通过修改hosts文件信息来解决这个问题.现在chrome访问github速度杠杠的! 二.macOS解决方法 打开host ...
- OpenModelica读取文件
parameter String file = Modelica.Utilities.Files.loadResource("J:/git/tcs/tcs.txt"); 将文件名变 ...
- Math.abs(~2020) 按位取反后的绝对值是多少 2021, 按位取反后,比正数多1
Math.abs(~2020) 按位取反后的绝对值是多少 2021, 按位取反后,比正数多1 int 值的取值 范围: -128 --- 127 之间, 0000 0000 按位取 ...
- 力扣(LeetCode)922. 按奇偶排序数组 II
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...
- 力扣(LeetCode)13. 罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- Codeforces 958C3 - Encryption (hard)
C3 - Encryption (hard) 思路: 记sum[i]表示0 - i 的和对 p 取模的值. 1.如果k * p > n,那么与C2的做法一致,O(k*p*n)复杂度低于1e8. ...