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

题意:

输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离。接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序号,输出他们之间的最小距离。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int dis[100007],sum[100007];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;++i){
cin>>dis[i];
sum[i]=sum[i-1]+dis[i];
}
int q;
cin>>q;
for(int i=1;i<=q;++i){
int u,v;
cin>>u>>v;
if(v<u)
swap(u,v);
int x=sum[v-1]-sum[u-1];
int y=sum[n]-x;
cout<<min(x,y)<<"\n";
}
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11605845.html

时间: 2024-11-08 19:16:34

【PAT甲级】1046 Shortest Distance (20 分)的相关文章

1046 Shortest Distance (20 分)

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

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 con

1046 Shortest Distance (20 分)(简单模拟,前缀和)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 注意:1.这是一个环,两点之间的距离有两种,一是顺时针,二是逆时针,求出其中的一个距离,另一个距离用这个圆的总距离减去得到,输出两个距离之间最小的那个 2.注意区分x,y的大小,有可能下标大的在前面. 3.用到了前缀和的思想 1 #include <algorithm> 2 #include <iostream> 3

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a

【PAT甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if

【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. 代码: #include<bits/stdc++.h>using namespace std;string s[107];int length[107];char ans[307];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; cin.igno

【PAT甲级】1008 Elevator (20分)

1008 Elevator 题目: The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one