题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点

AC代码

#include <cstdio>
#include <algorithm>
#define N 11000 using namespace std; int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int dis[N];
int dis_sum[N] = {0};
int n, times, small, big, sum = 0;;
scanf("%d", &n); //输入点的个数
for(int i = 1; i < n + 1; i++) {//输入点与点之间的距离
scanf("%d", &dis[i]);
sum += dis[i];
dis_sum[i] = sum;
}
/*for(int i = 1; i < n + 1; i++) {
printf("--- %d --- %d ---\n", dis[i], dis_sum[i]);
}*/
scanf("%d", &times);
for(int i = 0; i < times; i++) {
int temp1, temp2, right, left, big, small, shortest;
scanf("%d%d", &left, &right);
//printf("%d %d")
big= max(left, right);
small = min(left, right);
temp1 = dis_sum[big - 1] - dis_sum[small - 1]; //从右开始算的距离
//temp2 = dis_sum[small - 1] + (sum - dis_sum[big - 1]); //从左开始算的距离
temp2 = sum - temp1;
shortest = min(temp1, temp2);
//printf("small:%d---dis_sum[small]:%d---big:%d---dis_sum[big]:%d---temp1:%d---temp2:%d---shortest:%d\n", small, dis_sum[small], big, dis_sum[big], temp1, temp2, shortest);
printf("%d\n", shortest);
}
return 0;
}

PAT A1046 Shortest Distance (20 分)的更多相关文章

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

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

  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 A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  4. 1046 Shortest Distance (20 分)

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

  5. A1046 Shortest Distance (20)(20 分)

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

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

  7. 1046 Shortest Distance (20分)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  8. 【PAT甲级】1046 Shortest Distance (20 分)

    题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...

  9. A1046. Shortest Distance(20)

    17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...

随机推荐

  1. 初次使用自己写的testbench 验证了简单的NOT门。

    先是简单的非门模型: module notgate(a,b); input a; output b; assign b=~a; endmodule 下面是自己写的简陋的testbench: `time ...

  2. ssh登陆强制使用密码验证登陆

    Linux系统使用ssh进行登陆,可以采用密码登陆和秘钥登陆.采用密码登陆每次需要输入密码进行验证,验证通过则可登陆到环境. 秘钥登陆为在服务器的客户端生成相应的公钥和私钥,公钥用于加密,私钥用于解密 ...

  3. TIZ_c 第0周总结(2019/10/15-2019/10/22)工欲善其事必先利其器

    TIZ_c 第0周总结(2019/10/15-2019/10/22)工欲善其事必先利其器 任务清单 给自己取一个酷酷的id,并选择1-2个喜欢的方向.(只是初步选择,后期可更改) 改下群名片.例如yo ...

  4. Linux .bashrc

    Set environmental variable // 用:隔开 export PATH=$PATH:<PATH >:<PATH >:<PATH >:----- ...

  5. http协议格式详解

    参考:https://www.jianshu.com/p/8fe93a14754c 一.URI结构 HTTP使用统一资源标识符(URI)来传输数据和建立连接.URL(统一资源定位符)是一种特殊种类的U ...

  6. Nginx之核心结构体ngx_cycle_t

    1. ngx_listening_t 结构体 ngx_cycle_t 对象中有一个动态数组成员叫做 listening,它的每个数组元素都是 ngx_listening_t 结构体,而每个 ngx_l ...

  7. [MyBatis]最简MyBatis工程

    下载地址:https://files.cnblogs.com/files/xiandedanteng/fillMillionDatum01_191005.rar --END-- 2019年10月5日1 ...

  8. java读取XML文件,及封装XML字符串

    package com.yyl.text; import java.io.FileInputStream; import java.util.ArrayList; import org.junit.T ...

  9. 性能测试 | Web端性能测试

    这篇文章想跟大家分享关于Web性能测试的一些知识点.在分享之前,我想先跟大家分享一组数据,关于为什么要做性能测试?具体如下: 71%用户希望在手机上打开网页能跟电脑一样快: 5秒钟被认为是用户能忍受的 ...

  10. P5020 货币系统

    P5020 货币系统 题解 仔细分析... 这道题其实就是求所给数组中有多少个数字不能被该数组中的数字自由组合表示出来 比如样例1 3,10 不能被该集合里的数字表示出来,所以他们组成目标集合 6=3 ...