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. java第二周小结

    这是接触Java的第一周,了解这个语言的一些基础知识,下面是对这段时间重要知识点的汇总 一.Java是一种面向对象的语言    特点为:简洁高效.可移植性.适合分布式计算.健壮防患于未然的特性.多线程 ...

  2. SQL Server CDC最佳实践

    企业核心业务系统oltp的数据需要通过ETL同步到数据仓库,原始的ETL流程通过定制化从SQL Server中进行数据抽取,经过生产环境的监控,发现ETL过程的query会对生产系统造成额外负载.于是 ...

  3. legend3---2、网站的代码里面的/也是代表根目录

    legend3---2.网站的代码里面的/也是代表根目录 一.总结 一句话总结: 不过这个根目录在public下面 1.mysql删除数据库命令? drop database <数据库名> ...

  4. SpringBoot(十二):SpringBoot整合Kafka

    https://blog.csdn.net/saytime/article/details/79950635

  5. SetWindowsHookEx失败

    使用下面代码hook鼠标 res = SetWindowsHookEx(WH_MOUSE_LL, _mouseHookProcedure, Marshal.GetHINSTANCE(System.Re ...

  6. 十九、RF接口测试汇总(一)

    搭建项目:转自  http://chuansong.me/n/1858477 A.请求方式为get请求 方式一:导入RequestsLibrary库,get request    [ alias | ...

  7. Jmeter之线程组(Stepping和Ultimate)

    jmeter自带的线程组比较简单,如果需要逐渐增加并发数的功能并不能实现,所以就需要使用Jmeter插件--Stepping Thread Group. 一.安装Stepping/UItimate T ...

  8. unity监听粒子播放结束

    需要在粒子上挂脚本 脚本添加代码 public void OnParticleSystemStopped() { Debug.Log("粒子停止"); } 这是个生命周期 粒子的 ...

  9. C盘无损扩容(傻逼拯救者128G固态分两个盘)

    下载DiskGenius.exe 进行拆分分区(我从d盘拆分出20G给c盘) 然后右键此电脑,管理->磁盘管理 选中刚分出来的20G空间指向到c盘

  10. Maven 项目中的groupId和artifactId

    maven进行项目管理,如果我们要将项目加入到maven到本地仓库中,则需要对项目进行唯一性标示,而groupId和artifactId就起到这样对作用. groupId为项目组织对唯一标识符,可以理 ...