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

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. 20145238-荆玉茗 《Java程序设计》第6周学习总结

    20145238 <Java程序设计>第6周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·如果要将数据从来源中取出,可以使用输入串流,若将数据写入目的地,可以使用输出串流. ...

  2. 1.初识Quartz

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/QuartzDemo.git 前言: 接触一个新事物的开始,我们都会产生一些疑问: Quartz是什 ...

  3. JS-输入数字输出大写中文

    function(n) { var fraction = ['角', '分']; var digit = [ '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', ...

  4. JQuery发起ajax请求,并在页面动态的添加元素

    页面html代码: <li> <div class="coll-tit"><span class="coll-icon">& ...

  5. FAT32中文版分析+补充(1)

    概述 起先所有的FAT文件系统都是为IBM PC机器而设计的,这说明了一个重要的问题:FAT文件系统在磁盘上的数据是用“小端”(Little Endian)结构存储的.我们使用4个8-bit的字节—— ...

  6. IntelliJ IDEA 方法注释教程

    首先Ctrl +Alt +S ,打开Settings ,找到Live Template,然后点击右侧的绿色的“+”,选择Template Group 然后给新建的Group随便命个名 选中自己刚才创建 ...

  7. 统计寄存器AX中1 的个数

    ;==================================== ; 统计寄存器AX中1 的个数 DATAS segment DATAS ends CODES segment START: ...

  8. 服务器缺少vcruntime140.dll,无法运行

    Redis用了一段时间,有的时候,调试的时候,RedisDesktop是个不错的工具 当我想在服务器上安装的时候,才发现服务器64位的环境里面运行出错了 百度上有共享dll出来的,但是基本都没法用,虽 ...

  9. 【Effective C++读书笔记】序

    C++ 是一个难学易用的语言! [C++为什么难学?] C++的难学,不仅在其广博的语法,以及语法背后的语义,以及语义背后的深层思维,以及深层思维背后的对象模型: C++的难学还在于它提供了四种不同而 ...

  10. 利用python进行坐标提取以及筛选(文件操作的小应用)

    由于目前暂时还未学习到python关于数据处理的模块方面的知识,且刚好最近朋友发来一份坐标数据文件(txt格式),让我帮他对其进行筛选, 因此利用了最近刚学过的python文件处理操作以及以前所学的基 ...