PTA(Advanced Level)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,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的更多相关文章
- PAT (Advanced Level) 1046. Shortest Distance (20)
处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- 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
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- 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 (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[环形][比较]
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- 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(20 分) (线段树)
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 ...
随机推荐
- jpa介绍
1.jpa的介绍 JPA是Java Persistence API的简称, 中文名为Java持久层API; 是JDK 5.0注解或XML描述对象-关系表的映射关系, 并将运行期的实体对象持久化到数据库 ...
- Java进阶知识05 Hibernate联合主键之Annotation(注解)和XML实现方式
1.Hibernate联合主键(Annotation实现) 1.1.单列主键 1.1.1.为什么要有主键? //唯一确定一条记录 1.1.2.一个表能否有多个主键? //不能 1.1.3. ...
- P1121 环状最大两段子段和(DP)
P1121 环状最大两段子段和 难度 提高+/省选- 题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件 ...
- Killer Problem (UVA 11898 )
Problem You are given an array of N integers and Q queries. Each query is a closed interval [l, r]. ...
- vps能ping通但是ssh无法连接
一.全国ping测试网页https://tools.ipip.net/ping.php 如果100%丢包率,那么肯定被q了,如果没有,也不一定没被q,进入下一步 二.国内外端口扫描测试http://t ...
- BZOJ 4823 Luogu P3756 [CQOI2017]老C的方块 (网络流、最小割)
题目链接 (Luogu) https://www.luogu.org/problem/P3756 (BZOJ) http://lydsy.com/JudgeOnline/problem.php?id= ...
- Selenium定位策略
1.通过XPath使用contains() 它将启动一个窗口,其中包含文本框开发中涉及的所有特定代码. 记下它的id属性. 通过XPath定位元素的语法 - 使用contains()可以写成: //& ...
- java生成10个不相等的1-20的随机数
public class Test { public static void main(String[] args){ Random ran = new Random(); Set <Integ ...
- oracle性能诊断sql
--1.阻塞及等待事件信息查询-- 查询所有会话的状态.等待类型及当前正在执行的SQL脚本select t.SID, t.SERIAL#, t.Status, t.Action, t.Event, t ...
- koa 基础(十四)cookie 的基本使用
1.app.js /** * cookie的简介: * 1.cookie保存在浏览器客户端 * 2.可以让我们用同一个浏览器访问同一个域名的时候共享数据 * * cookie的作用: * 1.保存用户 ...