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
通常算法:
#include <iostream>
using namespace std;
int main()
{
/**
该方法运行超时
*/
int N,M,startNum,endNum,sum1,sum2,tmp;
cin>>N;
int graphic[N];//数组模拟简单图
for(int i=;i<N;i++){
cin>>graphic[i];
}
cin>>M;
while(M--){
sum1=;sum2=;
cin>>startNum>>endNum;
startNum--;endNum--;
if(startNum>endNum){
tmp=startNum;
startNum=endNum;
endNum=tmp;
}
for(int i=;i<N;i++){
if(i>=startNum&&i<endNum) sum1+=graphic[i];
else sum2+=graphic[i];
}
cout<<(sum1>sum2?sum2:sum1)<<endl;
}
/**
读题:
input
N个数 Dn为第i和i+1的出口距离,最后一个数是和第一个数的距离
M行 每行是出口
*/
system("pause");
return ;
}

优化后:

#include <iostream>
using namespace std;
int main()
{
/**
查阅参考https://www.jianshu.com/p/cb54521fda65
可以使用贪心算法
在输入的同时计算每个点到第一个点的距离,
并将它存放在数组dis中。两点的距离要么环
的劣弧,要么是环的优弧,这里先固定一下即
求由a到b的距离(a<b),另一端距离用总round
trip distance(圆环总长度)减去这里求的距离,比较两者取最小值,特别地,总距离程序中用虚拟的第n+1点表示,因为它到第一个点的距离恰好等于圆环长度。
*/
int N,M,sum,A,B,tmp,shortDistance1,shortDistance2;
cin>>N;int distance[N]={};
for(int i=;i<N;i++){
cin>>distance[i];
if(i!=) distance[i]+=distance[i-];//每次计算总长
sum=distance[i];
}
cin>>M;
while(M--){
/**这边比我之前少了一个复杂度*/
cin>>A>>B;
A--;B--;
if(A>B){
tmp=A;
A=B;
B=tmp;
}
shortDistance1=(B-==-?:distance[B-])-(A-==-?:distance[A-]);
shortDistance2=sum-shortDistance1;
cout<<(shortDistance1>shortDistance2?shortDistance2:shortDistance1)<<endl;
}
system("pause");
return ;
}

PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)的更多相关文章

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

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

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

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

  3. 1046 Shortest Distance (20 分)

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

  4. 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 ...

  5. 1046 Shortest Distance (20分)

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

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

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

  7. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

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

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

  9. PAT A1046 Shortest Distance (20 分)

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

随机推荐

  1. antd form表单一行多个组件并对其校验

    一行一个标签对应多个输入组件,这个需求很常见但在官方例子没看到合适的,因为官方建议: 注意:一个 Form.Item 建议只放一个被 getFieldDecorator 装饰过的 child,当有多个 ...

  2. html上传文件

    不太懂Html 做备用 html的文件上传分两个 第一个是Html文件 浏览器加载用的 另一个是PHP文件 处理上传文件的 下面是Html文件 叫index.html <html> < ...

  3. DB2创建视图并授权给其他用户

    创建视图并授权给其他用户 可以在操作系统界面.或者DB2交互界面下进行数据库操作 查看数据库节点[db2inst1@ELONEHR-DB ~]$ db2 list db directory Syste ...

  4. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  5. python - 标准库:subprocess模块

    subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定义了一个类: Popen. subprocess.Popen(args, bufsize=0, execut ...

  6. C—LINQ小结

    LINQ代表语言集成查询(Language-Integrated Query),它包括用于从数据源检索信息的一组功能.数据检索是许多程序的重要组成功能. 简介:System.Linq; var num ...

  7. C# user32.dll 找窗口 填数据

    工具:SpyLite [DllImport("user32.dll", EntryPoint = "FindWindow")] private extern s ...

  8. Slience is the sleep that nourishes wisdom

    cumulative: 积聚的 lag. v. 落后 backfire. n. 事与愿违 segregated. adj. 分隔的 back-and-forth: 来回地 initiative. ad ...

  9. 多线程13-CountdownEvent

        );         ));             ));             t1.Start();             t2.Start();             _coun ...

  10. 关于this与e.target区别以及data-*属性

    1 this与event.target 在编写事件函数时可以传入一个event参数,even参数可以使用一个target属性如even.target用以调用,其作用是指向返回事件的目标节点(触发该事件 ...