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 最近在做日志分 ...
随机推荐
- Implementing the On Item Checked Event for the TListView Control
The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer d ...
- Nunit与Vs 2012配合使用
要使用Nunit首先要去官网 http://www.nunit.org/ 下载Nunit.win .msi是安装版. bin .zip是绿色版. 下载完后安装. 在 VS2012 中使用 Nu ...
- Containerpilot 配置文件 之 Jobs
ContainerPilot job是用户定义的进程和规则,用于何时执行它,如何进行健康检查,以及如何向Consul做广告. 这些规则旨在允许灵活性覆盖几乎可能要运行的任何类型的进程. 一些可能的jo ...
- 个人总结-----非贪心算法的图的m着色判断及优化问题
1.问题描述: 对于著名的图的m着色,有两个主要的问题,一个是图的m色判定问题,一个是图的m色优化问题,描述如下. 图的m色判定问题: 给定无向连通图G和m种颜色.用这些颜色为图G的各顶点着色.问是否 ...
- python连接redis,redis集群
python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...
- 浅谈QT打印功能实现
QT作为一款轻量级的集成开发环境,其设计的目标是使开发人员利用QT这个应用程序框架更加快速及轻易的开发应用程序.要达到此目的,要求QT必须能够跨平台,QT能够在32位及64位的Linux,MAC OS ...
- 对.NET中导出数据到EXCEL的几种方法探讨
最近在做一个报表系统的时候,需要把DATASET中的数据导到EXCEL当中,于是在网上找了一遍,发现了好几种方法,本来以为应该差不多,但后来经过一一试用后,发现在性能上真的差别很大,现在就介绍一下,同 ...
- 10-多写一个@Autowired导致程序崩了
再是javaweb实验六中,是让我们改代码,让它跑起来,结果我少注释了一个,导致一直报错,检查许久没有找到,最后通过代码替换逐步查找,才发现问题.
- 批量去重URL地址并剔除打不开网址
#coding=utf-8 import os import httplib import socket dictlist ={}; def ReadHost(): hosts = []; obn = ...
- win 下 apache 实现负载均衡
1.打开apache的配置文件httpd.conf,打开如下项 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_mo ...