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

    The world lost seven astronauts of Space Shuttle Columbia(哥伦比亚号航天飞机) this month. It broughthome the ...

  2. 解决:People下面选择分享可见联系人,选择多个联系人后通过短信分享,短信中只显示一个联系人

    问题描述: [操作步骤]:People下导入导出中选择分享可见联系人,选择多个联系人后通过短信分享 [测试结果]:短信中只能显示一个联系人 [预期结果]:可以显示多个联系人 经过代码分析,从compo ...

  3. IPM简介

    1.IPM包含3个函数. image2ground:将图像中的像素点(u, v)对应到地平面上(Z=1)IPM的像素点(x, y): ground2image:将IPM中的像素点(x, y)基于IPM ...

  4. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  5. CTF竞赛(简介)

    一.解体模式(Jeopardy) 可通过互联网或现场网络参与,解决网络安全技术挑战题目,并以分值和时间排名. 题目主要包括:逆向,漏洞挖掘与利用,web渗透,密码,取证,隐写,安全编程 二.攻防模式( ...

  6. 做Global Admin

    globaladmin.4budget@nokia.com 10:22 https://ultima.int.net.nokia.com/eedb/Solutions/tools/ManageUser ...

  7. 使用webpack搭建vue项目中遇到的问题

    1:data数据文件经试验,需要放在生成的build文件夹中才能生效,但是应该把data文件夹先放在src中,然后如何定义config文件,让其复制过去? new CopyWebpackPlugin( ...

  8. SQL Server系统表介绍与使用

    关于SQL Server数据库的一切信息都保存在它的系统表格里.我怀疑你是否花过比较多的时间来检查系统表格,因为你总是忙于用户表格.但是,你可能需要偶尔做一点不同寻常的事,例如数据库所有的触发器.你可 ...

  9. My Router

    新路由 型号:Y1 CPU:单核 链接终端:2 路由系统版本:1.6.6.171.6.9.17 运行时长:0天0小时17分13秒 MAC地址:20:76:93:28:CA:24   LAN口状态 MA ...

  10. 【转】每天一个linux命令(59):rcp命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/03/14/2958685.html rcp代表“remote file copy”(远程文件拷贝).该命 ...