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 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 D1 D2 ⋯ DN, where Di is the distance between the i-th and the (-st exits, and DN 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 分) (知识点:贪心算法)的更多相关文章
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- 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 ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- 【PAT甲级】1046 Shortest Distance (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
- PAT (Advanced Level) 1046. Shortest Distance (20)
处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
随机推荐
- 【Spark机器学习速成宝典】基础篇03数据读取与保存(Python版)
目录 保存为文本文件:saveAsTextFile 保存为json:saveAsTextFile 保存为SequenceFile:saveAsSequenceFile 读取hive 保存为文本文件:s ...
- [torch] pytorch hook学习
pytorch hook学习 register_hook import torch x = torch.Tensor([0,1,2,3]).requires_grad_() y = torch.Ten ...
- leetcode-mid-backtracking-17. Letter Combinations of a Phone Number
mycode 68.26% class Solution(object): def letterCombinations(self, digits): """ :typ ...
- 安装ubuntu双系统
今天在win7下安装ubuntu14.1双系统,别折磨了一个下午.主要是开机系统引导问题. 引导程序是位于硬盘最前面的一段程序,由于扇区大部分是引导程序,故也成引导扇区.此外包含有硬盘的分区信息,共6 ...
- Vue知识整理16:单文件组件
过程较为复杂,这里直接写出视频地址,可以直接查看 https://learning.dcloud.io/#/?vid=14
- 我们建了一个 Golang 硬核技术交流群(内含视频福利)
目录 目录 Golang 是什么? 我们为什么选择 Golang? Golang 是云时代的宠儿! 我们搞了一场 Golang 入门直播 Golang 是什么? Golang 是谷歌 2009 年发布 ...
- oracle data guard --理论知识回顾02
继上一篇 管理影响物理standby的事件 1 创建表空间或数据文件初始化参数standby_file_management用来控制是否自动将primary数据库增加表空间或数据文件的改动,传播到st ...
- 解析jmeter阶梯加压与请求的计算
用百度做例子 我们设置阶梯加压线程组的请求参数,如下图 上图表示 1:每隔2秒钟,会在1秒内启动5个线程 2:每次线程加载之后都会运行2s然后开始下一次线程加载 3:最终会加载50个线程并持续运行30 ...
- Windows.命令行(CMD)_执行命令&环境变量
1.CMD命令中如果 命令有换行的话,就使用 ^来连接(这就类似于 Linux命令行中 \ 的作用) 2.环境变量 2.1.显示 所有环境变量的值,命令:set 2.2.显示 某个环境变量的值,命令 ...
- Mac020--常用插件
Google浏览器常用插件 1.github插件octotree 2.掘金Chrome网上应用商店 2-1.掘金/老司机的神兵利器 2-2.好用的Google插件:来自掘金 3.Gliffy Diag ...