PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏
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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int a[100005],n,m,l,r,x,y,k,sum;
scanf(" %d",&n);
a[0]=0;
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
a[i]=a[i-1]+k;
sum+=k;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%d",&l,&r);
if(l>r)
swap(l,r);
x=a[r-1]-a[l-1];
y=sum-x; printf("%d\n",(x<y)?x:y);
}
return 0;
}
PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏的更多相关文章
- PAT甲 1008. Elevator (20) 2016-09-09 23:00 22人阅读 评论(0) 收藏
1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...
- PAT甲 1041. Be Unique (20) 2016-09-09 23:14 33人阅读 评论(0) 收藏
1041. Be Unique (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Being uniqu ...
- PAT甲 1011. World Cup Betting (20) 2016-09-09 23:06 18人阅读 评论(0) 收藏
1011. World Cup Betting (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Wit ...
- PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏
1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...
- PAT甲 1029. Median (25) 2016-09-09 23:11 27人阅读 评论(0) 收藏
1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...
- PAT甲 1009. Product of Polynomials (25) 2016-09-09 23:02 96人阅读 评论(0) 收藏
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏
sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...
随机推荐
- frame标签使用
今天在做onebyone作业的时候,为了使自己的页面更加美观,我便使用了frame框架,百度了他的用法,总结如下 frame,是网页开发必须掌握的知识.例如后台架构.局部刷新,页面分割,都是frame ...
- oozie错误:javax.servlet.jsp.el.ELException: variable [***] cannot be resolved
完整错误: javax.servlet.jsp.el.ELException: variable [compute] cannot be resolved at org.apache.oozie.ut ...
- Little-endian和Big-endian(小端数据和大端数据)
Little和Big指的是内存地址的大小,end指的是数据的末尾. Little-endian指内存地址低的地方存数据的末尾(即低字节) Big-endian指内存地址高的地方存数据的末尾(即高字节) ...
- 有几个Pass,对象就会绘制几次
在每个SubShader内部可以包含多个,至少一个Pass.有几个Pass,对象就会绘制几次.在编写时我们应该尽量控制Pass的数量. 在Pass中包含Cg/HLSL程序片段,在其中编写vertex ...
- 转)Ubuntu14安装wireshark进行抓包
转自:http://jingyan.baidu.com/article/c74d60009d992f0f6a595de6.html 背景: ubuntu14.04/64位 为了抓包和分析包 安装过程: ...
- python作业之生成随机数
作业要求 生成一个6个字符长度的随机数,要求是包括字母和数字的组合 import random l1 = [] for i in range(6): a = random.randrange(0,10 ...
- 4sum, 4sum closest
4sum [抄题]: [思维问题]: 以为很复杂,其实是“排序+双指针”的最高阶模板 [一句话思路]: [输入量特别大怎么办]: [画图]: [一刷]: 先排序! if (i > 0 & ...
- fusioncharts 破解方法(转载)
FusionCharts是一个Flash的图表组件,它可以用来制作数据动画图表,其中动画效果用的是Adobe Flash 8 (原Macromedia Flash的)制作的flash , Fusion ...
- 获取APP的启动图 -Launch Image
http://adad184.com/2015/10/15/tips-access-current-launch-image/
- ubuntu 安装 google Gtest
1.安装源代码 在ubuntu的桌面上,右键选择打开终端,在终端中输入如下命令: $ sudo apt-get install libgtest-dev 下载源码后,apt将会在目录/usr/src/ ...