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. PHP面试题及答案(五)

    1. 禁用COOKIE 后 SEESION 还能用吗? 答案: 不能. 2. 抓取远程图片到本地,你会用什么函数? 答案: fsockopen(). 3.求两个日期的差数,例如2007-2-5 ~ 2 ...

  2. Windows安全认证是如何进行的?[NTLM篇]

    <上篇>中我们介绍Kerberos认证的整个流程.在允许的环境下,Kerberos是首选的认证方式.在这之前,Windows主要采用另一种认证协议——NTLM(NT Lan Manager ...

  3. ElasticSearch 处理自然语言流程

    ES处理人类语言 ElasticSearch提供了很多的语言分析器,这些分析器承担以下四种角色: 文本拆分为单词 The quick brown foxes → [ The, quick, brown ...

  4. C# 连接 postgres失败

    小程序完成了,要发布到客户的服务器上: 我的测试机:win10  64             数据库:PG9.6.5     64 客户服务器:win7    32             数据库: ...

  5. 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)

    1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...

  6. VS找到了XXX的副本,但是当前源代码与XXX中内置的版本不同

    1.博客园有解决ASP.Net出现以上问题的方法: 删除ASP.Net临时文件夹内的dll文件. https://www.cnblogs.com/autumn/p/5261576.html 2.但我的 ...

  7. cms-后台eazyui搭建

    1.引入eazyUi需要的js 2.布局:上.下.左.中 <%@ page language="java" contentType="text/html; char ...

  8. windows xp professional 序列号(密钥)及百度网盘下载地址

    HH7VV-6P3G9-82TWK-QKJJ3-MXR96 https://pan.baidu.com/share/link?uk=4247247642&shareid=500360

  9. pta 编程题14 Huffman Codes

    其它pta数据结构编程题请参见:pta 题目 题目给出一组字母和每个字母的频数,因为哈夫曼编码不唯一,然后给出几组编码,因为哈夫曼编码不唯一,所以让你判断这些编码是否符合是哈夫曼编码的一种. 解题思路 ...

  10. pta 编程题13 File Transfer

    其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...