A1046 Shortest Distance (20)(20 分)
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 分)的更多相关文章
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- pat 1046 Shortest Distance(20 分) (线段树)
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- A1046. Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- PAT甲级——A1046 Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
- A1046. Shortest Distance(20)
17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...
- PAT 1046 Shortest Distance
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT 1046 Shortest Distance[环形][比较]
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
随机推荐
- serv-U 7以上版本pasv端口的设置及中文乱码问题
利用serv-u架设ftp服务器已经是再常见不过了事情了,近日一朋友为图新鲜,弄了个7.4版本的新玩意儿,结果架设上去后,仅开了21端口,用LeapFtp在port模式下连接没问题,但是另一常见的cu ...
- 记DotNetBar换肤
界面: comboBoxEx 选择皮肤 buttonX 测试指定皮肤 styleManager 后台代码: 初始化 : this.EnableGlass = false; 设置窗体效果 不设置 依然 ...
- 做一个vue模态弹出框如何
运用的知识点包括: 路由的配置 插槽 vue的过渡动画 路由重定向 router/index.js里面配置路由 import Vue from 'vue' import Router from 'vu ...
- xaml实现无边框窗口
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/w ...
- Mysql数据库操作语句总结(一)
下面的内容来源于链接 https://www.cnblogs.com/bchjazh/p/5997728.html, 个人在此基础上进一步添加了一点东西. 个人操作数据库: mysql 5.5.2 ...
- 《Python高效开发实战》实战演练——开发Django站点1
6.2 实战演练:开发Django站点 用Django开发网站需要遵循Django的一套开发流程.本节通过建立一个消息录入页面演示Django的开发流程及相关技术. 6.12.1 建立项目 在进行D ...
- es6 随笔
记录一些学习es6中学习的新特性,挺有用,作为日后复习es6用,便于记忆. 1.变量定义let和const es6用let.const代替,let是定义块级作用域中的变量,const声明之后必须赋值, ...
- sql server 索引总结一
一.存储结构 在SQL Server中,有许多不同的可用排列规则选项. 二进制:按字符的数字表示形式排序(ASCII码中,用数字32表示空格,用68表示字母"D").因为所有内容都 ...
- 利用ajax实现分页效果
在网页中看到的分页效果,想一下就点击分页中的内容的时候,然后调用ajax调出对应的数据,正确的显示在相应的标签内. 1.用html实现正确的样式和结构 2.采用jquery中的ajax调出数据. 需要 ...
- pat乙级1049
浮点型乘整型和整型乘浮点型结果不同,不知为什么. double sum = 0.0; ; i < n; i++) { cin >> a[i]; sum += a[i] * (i + ...