PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点
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", ×);
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 分)的更多相关文章
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- 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 ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- 【PAT甲级】1046 Shortest Distance (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- A1046. Shortest Distance(20)
17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...
随机推荐
- Python之从继承到C3算法
在Python2.X和Python3.X有很多不同的地方,其中一个区别就是和继承有关. 在Python3.X中,一个类如果没有指明其继承哪个类的时候,其默认就是继承object类. 而在Python2 ...
- OpsManage安装过程中遇到的问题和解决方案
系统地址:https://github.com/welliamcao/OpsManage 系统:ubuntu ubuntu使用apt-get进行自动化安装 自带python2.7,不需要再次安装 1. ...
- Codeforces 678E. Another Sith Tournament(概率DP,状压)
Codeforces 678E. Another Sith Tournament 题意: n(n<=18)个人打擂台赛,给定任意两人对决的胜负概率,比赛规则:可指定一人作为最开始的擂主,每次可指 ...
- ubuntu nvidia驱动安装教程
1. 安装显卡切换软件 sudo add-apt-repository ppa:nilarimogard/webupd8 #添加PPA更新源 sudo apt-get update #刷新更新源列 ...
- BFS,优先队列优化
题意: 'S' : 起点 'T' : 终点 '#' : 毒气室 'B' :氧气 'P':不消耗步数 每次经过毒气室需要一瓶氧气,氧气可以重复获得,但只能带五瓶氧气,问最少步数 solution: HI ...
- ELK(elasticsearch+logstash+kibana)入门到熟练-从0开始搭建日志分析系统教程
#此文篇幅较长,涵盖了elk从搭建到运行的知识,看此文档,你需要会点linux,还要看得懂点正则表达式,还有一个聪明的大脑,如果你没有漏掉步骤的话,还搭建不起来elk,你来打我. ELK使用elast ...
- torch学习中的难点
https://github.com/zergtant/pytorch-handbook/blob/master/chapter2/2.1.4-pytorch-basics-data-lorder.i ...
- 数据库 | SQL查询&LIMIT的用法
body{ text-align:left; width:80%; margin:10px 100px; } 前言 select top n 形式的语句可以获取查询的前几个记录,但是 mysql没有此 ...
- 1.springboot内置tomcat的connection相关
最近在研究tomcat的连接超时问题,环境:jdk1.8 + springboot 2.1.1.RELEASE,以下仅为个人理解,如果异议,欢迎指正. springboot的tomcat的几个配置参数 ...
- 定位上下文(补充css的position属性)
]把元素的position属性设定为relative.absolute或fixed后,继而可以使用TRBL属性,相对于另一个元素移动该元素的位置.这里的“另一个元素”,就是该元素的定位上下文. 绝对定 ...