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 ... 
随机推荐
- 使用Camtasia 9 录制屏幕软件
			Camtasia 9 录制屏幕软件,并且有丰富的专业剪辑功能. 
- RabbitMQ的下载与安装
			RabbitMQ的安装注意事项: 1. 系统的管理员账户不能是中文(win8) 2. 计算机名不能是中文(win8) 3. 推荐:使用默认的安装目录 4. 使用的计算机用户必须是管理员 如果安装不成功 ... 
- 解决 go get golang.org/x/* 拉取失败问题
			go get -u -v golang.org/x/text 有时候会因为网络问题,导致失败. 在不用代理的情况下,可以使用以下方案. golang.org,他们在github上有一个镜像库 以 go ... 
- ThinkPHP数据查询与添加语句
			在ThinkPHP框架中实现数据的查询操作 function ShowAll() { //Model:数据库中每张表对应一个模型 //类名是表名,类里面的成员变量是列名 //把一张表对应一个类,其中一 ... 
- LC 275. H-Index II
			Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a ... 
- 基于Python使用scrapy-redis框架实现分布式爬虫
			1.首先介绍一下:scrapy-redis框架 scrapy-redis:一个三方的基于redis的分布式爬虫框架,配合scrapy使用,让爬虫具有了分布式爬取的功能.github地址: https: ... 
- python中requests.session的妙用
			在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有时候需要保持一些共用的数据,例如cookies信息. 1.requests库的session对象能够帮我们跨请求保持某些参数,也会在 ... 
- Selenium 2自动化测试实战38(整合自动发邮件功能)
			整合自动发邮件功能 解决了前面的问题后,现在就可以将自动发邮件功能集成到自动化测试项目中了.下面重新编辑runtest.py文件 #runtest.py #coding:utf-8 from HTML ... 
- Elasticsearch技术解析与实战--shard&replica机制
			序言 1.shard&replica机制 (1)index包含多个shard (2)每个shard都是一个最小工作单元,承载部分数据,lucene实例,完整的建立索引和处理请求的能力 (3)增 ... 
- docker网络(3)
			docker网络介绍 大量的互联网应用服务需要多个服务组件,这往往需要多个容器之间通过网络通信进行相互配合. docker 网络从覆盖范围可分为单个 host 上的容器网络和跨多个 host 的网络. ... 
