处理一下前缀和。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std; const int maxn=+;
int a[maxn],n,m;
int pre[maxn];
int sum=; int main()
{
scanf("%d",&n); pre[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
pre[i]=pre[i-]+a[i];
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int ans;
int li,ri; scanf("%d%d",&li,&ri);
if(li>ri) swap(li,ri);
ri--,ans=min(pre[ri]-pre[li-],sum-(pre[ri]-pre[li-]));
printf("%d\n",ans);
} return ;
}

PAT (Advanced Level) 1046. Shortest Distance (20)的更多相关文章

  1. PTA(Advanced Level)1046.Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  2. 【PAT甲级】1046 Shortest Distance (20 分)

    题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...

  3. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  4. 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 ...

  5. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  6. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  8. PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...

  9. 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 ...

随机推荐

  1. Chapter 2 Open Book——6

    Last night I'd discovered that Charlie couldn't cook much besides friedeggs and bacon. 昨天晚上我终于发现查理除了 ...

  2. data Mining with Weka: Trailer More Data Mining with Weka 用weka 进行数据挖掘 Weka 用weka 进行更多数据挖掘

    https://www.youtube.com/user/WekaMOOC 大学公开课  视频教程 weka 入门教程 data Mining with Weka: Trailer  More Dat ...

  3. 深入理解HTTP协议(转) 浏览器和服务器如何通信(HTTP协议)

    http协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...

  4. 通过C++修改系统时间代码

    #include <windows.h>#include <stdio.h>#include <iostream>using namespace std;int m ...

  5. Autofac 组件、服务、自动装配(2)

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...

  6. spring security 3中的10个典型用法小结

    spring security 3比较庞大,但功能很强,下面小结下spring security 3中值得 注意的10个典型用法 1)多个authentication-provide可以同时使用 &l ...

  7. CentOS 7 BIND 搭建

    域名查找顺序 设置 /etc/host.conf 1. bind 安装 $ yum install bind bind-utilsnslookup (name server lookup) 在bind ...

  8. 【转】虚拟机下安装小红帽Linux9.0图解

    http://blog.163.com/ly676830315@126/blog/static/10173372220119148583539/

  9. JQuery获取input type="text"中的值的各种方式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. CSS单行、多行文本溢出显示省略号(……)

    这个问题经常遇到 1.单行文本溢出显示省略号(…) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 overflow:hidden;text-overflo ...