pat1046. Shortest Distance (20)
1046. Shortest Distance (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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
int dis[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i;
dis[]=;
int total=;
for(i=;i<=n;i++){
scanf("%d",&dis[i]);
dis[i]=dis[i]+dis[i-];
}
scanf("%d",&dis[]);
total=dis[n]+dis[];//一个环总长度
//cout<<total<<endl;
int m;
scanf("%d",&m);
int a,b;
for(i=;i<m;i++){
scanf("%d %d",&a,&b);
if(a>b){
a=a+b;
b=a-b;
a=a-b;
}
printf("%d\n",dis[b]-dis[a]<total-(dis[b]-dis[a])?dis[b]-dis[a]:total-(dis[b]-dis[a]));
}
return ;
}
pat1046. Shortest Distance (20)的更多相关文章
- PAT1046: Shortest Distance
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- 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 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- 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 ...
- A1046. Shortest Distance(20)
17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...
随机推荐
- EchartJS平均线、最大值、最小值
1.先来看一个没有平均线.最大值.最小值的简单实例 option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigge ...
- 「ZOJ 1354」Extended Lights Out「高斯消元」
题意:给定一个\(5\times 6\)的棋盘的\(01\)状态,每次操作可以使它自己和周围四个格子状态取反,求如何操作,输出一个\(01\)矩阵 题解:这题可以通过枚举第一行的状态然后剩下递推来做, ...
- APIO 2012 派遣(可并堆)
APIO 2012 派遣(可并堆) 给定一棵N个点的树和M,每个点有两个权值ai,bi,每次可以选择一个点x,然后在这个点的子树中选若干点(可以不选自己),使得这些点的\(\sum b_i<=M ...
- [51nod1220] 约数之和(杜教筛+莫比乌斯反演)
题面 传送门 题解 嗯--还是懒得写了--这里 //minamoto #include<bits/stdc++.h> #define R register #define IT map&l ...
- [51nod1222] 最小公倍数计数(莫比乌斯反演)
题面 传送门 题解 我此生可能注定要和反演过不去了--死都看不出来为啥它会突然繁衍反演起来啊-- 设\(f(n)=\sum_{i=1}^n\sum_{j=1}^n[{ij\over\gcd(i,j)} ...
- partial、struct、interface与C#和CLR的关系
partial.struct.interface是C#编译器特有的,CLR对此一无所知.
- iPhone10.3.X越狱后SSH连接不上的问题(已解决)
iPhone10.3.X越狱后SSH连接不上的问题 G0blin RC2,iPhone5s10.3.3 Jailbreak 最近研究了好几天,试了好多的方法. ssh 访问越狱iPhone的两种方式 ...
- python3 continue和break 区别
for i in range(10): if i==5: continue #跳出当次循环 if i==8: break #跳出整个for循环 print(i)
- 动态添加表sql
注意:1.tb_wx_userinfo已经存在,直接复制该表结构 DECLARE @manufacturer_id NVARCHAR(10),@sql NVARCHAR(500) SET @manuf ...
- gitignore 忽略某文件夹下 非某后缀名的文件
忽略指定文件夹下的除某一文件之外的其他文件 parent_dir/!spec_file_name.html 忽略指定文件夹下的除某一类后缀名的文件 parent_dir/*[!*.html] 注意:若 ...