1046 Shortest Distance (20)(20 分)提问

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:

5 1 2 4 14 9
3
1 3
2 5
4 1

Sample Output:

3
10
7

思考

这里面c++的解法用到了头文件algorithm

纯C语言可以使用algorithm头文件,因为algorithm是C++库里的 algorithm中的大部分算法都是针对C++语言特有的,需要用到STL(标准模板库)的容器等。具体可以参考:https://en.wikipedia.org/wiki/Algorithm_(C%2B%2B) 纯C语言可以在网上找一些第三方的库去替代,但是灵活性肯定是比C++的标准库提供的方法低很多,因为语言本身的局限性。

交换和求较小值

/*交换两个整数值*/
myswap(int *a,int *b){
int *temp;
temp=a;
a=b;
b=temp;
}//这个交换对外界的那两个left与right没有影响
/*交换修正版*/
myswap(int *a,int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}/*传入指针,就能修改这个值本身*/
/*求两整数较小值*/
int mymin(int a,int b){
return (a>b)?b:a;
}

AC代码

#include <stdio.h>
#define max 100005
int dis[max], A[max];
/*交换两个整数值*/
myswap(int *a,int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}//还是有疑惑的
/*求两整数较小值*/
int mymin(int a,int b){
return a>b?b:a;
}
int main() {
int sum = 0, query, n, left, right;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
sum += A[i];
dis[i] = sum;//存入了顺时针从1号点到i+1号点的距离
}
scanf("%d", &query);
for(int i = 0; i < query; i++) {
scanf("%d%d", &left, &right);
if(left > right) myswap(&left, &right);
int temp = dis[right - 1] - dis[left - 1];
printf("%d\n", mymin(temp, sum - temp));
}
return 0;
}

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

  1. 1046 Shortest Distance (20 分)

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

  2. pat 1046 Shortest Distance(20 分) (线段树)

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

  3. PAT A1046 Shortest Distance

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

  4. A1046. Shortest Distance

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

  5. PAT甲级——A1046 Shortest Distance

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

  6. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  7. A1046. Shortest Distance(20)

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

  8. PAT 1046 Shortest Distance

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

  9. PAT 1046 Shortest Distance[环形][比较]

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

随机推荐

  1. 【踩坑】socket.io服务器不能访问

    今天在单机测试socket.io服务器时一切正常,但用jar包的方式部署在服务器时发现客户端无法连接. 于是做了一系列排查,如检查端口是否在占用,防火墙有没有开放该端口,阿里云终端是否有开放安全组,本 ...

  2. Spring cloud Eureka 服务治理(搭建服务注册中心)

    服务之类是微服务架构中最为核心的基础模块,它主要用来实现各个微服务实例的自动化注册和发现. 1. 服务注册 在服务治理框架中,通常会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机. ...

  3. 学习笔记:《JavaScript高级程序设计》

    第1章 JavaScript简介 1.一个完整的JavaScript实现应该由三部分组成:核心(ECMAScript),文档对象模型(DOM)和浏览器对象模型(BOM). 2.Web浏览器只是ECMA ...

  4. 使用create react app教程

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  5. Protocol Buffer学习教程之类库应用(四)

    Protocol Buffer学习教程之类库应用(四) 此教程是通过一个简单的示例,给C++开发者介绍一下如何使用protocol buffers编程,主要包括以下几部分: 定义一个.proto文件 ...

  6. SQL Server索引总结二

    从CREATE开始 通过显式的CREATE INDEX命令 在创建约束时作为隐含的对象 随约束创建的隐含索引 当向表中添加如下两种约束之一时,就会创建隐含索引. 主键约束(聚集索引) 唯一约束(唯一索 ...

  7. COGS 1944. 背驮式行走

    ★   输入文件:piggyback.in   输出文件:piggyback.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] Bessie和她妹妹Elsie白天都在牧场 ...

  8. IOS 强指针(strong)和弱指针(weak)

    // strong 强指针        // weak 弱指针        // ARC, 只要对象没有强指针就会自动释放        // OC中默认都是强指针

  9. 【UVA10652】Board Wrapping(求凸包面积)

    点此看题面 大致题意: 告诉你若干个矩形的重心坐标.长.宽和相对\(y\)轴的偏转角度,求矩形面积和与能围住这些矩形的最小凸包面积之比. 矩形面积和 这应该是比较好求的吧. 已经给了你长和宽,直接乘起 ...

  10. 2017.12.19 Java包的静态导入import static和import的区别

    import static静态导入是JDK1.5中的新特性.一般我们导入一个类都用 import com-..ClassName;而静态导入是这样:import static com-..ClassN ...