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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424

注意:1.这是一个环,两点之间的距离有两种,一是顺时针,二是逆时针,求出其中的一个距离,另一个距离用这个圆的总距离减去得到,输出两个距离之间最小的那个

2.注意区分x,y的大小,有可能下标大的在前面。

3.用到了前缀和的思想

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <cstring>
 6 #include <vector>
 7 using namespace std;
 8 const int maxn = 0x3f3f3f3f;
 9 typedef long long ll;
10 int main() {
11     int n,m,a[100005],sum[100005],sum2 = 0;
12     cin >> n;
13     sum[0] = 0;
14     for(int i = 1; i <= n; i++) {
15         cin >> a[i];
16         if(i < n)
17         {
18             if(i == 1) sum[1] = a[1];
19             else sum[i] = sum[i-1] + a[i];
20         }
21         sum2 += a[i];
22     }
23     cin >> m;
24     int x,y,ans1,ans2;
25     while(m--) {
26     cin >> x >> y;
27         if(x < y) {
28             ans1 = sum[y-1] - sum[x-1];
29             ans2 = sum2 - ans1;
30         }
31         else{
32             ans1 = sum[x-1] - sum[y-1];
33             ans2 = sum2 - ans1;
34         }
35         cout << min(ans1,ans2) << endl;
36     }
37     return 0;
38 }

原文地址:https://www.cnblogs.com/LLLAIH/p/11743463.html

时间: 2024-08-29 12:11:04

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

【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

PAT/简单模拟习题集(二)

B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. Input: 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即甲.乙双方同时给出的的手势.C代表"锤子".J代表"剪刀".B代表"布",第1个字母代表甲方

A1046——入门模拟 Shortest Distance

2019-12-15 15:25:34 #include <bits/stdc++.h> #include<math.h> using namespace std; const int MAXN = 100005; int main(){ int N; cin>>N; int temp[N+1]; for(int i=0;i<N+1;++i){ temp[i] = 0; } for(int i = 1;i<N+1;++i){ cin>>temp[