[A] 1046 Shortest Distance

时间:2022-09-20 22:56:59

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&period; Shortest Distance &lpar;20&rpar; 2016-09-09 23&colon;17 22人阅读 评论&lpar;0&rpar; 收藏

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  4. PAT 1046 Shortest Distance&lbrack;环形&rsqb;&lbrack;比较&rsqb;

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

  5. 1046 Shortest Distance &lpar;20 分&rpar;

    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 分) &lpar;线段树&rpar;

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

  7. PAT 甲级 1046 Shortest Distance &lpar;20 分&rpar;(前缀和&comma;想了一会儿)

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

  8. PAT &lpar;Advanced Level&rpar; Practice 1046 Shortest Distance &lpar;20 分&rpar; 凌宸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. Android ActionBar Home按钮返回事件处理的两种方式

    今早无聊查看了一下android官方文档,最近对ActionBar很感兴趣,它确实对我们的日常开发起到了很便捷的作用. 对于通过点击ActionBar的Home按钮返回,以前我只知道有一种方式:也就是 ...

  2. Dynamics AX 4&period;0 多表looup

    project rar:http://files.cnblogs.com/files/sxypeace/PrivateProject_MutilTableLookupOnCtronl.rar 在AX ...

  3. vue-router实例

    最近刚刚用vue写了个公司项目,使用vue-cli构建的,算是中大型项目吧,然后这里想记录并且分享一下其中的知识点,希望对大家有帮助,后期会逐渐分享:话不多说,直接上代码!! main.js // T ...

  4. 变量内容的删除、取代与替换 (Optional)

    变量除了可以直接设置来修改原本的内容之外,有没有办法通过简单的动作来将变量的内容进行微调呢? 举例来说,进行变量内容的删除.取代与替换等!是可以的!我们可以通过几个简单的小步骤来进行变量内容的微调喔! ...

  5. python性能分析之cProfile模块

    cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile -s cumulative -s cumulative开关告诉cProfile对每个函数累计花费的时间进行排 ...

  6. win 7设置主机域名

    1 Control Panel\Network and Internet\Network Connections right click Local Area Connection<proper ...

  7. GIT checkout 和 reset 区别

    git checkout -- file:撤销对工作区修改:这个命令是以最新的存储时间节点(add和commit)为参照,覆盖工作区对应文件file:这个命令改变的是工作区 git reset HEA ...

  8. JavaScript 小实例 - 表单输入内容检测,对页面的增删改

    JavaScript 小实例 - 表单输入内容检测,对页面的增删改 效果体验地址:https://xpwi.github.io/js/JavaScript01/jsForm.html 功能: 1.向页 ...

  9. Myeclipse破解总结

    今天安装svn,Myeclipse莫名的崩了,然后就重装,然后不知为什么一直失败...经过无数次尝试,终于成功,应该是把这个破解过程遇到的所有问题都遇到了吧.有个别细节我没尝试,但以下总结用于Myec ...

  10. Solr学习笔记&lpar;1&rpar; —— Solr概述&amp&semi;Solr的安装

    一.概述 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序. 1.1 实现方法 在一些大型门户网站.电子商务网站等都需要站内搜索功能,使用传统 ...