PAT (Advanced Level) 1046. Shortest Distance (20)
处理一下前缀和。
#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)的更多相关文章
- 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 ...
- 【PAT甲级】1046 Shortest Distance (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- 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 ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642
PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...
- 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 ...
随机推荐
- css3-多列显示文字
-------------------------- <style> #demo{ margin: 20px auto; ...
- Handling Captcha | Webdriver
http://seleniumworks.blogspot.kr/2013/09/handling-captcha-webdriver.html Make use of the 'input' tag ...
- hdu_5769_Substring(后缀数组)
题目链接:hdu_5769_Substring 题意: 给你一个字符a和一个串b,问你有多少个包括a的字串 题解: #include<bits/stdc++.h> #define F(i, ...
- view视图--display中echo出ob_get_contents的缓冲内容--(实现,拼接好文件--导入文件)
view.php01默认设置有3个公共的属性,其他属性.后面实例化的时候.通过传递参数.foreach遍历,不断的增加属性02view对象的实例化.位置在-->控制器父类的构造方法中视图的目录名 ...
- 经典.net面试题目(1)
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保 ...
- ios 控件 UIButton
- (void)setTitle:(NSString *)title forState:(UIControlState)state; //设置标题 - (void)setTitleColor:(UIC ...
- C++字符串(1)
C++ 拼接字符串常量 C++允许拼接字符串字面值,即将两个用引号括起的字符串合并为一个.事实上,任何两个由空白(空格,制表符和换行符)分隔的字符串常量都将自动拼接成一个. 例子: cout < ...
- java transient修饰符
1)一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问. 2)transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被trans ...
- alert()显示中文出现乱码
1.如果是外部调用 <script language="javascript" type="text/javascript" src="tswc ...
- div.2/D. As Fast As Possible<数学题,二分>
题目连接 题意: n个学生出去玩,要前进一段距离,租了一辆可以载k个人的车,问到达到目的地的最短时间. cin: n,l,v1,v2,k. £:所有人一起到达终点的时候时间最短. £:所有人走路和坐车 ...