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,105]), followed by N integer distances D1 D2 ⋯ DN, where Di is the distance between the i-th and the (i+1)-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 (≤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
看到一个很好的代码,原样贴下。原帖移步https://www.cnblogs.com/zlrrrr/p/9439663.html
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], dis[maxn];
int main() {
int N, sum = 0;
scanf("%d", &N);
for(int i = 1; i <= N; i ++) {
scanf("%d", &a[i]);
sum += a[i]; //在输入过程中建立dis数组
dis[i] = sum; //降低时间复杂度
}
int T, left, right, temp;
scanf("%d", &T);
for(int i = 1; i <= T; i ++) {
scanf("%d%d", &left, &right);
if(left > right) {
swap(left, right); //swap函数交换
}
temp = dis[right - 1] - dis[left - 1];
printf("%d\n", min(temp, sum - temp));//min函数判断
}
return 0;
}
- Layui常用方法
- 使用Identity Server 4建立Authorization Server (4)
- 使用Identity Server 4建立Authorization Server (3)
- 使用Identity Server 4建立Authorization Server (2)
- 使用Identity Server 4建立Authorization Server (1)
- Java之异常处理
- 挖洞经验 | 看我如何利用SAML漏洞实现Uber内部聊天系统未授权登录
- 使用ichartjs生成图表
- 使用angular4和asp.net core 2 web api做个练习项目(四)
- list.add(),向List集合插入对象报空指针异常
- 使用angular4和asp.net core 2 web api做个练习项目(三)
- 即学即用系列一:纯函数
- Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File
- React编程思想
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一天一大 leet(用两个栈实现队列)难度:简单 DAY-30
- SpringBoot实战:整合Redis、mybatis,封装RedisUtils工具类等(附源码)
- 时间序列&日期学习笔记大全(下)
- 带你了解鸿蒙开发基本流程
- Python 中也可以写 Excel 中的 “Vlookup” 函数?太牛逼了吧!
- 一天一大 leet(除数博弈)难度:简单-Day20200724
- 一天一大 leet(不同路径 II)难度:中等-Day20200706
- Helm部署应用时RBAC权限报错“secrets is forbidden: User "system:...”
- 原来Python自带了数据库,用起来真方便!
- 一天一大 leet(路径总和)难度:简单-Day20200707
- 一天一大 leet(无重复字符的最长子串)难度:中等-more-001
- 一天一大 leet(交错字符串)难度:困难-Day20200718
- 我把公司 10 年老系统改造 Maven,真香!!
- 一天一大 leet(恢复空格)难度:中等-Day20200709
- 一天一大 leet(旋转数组的最小数字)难度:简单-Day20200721