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<math.h>
 7 #include<algorithm>
 8 using namespace std;
 9 using std::vector;
10 const int maxn = 100009;
11 int dis[maxn];
12 int main() {
13     int n;
14     scanf("%d", &n);
15     for (int i = 0; i < n; i++)
16         scanf("%d", &dis[i]);
17     int m;
18     scanf("%d", &m);
19     for (int i = 0; i < m; i++) {
20         int a, b;
21         scanf("%d %d", &a, &b);
22         a--;
23         b--;
24         int d1=0,d2=0;
25         int in = a;
26         while (in != b) {
27             d1 += dis[in];
28             in = (in + 1) % n;
29         }
30         in = a;
31         while (in != b&&d2<d1) {
32             in = ((in - 1) + n) % n;
33             d2 += dis[in];
34         }
35         printf("%d\n", min(d1, d2));
36     }
37     system("pause");
38     return 0;
39 }

预处理代码:

 1 #include<iostream>
 2 #include<stdlib.h>
 3 #include<stdio.h>
 4 #include<vector>
 5 #include<string>
 6 #include<math.h>
 7 #include<algorithm>
 8 using namespace std;
 9 using std::vector;
10 const int maxn = 100009;
11 int dis[maxn] = {0};
12 int dis_pre[maxn];
13 int main() {
14     int n;
15     int sum = 0;
16     scanf("%d", &n);
17     for (int i = 1; i <= n; i++) {
18         scanf("%d", &dis[i]);
19         dis_pre[i+1] = dis[i] + dis_pre[i];
20         sum += dis[i];
21     }
22     int m;
23     scanf("%d", &m);
24     for (int i = 0; i < m; i++) {
25         int a, b;
26         scanf("%d %d", &a, &b);
27         int d = abs(dis_pre[b] - dis_pre[a]);
28         printf("%d\n", min(d,sum-d));
29     }
30     system("pause");
31     return 0;
32 }

原文地址:https://www.cnblogs.com/songlinxuan/p/12187458.html

时间: 2024-08-30 05:03:40

1046 Shortest Distance (20point(s)) Easy only once *数组预处理问题的相关文章

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

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

【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

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——甲级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

(Easy) Shortest distance to Character LeetCode

Description: Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note:

[Solution] 821. Shortest Distance to a Character

Difficulty: Easy Problem Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2,

[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Write a query to find the shortest distance between two points in these points. | x | |-----| | -1 | | 0 | | 2 | The shortest distance is '1' obviously, w