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

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. Tomcat怎么关闭日志输出

    tomcat中禁用catalina.out的输出,又可能很大. 1.直接修改catalina.sh文件的输出语句. 在文件中找到以下内容. if [ -z "$CATALINA_OUT&qu ...

  2. 腾讯域名防封 微信/QQ域名检测,域名防封的原理

    微信屏蔽网页的依据是什么?明面上的原因是网页内容有诱导.诈骗等不和谐的内容时候,被用户举报就会封闭.实际上这只是表面现象,因为我们能明确的感受到不同的阶段,同样的内容,被封杀的频率差别很大的,也就是说 ...

  3. Codeforces 808 E. Selling Souvenirs(三分)

    E. Selling Souvenirs 题意: n件物品,有重量和价值,重量只有三种1,2,3.问取不超过m重量的物品的价值总和最大是多少.(n<=1e5,w<=3e5) 思路: n*w ...

  4. 说说如何使用unity Vs来进行断点调试

    转载自:http://dong2008hong.blog.163.com/blog/static/4696882720140293549365/ 大家可以从这下载最新版的unity vs. Unity ...

  5. selenium知识点

    1. 导包 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from se ...

  6. C#_switch语句,for循环,do while循环,while循环

    1:switch语句,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  7. cast()、decimal(M,D) --SQL对查询字段保留小数位操作

    参考:http://database.51cto.com/art/201005/201651.htm http://www.lai18.com/content/1693593.html 直接上例子,以 ...

  8. 胆机实作(1)——12AU7甲类单端前级

    第一次做胆机,选了最经典的单端共阴架构.电路没有什么复杂的,基本就是教科书电路,没有用负反馈,因为我不喜欢负反馈的声音.用电路板做机器焊接也没什么难度,器件找好开焊就行,试做机就不打算找外壳了,也不用 ...

  9. 一百零三:CMS系统之使用sweetalert提示框优化返回结果

    在base模板中引用 在修改密码的js中使用 $(function () { $('#submit').click(function (evnet) { evnet.preventDefault(); ...

  10. hutool-all 包把实体Bean转化成字符串,以及把字符串转化成Bean对象

    GxyJobEntity gxyJobEntity1 = new GxyJobEntity(); gxyJobEntity1.setUserId("user001"); gxyJo ...