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
题目分析:刚开始的直接暴力做 第三个点没过 看了别人的博客 认识了前缀数组
错误代码
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Dist[];
int N, M;
int Sum;
void Ans(int i, int j)
{
int sum = ;
if (i > j)Ans(j, i);
else
{
for (int k = i; k < j; k++)
sum += Dist[k];
sum = (sum < (Sum - sum)) ? sum : Sum - sum;
cout << sum << endl;
}
}
int main()
{ cin >> N;
for (int i = ; i <= N; i++)
{
cin >> Dist[i];
Sum += Dist[i];
}
cin >> M;
int v1, v2;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
Ans(v1, v2);
}
}

ac代码

 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
vector<int>Dist;
int sum;
int main()
{
int N;
cin >> N;
Dist.resize(N + );
for (int i = ; i <=N; i++)
{
int temp;
cin >> temp;
sum += temp;
Dist[i] = sum;
}
int M;
cin >> M;
int v1, v2;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
if (v1 > v2)
swap(v1,v2);
int s1 = Dist[v2 - ] - Dist[v1 - ];
int s2 = sum - s1;
if (s1 > s2)
cout << s2 << endl;
else
cout << s1 << endl;
}
}

1046 Shortest Distance (20分)的更多相关文章

  1. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  2. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  3. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  4. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

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

    题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...

  6. PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  7. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  8. 1046 Shortest Distance (20)

    #include<stdio.h> int main() { int n,m,a,b,tem,pre,p; int i,j; ]; while(scanf("%d",& ...

  9. PAT (Advanced Level) 1046. Shortest Distance (20)

    处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

随机推荐

  1. 数据库中的两个最重要的日志redo log和binlog

    mysql整体来看其实只有两部分,一部分是server层,一部分是引擎层. 1.redo log(重做日志):当有一条记录需要更新的时候,InnoDB 引擎就会先把记录写入redo log里面,并更新 ...

  2. pyppeteer基本使用demo

    # -*- coding: utf-8 -*- # 类似selenium,支持异步,不需要再单独安装环境,pyppeteer自动安装环境 # 异步await要写到一个函数的内部 from pyppet ...

  3. drf(请求封装/认证/权限/节流)

    1.请求的封装 class HttpRequest(object): def __init__(self): pass @propery def GET(self): pass @propery de ...

  4. MySQL记录操作(单表查询)

    单表查询的语法及关键字执行的优先级 单表查询语法 SELECT DISTINCT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER ...

  5. vue基础----自定义组件directive ,bind,update,insert

    <div id="app"> <input type="text" v-limit.3="msg" v-focus> ...

  6. vscode 对于 md的编写 左侧 大纲 很重要!!

    vscode 对于 md的编写 左侧 大纲 很重要!!

  7. 初学嵌入式Linux

    初学嵌入式Linux,感觉需要学习的东西太多了.把学习过程中的收获和问题记录在这里,算是一份经验吧.     前面利用开发板带的现成的东西step by step让Linux 2.4.19在开发板上跑 ...

  8. python enumerate用法总结(转)

    原文链接:https://blog.csdn.net/churximi/article/details/51648388 enumerate()说明 enumerate()是python的内置函数en ...

  9. [组件封装]微信小程序-底部弹框

    描述 模仿ios浏览器底部弹框效果. 遮罩层淡入淡出,弹框高度根据内容自适应. 效果 源码 popup-bottom.wxml <!-- popup-bottom.wxml --> < ...

  10. VMware 虚拟机正在使用中

    1.出现报错信息: 2.找到安装目录下面的.lck后缀文件夹(如有多个则全部删除) 3.删除此文件夹 4.成功开启虚拟机