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 ...
随机推荐
- Ping链路测试
https://help.aliyun.com/knowledge_detail/40573.html?spm=5176.2020520165.121.d519.4b4f7029sHzfmi#TRAC ...
- linux inotify 监控文件系统事件
https://blog.csdn.net/cheng_fangang/article/details/41075515
- WPF 绑定数据方式
======================================================================== Foreground="{Binding P ...
- MYSQL5.7源码包编译安装
Centos下用cmake编译安装MySQL 5.7安装依赖包yum -y install gcc gcc-c++ ncurses ncurses-devel cmake下载相应源码包cd /usr/ ...
- 阶段3 1.Mybatis_09.Mybatis的多表操作_9 mybatis多对多操作-查询用户获取用户所包含的角色信息
sql语句以user作为主表 用户的全部信息,以为用户下的角色的.并不是所有的用户都有角色,有角色的就带角色.没角色的就为null 首先修改实体类 定义List<Role> 生成gette ...
- 【Fiddler】开启手机的http或https抓包
fiddler安装 下载fiddler最新版: 默认安装: 打开fiddler工具,默认界面: 选择上方,Tools-→options General界面 HTTPS界面 CONNECTIONS,po ...
- 【MM系列】SAP MM模块-控制采购订单中某些项目的输出显示
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-控制采购订单中某些 ...
- 【Linux开发】【DSP开发】利用CCS6.1生成out文件的同时生成bin文件
[Linux开发][DSP开发]利用CCS6.1生成out文件的同时生成bin文件 标签:[DSP开发] [Linux开发] 尝试在windows上安装的CCS6.1开发AM4378-Linux下的应 ...
- 多线程16-SpinWait
); isCompleted = )); isCompleted = ); isCompleted = true; ...
- 将从model中获得的数据传到js函数中
刚遇到了一种情况,从controller中获得的model是一个集合,需要将这个集合循环放到标签中,并且需要为这些标签添加点击事件,每个值传入对应的点击事件函数中,由于model中的值是通过${ite ...