这道题,折磨了我一个多小时,前前后后写了三个算法。

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,10​5​​]), followed by N integer distances D​1​​D​2​​ ⋯ D​N​​, where D​i​​ is the distance between the i-th and the (i+1)-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 (≤10​4​​), 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 10​7​​.

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的更多相关文章

  1. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

  2. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  3. PAT甲级——A1046 Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  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. 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 ...

  6. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  7. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  8. PAT 1046 Shortest Distance

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  9. PAT 1046 Shortest Distance[环形][比较]

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

随机推荐

  1. Entity Framework的扩展库

    https://github.com/jcachat/EntityFramework.DynamicFilters Provides global & scoped filters for E ...

  2. python实现连续子数组的最大和

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  3. json 序列化和反序列化的3个方法

    https://www.cnblogs.com/caofangsheng/p/5687994.html

  4. 测试 jdbc 中连接关闭的时机

    测试 jdbc 中连接关闭的时机 写一段程序,测试 jdbc 连接的关闭情况 /** * 测试 jdbc 连接的关闭情况 */ public static void testOpenCon(){ // ...

  5. 前端css盒模型及标准文档流及浮动问题

    1.盒模型 "box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型有两种:标准模型和IE模型.这里重 ...

  6. django连接mysql数据库配置,出现no module named mysqldb报错

    作为一个菜鸟运维也是要有梦想的,万一学会了python走向人生巅峰了呢.好吧,都是瞎想,今天主要介绍下django配置,最近也开始摸索这个牛b框架了,当然大佬肯定不屑一顾,都是照顾照顾我们这些菜鸟初学 ...

  7. sql server几种Join的区别测试方法与union表的合并

    /* sql server几种Join的区别测试方法 主要来介绍下Inner Join , Full Out Join , Cross Join , Left Join , Right Join的区别 ...

  8. GNU汇编 伪指令

    伪指令 本身并没有所对应的机器码 它只是在编译的时候起作用,或者转换为其他的实际指令来运行 global ascii byte word data equ align @ 下面的例子是在数据段存放数据 ...

  9. JQ常用方法(哈哈)

    1ajax请求 $(function(){   $("#send").click(function(){     $.ajax({     type:"get" ...

  10. JZOJ 5922. sequence

    5922. [NOIP2018模拟10.23]sequence (File IO): input:sequence.in output:sequence.out Time Limits: 1000 m ...