PAT——甲级1046S:shortest Distance
这道题,折磨了我一个多小时,前前后后写了三个算法。
1046 Shortest Distance (20 point(s))
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,105]), followed by N integer distances D1D2 ⋯ DN, where Di is the distance between the i-th and the (i+1)-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 (≤104), 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 107.
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
题目特很简单,就是一个多个点组成的环形,求两个点之间的最短距离。阳历第一行5代表5个点,后面第i个数代表第一个点到第i+1个点的距离。等等。。。
然后我第一个想法是循环链表,第一次完成的写链表的代码,废了九牛二虎之力,写了一个循环链表,然后就贼舒服的求最短距离。
先贴出来的我写的代码
#include<cstdio>
class Node
{
public:
int num;
int dis;
Node* next;
Node* pre;
}; class cycleLinkList
{
public:
cycleLinkList();
void insert_back(int num, int dis);
int find_nearest(int prenum,int lastnum);
//void displayAll(void);
private:
Node* head;
};
cycleLinkList::cycleLinkList()
{
head = new Node;
head->num = ;
head->pre = head;
head->next =
head;head->dis = ;
}
void cycleLinkList::insert_back(int num, int dis) {
Node* pre = head->next;
while (pre->next!=head)
{
pre = pre->next;
}
Node* new_Node=new Node;
new_Node->dis = dis;
new_Node->num = num;
new_Node->next = head;
new_Node->pre = pre;
pre->next->pre = new_Node;
pre->next = new_Node;
}
int cycleLinkList::find_nearest(int prenum, int lastnum) {
Node* pre = head->next;
while (pre->num != prenum)
{
pre = pre->next;
}
int predistance=,nextdistance=;
Node* pre_dir = pre;
while (pre_dir->num != lastnum)
{
pre_dir = pre_dir->pre;
predistance += pre_dir->dis; }
Node* next_dir = pre;
while (next_dir->num != lastnum)
{
nextdistance += next_dir->dis;
next_dir = next_dir->next;
}
if (nextdistance >= predistance)return predistance;
else return nextdistance; }
/*
void cycleLinkList::displayAll(void)
{
Node* _head = head->next;
while (_head != head)
{
printf("%d %d\n", _head->num, _head->dis);
_head = _head->next;
}
printf("pre\n");
_head = head->pre;
while (_head != head)
{
printf("%d %d\n", _head->num, _head->dis);
_head = _head->pre;
}
}*/ int main()
{
int n,m,dis;
scanf("%d", &n);
cycleLinkList data;
for (int i = ; i < n; i++){
scanf("%d", &dis);
data.insert_back(i + , dis);
}
//data.displayAll();
//printf("insert");
scanf("%d", &m);
for (int i = ; i < m; i++){
int pre_num, last_num;
scanf("%d%d", &pre_num,&last_num);
printf("%d\n",data.find_nearest(pre_num, last_num));
}
return ;
}
写完之后运行很正常,当然是很舒服啦,但是。。。提交给OJ就是运行超时,我也很无奈,循环链表那么直观,竟然不让用。
那我就用数组呗,第二个想法,用数组。
#include<cstdio>
int main()
{
int dis[],n,m,total_dis=;
memset(dis, , (*sizeof(int)));
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%d", &dis[i]);
total_dis += dis[i];
}
scanf("%d", &m);
for (int i = ; i < m; i++) {
int prenum, lastnum,nextdis=;
scanf("%d%d", &prenum, &lastnum);
if (prenum > lastnum) { int temp = prenum;prenum = lastnum;lastnum = temp; } for (int j = prenum - ;j < lastnum - ;j++)
nextdis += dis[j];
if (total_dis - nextdis >= nextdis) printf("%d\n", nextdis);
else printf("%d\n", total_dis-nextdis);
}
return ;
}
不过这个代码交上去,还是有一个运行超时。牛客PATOJ的全部通过,但PAT官网OJ最后一个超时。
主要原因出在我每次计算最短距离都要遍历数组,大概相当于O(m*n)的复杂度。运行时间太长。
然后我就求助教材了,发现可以给数组保存为第i个元素是第i个点到第i个点的距离,然后求两个点a,b之间的距离就可以直接dis[b]-dis[a]了。(其中a<b,否则交换)
这样的话就输入的时候遍历n次,找最短距离的时候遍历m次就行了。
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int dis[], n, m, total_dis = ;
scanf("%d", &n);
for (int i = ; i < n; i++)
{
int temp;
scanf("%d", &temp);
dis[i] = total_dis;
total_dis += temp;
//printf("i: %d dis:%d total:%d\n", i, dis[i],total_dis);
}
scanf("%d", &m);
for (int i = ; i < m; i++) {
int prenum, lastnum;
scanf("%d%d", &prenum, &lastnum);
if (prenum > lastnum) swap(prenum, lastnum);
int temp = dis[lastnum-] - dis[prenum-];
printf("%d\n",min(temp,total_dis-temp));
}
return ;
}
然后终于通过啦。
这也学到了一点,拿到题先想想有没有简化的方法,按照最简单的思维去写代码,往往可能运行超时。
就像这道题最后一种方法,就简化了很多。越简单的题越需要脑子,不要一拿上来就做。
int main(){int dis[100005],n,m,total_dis=0;memset(dis, 0, (100005*sizeof(int)));scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%d", &dis[i]);total_dis += dis[i];}scanf("%d", &m);for (int i = 0; i < m; i++) {int prenum, lastnum,nextdis=0;scanf("%d%d", &prenum, &lastnum);if (prenum > lastnum) { int temp = prenum;prenum = lastnum;lastnum = temp; }for (int j = prenum - 1;j < lastnum - 1;j++)nextdis += dis[j];if (total_dis - nextdis >= nextdis) printf("%d\n", nextdis);else printf("%d\n", total_dis-nextdis);}return 0;}
PAT——甲级1046S:shortest Distance的更多相关文章
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT甲级——A1046 Shortest Distance
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) 2016-09-09 23:17 22人阅读 评论(0) 收藏
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- 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 ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT 1046 Shortest Distance
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT 1046 Shortest Distance[环形][比较]
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
随机推荐
- PHP APC安装与使用
先要解决一下httpd-devel依赖库问题 yum install cyrus-sasl-devel db4-devel openldap apr apr-util apr-util-devel p ...
- Spring boot 集成三种拦截方式
三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...
- computed--实时计算属性
项目:https://github.com/ccyinghua/vue-node-mongodb-project/blob/master/07-shoppingCart.md 实时计算属性的compu ...
- 在Linux文件清空的几种方法
在Linux文件清空的几种方法 1.使用重定向的方法 [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > tes ...
- 给网站添加icon图标
只需制成ico结尾的图片即可
- docker官方仓库下载镜像
官方仓库镜像地址:https://hub.docker.com/search/ 以下载mysql为例 进入到详情页后我们看到有很多Tags 我们选择5.7.25版本进行下载 # docker pull ...
- oracle中序列,同义词的创建
序列 序列是用来生成唯一,连续的整数的数据库对象.序列通常用来自动生成主机那或唯一键的值.序列可以按升序排序, 也可以按降序排序.例如,销售流水表中的流水号可以使用序列自动生成. 创建序列语法: cr ...
- centos7重启apache、nginx、mysql、php-fpm命令
apache启动systemctl start httpd停止systemctl stop httpd重启systemctl restart httpd mysql启动systemctl start ...
- Python必学:使用哪款文本编辑器更好?
Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个文件,这样,程 ...
- Pandas 数据读取
1.读取table # 读取普通分隔数据:read_table # 可以读取txt,csv import os os.chdir('F:/') #首先设置一下读取的路径 data1 = pd.read ...