1046 Shortest Distance (20 分)

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,10​5​​]), followed by Ninteger distances D​1​​ D​2​​ ⋯ 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 (≤10​4​​), 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 10​7​​.

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

分析: 题目意思是给了一个环,已经环的每条边(公路)的长度,现给出起点和终点,求出起点和终点之间的最短路径长度,即求顺时针和逆时针行驶的距离最小值。

先求顺时针的距离,而逆时针距离=sum-顺时针距离;

另外暴力求解测试点3会超时,因为每次遍历数组,最大情况10^5个顶点,若有10^4个查询,则一共10^9,对于100ms的时限不能承受。

可以在输入距离时用dis数组记录从1顺时针出发到各顶点的距离。

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-23-19.12.21
 * Description : A1046
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 int a[maxn],dis[maxn];
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     ;
     scanf("%d",&n);
     ;i<=n;i++){
         scanf("%d",&a[i]);
         sum+=a[i];
         dis[i]=sum;
     }
     scanf("%d",&m);
     ;i<m;i++){
         ,ans2=;
         scanf("%d%d",&st,&ed);
         if(st>ed) swap(st,ed);
         ans1=dis[ed-]-dis[st-];
         ans2=sum-ans1;
         printf("%d\n",min(ans1,ans2));
     }
     ;
 }

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. 1046 Shortest Distance (20分)

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

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

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

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

  8. PAT A1046 Shortest Distance (20 分)

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

  9. 1046 Shortest Distance (20)

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

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

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

随机推荐

  1. show point on image

    show point on image... for ( int i = 0; i < probp.size(); i++ ) { cv::Point pt = probp[i]; Distan ...

  2. 如何快速切换目录cd-linux

    前言 cd命令是linux系统中的基本命令行,表示改变工作目录.本文主要介绍几个常用的cd命令. 系统环境 OS:ubuntu16.04. 操作过程 cd www 表示切换到www目录: cd .. ...

  3. PS如何批量处理图片

    喜爱摄影的朋友可能都有这样的体会,相机里面存了大量的图片,一般都是2048×1536或者更大像素的照片,每张都有1M以上,如果设置的清晰度高,则照片就更大,这样的图片是无法上传到博客中的(博客要求每张 ...

  4. scroll滚动动画(js/ts)

    //(蓝色this部分为dom) scrollToLeft(option?: { duration?: number, direction?: number }) { let direction = ...

  5. BZOJ1076: [SCOI2008]奖励关【状压DP+期望DP】

    Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的 ...

  6. 【WebForm】知识笔记

    一.ashx介绍以及ashx文件与aspx文件之间的区别 ashx是什么文件? .ashx 文件用于写web handler的. .ashx文件与.aspx文件类似,可以通过它来调用HttpHandl ...

  7. MySQL--Delete语句别名+LIMIT

    在MySQL中,可以使用LIMIT来限制删除的数量,但部分写法并不支持LIMIT. LIMIT方式 对于查询: ; 可以转换成: ; 别名方式: ## 使用别名 DELETE T FROM T1 AS ...

  8. Web Js推断键盘出发事件

      window.document.onkeydown = disableRefresh; function disableRefresh(evt){ evt = (evt) ? evt : wind ...

  9. CentOS7.4 删除virbr0 virbr0-nic虚拟网卡

    本文摘抄自  https://www.cnblogs.com/cloudos/p/8288041.html 在CentOS 7的安装过程中如果有选择相关虚拟化的的服务安装系统后,启动网卡时会发现有一个 ...

  10. FastAdmin CMS 插件下载

    FastAdmin CMS 插件下载 CMS内容管理系统插件(含小程序) 自定义内容模型.自定义单页.自定义表单.自定义会员发布.付费阅读.小程序等 提供全部前后端源代码和小程序源代码 功能特性 基于 ...