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 ...
随机推荐
- 【集训队作业2018】count
CSP后第一发Blog. 这道题没有一下子过掉,开始还推出了错的结论.在错误的生成函数里绕了好久-- 很显然的转笛卡尔树,一个笛卡尔树对应一种序列.只要考虑一个笛卡尔树是否合法. 贪心地填数发现,从根 ...
- MIME协议(六) -- MIME实例分析
MIME实例分析 了解MIME协议的基本组织结构后,下面用Outlook Express撰写出一封显示效果如图4所示的电子邮件,然后分析该邮件的源文件,以便读者更加深入地了解MIME协议. 1. 启动 ...
- BZOJ 2402 陶陶的难题II (树链剖分、线段树、凸包、分数规划)
毒瘤,毒瘤,毒瘤-- \(30000\)这个数据范围,看上去就是要搞事的啊... 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2402 ...
- 服务器多进程powershell导致服务器瘫痪问题解决
1.公司服务器多次无法访问,经查多由于开启了多个powershell进程,网上查询是被挖矿了,可通过将powershell应用程序重命名解决. 2.然而重命名的时候发现需要trustedInstall ...
- linux中wget未找到命令
(转)linux中wget未找到命令 转:https://blog.csdn.net/djj_alice/article/details/80407769 在装数据库的时候发现无法使用wget命令 ...
- Linux设备驱动程序 之 ioctl
ioctl 除了读取和写入设备之外,大部分驱动程序还需要另外一种能力,即通过设备驱动程序执行各种类型的硬件控制,通常这种需求使用ioctl方法支持,该方法实现了同名的系统调用: 在用户空间,ioctl ...
- JS基础_垃圾回收(GC)
垃圾回收(GC) 程序运行过程中也会产生垃圾,这些垃圾积攒过多以后,会导致程序运行的速度过慢,所以我门需要一个垃圾回收的机制,来处理程序运行过程中产生的垃圾 当一个对象没有任何的变量或属性对它进行引用 ...
- FreeMarker学习(常用表达式)
直接指定值 字符串: "Foo" 或者 'Foo' 或者 "It's \"quoted\"" 或者 'It\'s "quoted& ...
- Qt Model/View 的简单说明
目录: (一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型 ...
- 【Taro全实践】Taro在微信小程序中的生命周期
一.Taro的本身生命周期 生命周期componentWillMount在微信小程序中这一生命周期方法对应页面的onLoad或入口文件app中的onLaunch componentDidMount在微 ...