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 ...
随机推荐
- jquery resize()方法 语法
jquery resize()方法 语法 作用:当调整浏览器窗口的大小时,发生 resize 事件.resize() 方法触发 resize 事件,或规定当发生 resize 事件时运行的函数.大理石 ...
- RestFul是啥
1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...
- linux 动态库文件stripped属性理解
[file命令not stripped] UNIX下*.o和*.so文件显示的stripped和not stripped是什么意思? 表示符号表是否被清除. 在centos 6.2下用file命令查看 ...
- Node.js安装及环境配置
1.Node.js简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js ...
- JVM GC之对象生死
1.简述 在Java内存运行时区域的各个部分中,程序计数器.虚拟机栈.本地方法栈3个区域随着线程而生,随着线程而亡.栈中的栈帧随着方法的进入和退出而有条不紊的进行着入栈和出栈操作. 每个栈帧需要分配多 ...
- super 和 this 的区别
一 this和super关键字区别 1.子类的构造函数如果要引用super的话,必须把super放在函数的首位.2.super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句)3.t ...
- truncate at 255 characters with xlsx files(OLEDB方式读取Excel丢失数据、字符串截断的原因和解决方法)
The TypeGuessRows setting is supported by ACE. Note the version numbers in the key may change depend ...
- js中的事件委托技术
1.什么是事件委托:通俗的讲,时间就是onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个时间本来是加在某些元素上的,然而你却加到别人身上来做,完成这 ...
- [转]基于java的程序OutOfMemory问题的解决及Xms/Xmx/Xss的解释和应用
长期以来一直都是做java应用的开发,所使用的开发工具基本上也都是基于java的,经常用的有eclipse, netbeans, ant, maven, cruisecontrol, tomcat, ...
- Chrome Development Tool: [VM] file from javascript
Chrome Development Tool: [VM] file from javascript [VM] (scriptId) has no special meaning. It's a du ...