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 each case, the first line contains an integer N (in [3]), followed by Ninteger distances D?1?? D?2?? ? D?N??, where D?i?? is the distance between the i-th and the (-st exits, and D?N?? 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(≤), 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 1.

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

水题,就是一个前缀和就完事。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 int an[100005];
 5 int main(){
 6     cin >> n;
 7     int sum = 0;
 8     for(int i = 1; i <= n ; i ++){
 9         cin >> an[i];
10         sum += an[i];
11         an[i] += an[i-1];
12     }
13     cin >> m;
14     int xx, yy, x, y;
15     for(int i = 0; i < m; i ++){
16         cin >> xx >> yy;
17         y = max(xx,yy);
18         x = min(xx,yy);
19         int cnt = an[y-1]-an[x-1];
20         int cnt1 = sum - cnt;
21         int ans = min(cnt, cnt1);
22         cout << ans << endl;
23     }
24     return 0;
25 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11182671.html

时间: 2024-10-10 13:06:42

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 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甲级】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_stdi

pat 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

问题 E: Shortest Distance (20)

1 #include <iostream> 2 #include<cstring> 3 #include<vector> 4 #define maxn 100001 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 6 using namespace std; 7 int n; 8 typedef

1046 Shortest Distance (20point(s)) Easy only once *数组预处理问题

基本思想: 对于这个环形的正权值队列来说,完全可以从第一个节点计数,用1~i和1~j节点的距离来计算i~j节点的距离: 注意点: 1.对于高次个数,遍历不靠谱,找机会打表和优化结构: 2.对于正负权值要注意: 超时代码: 1 #include<iostream> 2 #include<stdlib.h> 3 #include<stdio.h> 4 #include<vector> 5 #include<string> 6 #include<

PAT——甲级1046S:shortest Distance

这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) 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

PTA-C-4-7 统计某类完全平方数 (20分)

4-7 统计某类完全平方数   (20分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144.676等. 函数接口定义: int IsTheNumber ( const int N ); 其中N是用户传入的参数.如果N满足条件,则该函数必须返回1,否则返回0. 裁判测试程序样例: #include <stdio.h> #include <math.h> int IsTheNumber ( const int N ); int ma

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样