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 D​1​​ D​2​​ ⋯ D​N​​, where D​i​​ is the distance between the i-th and the (-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 (≤), 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
题目分析:刚开始的直接暴力做 第三个点没过 看了别人的博客 认识了前缀数组
错误代码
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Dist[];
int N, M;
int Sum;
void Ans(int i, int j)
{
int sum = ;
if (i > j)Ans(j, i);
else
{
for (int k = i; k < j; k++)
sum += Dist[k];
sum = (sum < (Sum - sum)) ? sum : Sum - sum;
cout << sum << endl;
}
}
int main()
{ cin >> N;
for (int i = ; i <= N; i++)
{
cin >> Dist[i];
Sum += Dist[i];
}
cin >> M;
int v1, v2;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
Ans(v1, v2);
}
}

ac代码

 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
vector<int>Dist;
int sum;
int main()
{
int N;
cin >> N;
Dist.resize(N + );
for (int i = ; i <=N; i++)
{
int temp;
cin >> temp;
sum += temp;
Dist[i] = sum;
}
int M;
cin >> M;
int v1, v2;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
if (v1 > v2)
swap(v1,v2);
int s1 = Dist[v2 - ] - Dist[v1 - ];
int s2 = sum - s1;
if (s1 > s2)
cout << s2 << endl;
else
cout << s1 << endl;
}
}

1046 Shortest Distance (20分)的更多相关文章

  1. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  2. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  3. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  4. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  5. 【PAT甲级】1046 Shortest Distance (20 分)

    题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...

  6. 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 ...

  7. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  8. 1046 Shortest Distance (20)

    #include<stdio.h> int main() { int n,m,a,b,tem,pre,p; int i,j; ]; while(scanf("%d",& ...

  9. PAT (Advanced Level) 1046. Shortest Distance (20)

    处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

随机推荐

  1. Java继承中构造器的调用原理

    Java的继承是比较重要的特性,也是比较容易出错的地方,下面这个例子将展示如果父类构造器中调用被子类重写的方法时会出现的情况: 首先是父类: public class test { void fun( ...

  2. 【Spring Data 系列学习】Spring Data JPA @Query 注解查询

    [Spring Data 系列学习]Spring Data JPA @Query 注解查询 前面的章节讲述了 Spring Data Jpa 通过声明式对数据库进行操作,上手速度快简单易操作.但同时 ...

  3. json 的基础入门

    JSON是什么: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.在初始的项目开发中人们更倾向于使用xml来进行数据的传输,但当JSON出现后,开发者更倾 ...

  4. .Net Core 实现图片验证码

    记录自己的学习,参考了网上各位大佬的技术,往往在登录的时候需要使用到验证码来进行简单的一个校验,这边使用在.net core上进行生成图片二维码 思路很简单=> 生成一个随机数->保存到服 ...

  5. Mybatis(一)Mybatis相关概念

    1.1 传统的JDBC实现 public static void main(String[] args) { Connection connetion = null; PreparedStatemen ...

  6. JavaScript每日学习日记(2)

    8.13.2019 1. 正则表达式常见字符串方法: search( ) , replace( ) var str = "Visit Website"; var n = str.s ...

  7. 【盘它!】那些让效率MAX的工具和方法(Mac篇)

    一.前言 人类之所以伟大,是因为会创造并使用工具! 工欲善其事必先利器.高效的工具和方法不仅能最大化 节省我们的时间,还可以一定程度上让我们 保持专注,以达到 事半功倍 的效果. 但仅仅有工具不会使用 ...

  8. bash编程练习,带选项,添加或删除用户

    脚本练习题: 可以接受选项及参数,而后能获取每一个选项,及选项的参数,并能根据选项及参数做出特定的操作: 比如:adminusers.sh -a|--add user .. -d|--del user ...

  9. 在ASP.NET MVC中如何预防Cookie的窃取攻击(转载)

    Cookie Cookie is a small piece of data sent by a web server to a web browser. The browser stores thi ...

  10. Java 中,如何对日期进行加减操作

    今天在做项目时,遇到了对时间的加减进行操作的需求,根据传入的日期字符串,操作参数("+","-"),加数(要操作的天数),对日期进行加减操作,经查询资料,自己写 ...