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 ...
随机推荐
- nginx缓存批量清除
研究了一段时间的缓存清除,说说了解的三种方式吧. 1. 原始的只增加缓存模块的,根据访问的路径一条条清除. 根据此方式要进行批量清除的话,必须在设定的缓存目录下通过自己写的程序来读取ng ...
- 文本编辑器Vim技巧
1. 导入文件内容 :r 文件名 2. 插入当前日期 :r !date 3. :!which ls 4. :r !命令
- Shell编程学习之重定向
这一篇讲一下重定向 有些时候你想要保存某些命令产生的输出而不是在显示器上显示它. 为了应对这样的问题 bash shell 也就提供了一些重定向的操作符. 我们先了解一些基本的应用. 输出重定向 输出 ...
- JavaEE权限管理系统的搭建(二)--------聚合工程项目的创建和依赖关系
本项目是一个聚合工程,所以要先搭建一个聚合工程的框架 搭建完成的项目结构图如下: 首先创建父项目:pom类型 子模块:web层的搭建,war类型 把这个两个目录标记为对应的类型 其他子模块:和serv ...
- Vue nodejs商城项目-搭建express框架环境
1.express-project 搭建express框架环境 安装express generator生成器 通过生成器自动创建项目 配置分析 安装 cnpm i -g express-generat ...
- iOS 检测版本更新(02)
iOS 检测版本更新 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bu ...
- jQuery 效果使用
.hide() 隐藏匹配的元素. .hide() 这个方法不接受任何参数. .hide([duration][,complete]) duration 一个字符串或者数字决定动画将运行多久. comp ...
- vue数据绑定html
html标签的纯文本显示/被当做html标签处理: 1)使用两个大括号时,假如字符串内容是html标签,那么不会被转义: 2)使用三个大括号时,字符串内的html标签会被直接转义 a.两个大括号: & ...
- python__系统 : socket_TCP相关
tcp和udp对比起来.还是tcp相对稳定一些,但是因为有三次挥手和四次握手,以及确认包(ack)的存在,可能在速度上会比udp慢. 用python的socket模块可以建立tcp服务端: from ...
- python中的列表内置方法小结
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- ''' names=['zhangyu','mahongyan','zhangguobin','shac ...