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


晚上被这道题坑了很久,一直没想明白为什么不能完全ac,会出现超时,只能拿17/20的分,,我一开始的想法是每次都一个循环遍历相加来求距离,查了网上资料说,在极端情况下,每次查询都需要遍历整个数组,即有1e5次操作,而共有1e4次查询,所以极端情况下会有1e9次操作,这在100ms内往往会超时。
解决办法是一开始设置一个dis[i]表示1号结点顺时针方向到达i号结点的下一个结点的距离,这样在输入时就可以直接得到dis,因此查询复杂度可以直接达到o(1),这样就好了orz.。。

#include<cstdio>

using namespace std;

#define MAX 100001

int dis[MAX] , a[MAX];

int main(void){
    int sum = 0;
    int n,m,v1,v2;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&a[i]);
        sum += a[i];
        dis[i] = sum;
    }

    scanf("%d",&m);
    while(m--){
        scanf("%d %d",&v1,&v2);
        if(v1 > v2){
            int t = v1;
            v1 = v2;
            v2 =t;
        }

        int ans = dis[v2-1] - dis[v1-1];
        if(ans > (sum-ans) )
            ans = sum -ans;
        printf("%d\n",ans);
    }

    return 0;
}

[A] 1046 Shortest Distance的更多相关文章

  1. PAT 1046 Shortest Distance

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

  2. 1046 Shortest Distance (20 分)

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

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

  4. PAT 1046 Shortest Distance[环形][比较]

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

  5. 1046 Shortest Distance (20 分)

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

  6. pat 1046 Shortest Distance(20 分) (线段树)

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

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

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

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

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

  9. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

随机推荐

  1. C# WebApi 返回JSON

    在默认情况下,当我们新建一个webapi项目,会自动返回XML格式的数据,如果我们想返回JSON的数据,可以设置下面的三种方法. 1. 不用改配置文件,在Controller的方法中,直接返回Http ...

  2. [转]SQL - Create XML - How to set Unicode UTF-8

    本文转自:https://stackoverflow.com/questions/44754356/sql-create-xml-how-to-set-unicode-utf-8 I found a ...

  3. python模块之xlrd

    python处理excel的模块,xlrd读取excel,xlwt写入excel 一.安装 pip install xlrd 二.使用 1. 打开excel,得到Book对象 import xlrd ...

  4. CSS学习笔记04 CSS文字排版常用属性

    字体样式属性 font-size:字号大小 font-size属性用于设置字号,该属性的值可以使用相对长度单位,也可以使用绝对长度单位.其中,相对长度单位比较常用,推荐使用像素单位px,绝对长度单位使 ...

  5. POJ1611(KB2-B)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 39211   Accepted: 18981 De ...

  6. Can’t connect to local MySQL server through socket的解决方法

    http://www.aiezu.com/db/mysql_cant_connect_through_socket.html mysql,mysqldump,php连接mysql服务常会提示下面错误: ...

  7. CSS3实现的几个小loading效果

    昨晚上闲的没事突然想做几个小loading效果,下面是昨晚上做的几个小案例,分享给大家 1.水波loading:这个loading是我觉得非常简单,但是看上去的效果却非常不错的一个小loading 这 ...

  8. 【代码笔记】iOS-UITableView上的button点击事件

    代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController < ...

  9. memset初始化数组的问题

    今天才搞清楚,memset用于初始化数组,仅能初始化为0值,而不能初始化一个特定的值,这怎么能模糊了呢??? 因此,如果对申请的一段存放数组的内存进行初始化,每个数组元素均初始化为特定的值,必须使用循 ...

  10. JNI使用方法

    JNI可以让我们在java代码中调用本地库的功能. 下面记录一下JNI简单的使用方法 创建java端接口 public class JNIIterface { // 导入最终生成的dll文件 stat ...