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. 逆向路由器固件之解包 Part1

    这个系列的文章以逆向路由器固件的方式来挖掘路由器中存在的漏洞. 本篇文章主要以介绍解包固件工具为主.文中演示用的固件可以在这里下载.由于针对设备的攻击越来越多,很多厂商把不提供固件下载作为一种安全策略 ...

  2. C++中特殊的宏定义

    常规用法不再介绍,做如下几点说明和介绍 1. 带参数的宏只完成简单字符替换,之前不做计算实参的工作,如下 #define SUM(x,y) x+yint a=3,b=2,c=1;int s;s=SUM ...

  3. nginx+uwsgi+django部署流程

    当我们在用django开发的web项目时,开发测试过程中用到的是django自带的测试服务器,由于其安全及稳定等性能方面的局限性,django官方并不建议将测试服务器用在实际生产. nginx+uws ...

  4. linux 优化git操作速度

    修改 ssh配置:useDNS:no

  5. 好玩的Python库tqdm (转载)

    原文地址: https://blog.csdn.net/zejianli/article/details/77915751 可以显示循环的进度条的库,再也不用担心不知道程序跑到哪里还要跑多久了 tqd ...

  6. S3TC IAP15F2K61S2点亮一个发光二极管keil和stc-isp软件操作

    1.安装破解软件 2.打开STC-ISP,找到头文件,选择保存文件 3.找到keil的安装目录,keil/C51/INC 并保存 4.在桌面新建文件夹 5.打开keil 6.找到在桌面上新建的文件夹 ...

  7. chapter02 朴素贝叶斯分类器对新闻文本数据进行类型预测

    基本数学假设:各个维度上的特征被分类的条件概率之间是相互独立的.所以在特征关联性较强的分类任务上的性能表现不佳. #coding=utf8 # 从sklearn.datasets里导入新闻数据抓取器f ...

  8. Audition CS6在Win 7中“音频输入的采样频率与输出设备不匹配”问题的解决 - imsoft.cnblogs

    1.运行Audion CS6,出现问题框. 2.右击任务栏右下方的那个小喇叭图标,在弹出的提示框里,单击“播放设备”.如图: 3.单击“播放设备”后,在图内左键单击“扬声器”然后双击“扬声器”,出现下 ...

  9. Avito Cool Challenge 2018(div1+2)

    A. Definite Game: 题意:输入N,输出最小的结果N-x,其中x不少N的因子. 思路:N=2时,输出2:其他情况输出1:因为N>2时,N-1不会是N的因子. #include< ...

  10. 20155338 2016-2017-2 《Java程序设计》第8周学习总结

    20155338 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 一.日志 API简介 java++.util.logging包提供了日志功能相关类与接口,不 ...