处理一下前缀和。

#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. HTML之打开/另存为/打印/刷新/查看原文件等按钮的代码

    ■打开■ <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开> < ...

  2. LeetCode OJ 292.Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  3. cakephp2.3.0 lib中的Model.php有一个bug

    1. cakephp2.3.0 lib中的Model.php有一个bug, 加上 !empty($db->config['prefix']) 这个判断更好.有时候会少进行一次 new PDO() ...

  4. opatch auto in windows db in 11.2.0.4

    --prapare:copy 192.168.63.83 D:\oracle_patch\1612 to 192.168.2.169 D:\oracle_patch\1612cd D:\oracle_ ...

  5. HTML+CSS Day06 超链接的样式与搭配

    1.格式 a:link    {} 普通的.未被访问的链接 a:visited {}  用户已访问的链接 a:hover  {} 鼠标指针位于链接的上方 a:active{} 链接被点击的时刻 2.常 ...

  6. OC与Swift桥接问题

    入职新公司后,接手了一个Swift项目.项目质量已经吐槽过一次就略过了,感兴趣的可以看我之前的博客.当然我之前对Swift只是略有了解,略到只看过没写过的程度,主要语言还是OC.不过嘛其实语言都是相通 ...

  7. 使用Core Animation对象来实现动画

    转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...

  8. 第一个前台页面----xflow的页面

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ tagl ...

  9. Arch: Configurations

    the original purpose is to show the steps needed to setup i3 in vbox.. easy. alright, it is a bit mi ...

  10. MySQ安装

    1.去官网下载安装包 .http://www.mysql.com/downloads/ 2.安装过程中会出现下面的提示:记得保存你的MySQL的初始的默认密码.如果没有注意,那么恢复起来有点麻烦,后续 ...