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 N integer 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 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 int Dist[10000];
14 int N, M;
15 int Sum;
16 void Ans(int i, int j)
17 {
18     int sum = 0;
19     if (i > j)Ans(j, i);
20     else
21     {
22         for (int k = i; k < j; k++)
23             sum += Dist[k];
24         sum = (sum < (Sum - sum)) ? sum : Sum - sum;
25         cout << sum << endl;
26     }
27 }
28 int main()
29 {
30
31     cin >> N;
32     for (int i = 1; i <= N; i++)
33     {
34         cin >> Dist[i];
35         Sum += Dist[i];
36     }
37     cin >> M;
38     int v1, v2;
39     for (int i = 0; i < M; i++)
40     {
41         cin >> v1 >> v2;
42         Ans(v1, v2);
43     }
44 }

ac代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 vector<int>Dist;
14 int sum;
15 int main()
16 {
17     int N;
18     cin >> N;
19     Dist.resize(N + 1);
20     for (int i = 1; i <=N; i++)
21     {
22         int temp;
23         cin >> temp;
24         sum += temp;
25         Dist[i] = sum;
26     }
27     int M;
28     cin >> M;
29     int v1, v2;
30     for (int i = 0; i < M; i++)
31     {
32         cin >> v1 >> v2;
33         if (v1 > v2)
34             swap(v1,v2);
35         int s1 = Dist[v2 - 1] - Dist[v1 - 1];
36         int s2 = sum - s1;
37         if (s1 > s2)
38             cout << s2 << endl;
39         else
40             cout << s1 << endl;
41     }
42 }

原文地址:https://www.cnblogs.com/57one/p/12037128.html

时间: 2024-11-01 16:52:25

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 分)(简单模拟,前缀和)

题目链接: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则不输出该项. 输入样