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. MyBatis空where拦截器

    最近项目中出现了至少两次因为Mybatis的动态where条件不满足导致实际sql语句的where条件为空,进而查询全表,当数据量比较大的时候,导致OOM的情况. 如何禁止这种情况,个人觉得三种措施: ...

  2. 电脑分辨率与pc端页面布局

    在电脑设置中选择:控制面板->外观和个性化->显示 可以设置页面的显示比例,原因是在1920*1080的分辨率下页面的图标就会变得比较小,方便用户看,这个功能就是把页面内容变大(默认是中等 ...

  3. C#中,Json的序列化和反序列化的几种方式总结

    在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据,以及如何反序列化Json数据到对象. 什么是JSON? JSON (JavaScript Object Notation) ...

  4. PCA算法学习(Matlab实现)

    PCA(主成分分析)算法,主要用于数据降维,保留了数据集中对方差贡献最大的若干个特征来达到简化数据集的目的. 实现数据降维的步骤: 1.将原始数据中的每一个样本用向量表示,把所有样本组合起来构成一个矩 ...

  5. oracleHelper 操作帮助类

    using System; using System.Configuration; using System.Data; using System.Collections; using Oracle. ...

  6. Android四大组件framework层

    activity https://www.kancloud.cn/alex_wsc/android-deep2/413484 当前Activity Activity向AMS发送StartActivit ...

  7. JAVA—集合框架

    ref:https://blog.csdn.net/u012961566/article/details/76915755 https://blog.csdn.net/u011240877/artic ...

  8. Codeforces550C(SummerTrainingDay01-H)

    C. Divisibility by Eight time limit per test : 2 seconds memory limit per test : 256 megabytes input ...

  9. C++ 的那些坑 (Day 0)

    C++ 的那些坑 (Day 0) 永远的for循环 其实这里要说的并不是for循环本身还是其中的计数变量的类型的选择. std::string s = "abcd" for (st ...

  10. Django-url反向解析和命名空间

    一.urls硬编码 在反向解析和命名空间之前我们先来说说URLS硬编码,用django 开发应用的时候,可以完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRed ...