资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
有n辆自行车依次来到停车棚,除了第一辆自行车外,每辆自行车都会恰好停放在已经在停车棚里的某辆自行车的左边或右边。(e.g.停车棚里已经有3辆自行车,从左到右编号为:3,5,1。现在编号为2的第4辆自行车要停在5号自行车的左边,所以现在停车棚里的自行车编号是:3,2,5,1)。给定n辆自行车的停放情况,按顺序输出最后停车棚里的自行车编号。
输入格式
第一行一个整数n。
第二行一个整数x。表示第一辆自行车的编号。
以下n-1行,每行3个整数x,y,z。
z=0时,表示编号为x的自行车恰停放在编号为y的自行车的左边
z=1时,表示编号为x的自行车恰停放在编号为y的自行车的右边
输出格式
从左到右输出停车棚里的自行车编号
样例输入
4
3
1 3 1
2 1 0
5 2 1
样例输出
3 2 5 1
数据规模和约定
n<=100000
自行车编号为不超过100000的正整数。
这个题我们很轻易地可以想到利用链表进行添加,于是乎代码很轻易得写了出来
#include <iostream>
#include <vector>
#include<stack>
#include<set>
using namespace std;
//链表结构体
struct linked {
int index;
linked* front;//前驱
linked* next;//后继
//构造函数
linked(int index) {
this->index = index;
front = NULL;
next = NULL;
}
};
//加在左边
void addleft(linked* head, int x, int y) {
linked* pre = head->next;
while (pre != NULL && pre->index != y)
pre = pre->next;
linked* temp = new linked(x);
linked* front = pre->front;
front->next = temp;
temp->next = pre;
temp->front = front;
pre->front = temp;
}
//加在右边
void addright(linked* head, int x, int y) {
linked* pre = head->next;
while (pre != NULL && pre->index != y)
pre = pre->next;
linked* temp = new linked(x);
if(pre->next!=NULL){
linked* next = pre->next;
pre->next = temp;
temp->front = pre;
temp->next = next;
next->front = temp;
}
else {
pre->next = temp;
temp->front = pre;
}
}
int main() {
int n;
cin >> n;
linked* head = new linked(0);
/*head->front = NULL;
head->next = NULL;*/
int in;
cin >> in;
linked* fi = new linked(in);
head->next = fi;
fi->front = head;
while (--n) {
int x, y, z;
cin >> x >> y >> z;
if (z == 0) {
addleft(head, x, y);
}
else {
addright(head, x, y);
}
}
linked* pre = head->next;
while (pre != NULL) {
cout << pre->index << " ";
pre = pre->next;
}
return 0;
}
但是我们提交之后发现超时
我们便应该想如何减少时间复杂度,map查找复杂度为On(1),便可以利用map
#include <iostream>
#include <vector>
#include<stack>
#include<map>
using namespace std;
//链表结构体
struct linked {
int index;
linked* front;//前驱
linked* next;//后继
//构造函数
linked(int index) {
this->index = index;
front = NULL;
next = NULL;
}
};
//map 保存结点,声明为全局变量,以便函数调用
map<int, linked*> nhash;
//加在左边
void addleft( int x, int y) {
//获得位置
linked* pre = nhash[y];
linked* temp = new linked(x);
linked* front = pre->front;
front->next = temp;
temp->next = pre;
temp->front = front;
pre->front = temp;
nhash[x] = temp;
}
//加在右边
void addright( int x, int y) {
linked* pre = nhash[y];
linked* temp = new linked(x);
if(pre->next!=NULL){
linked* next = pre->next;
pre->next = temp;
temp->front = pre;
temp->next = next;
next->front = temp;
}
else {
pre->next = temp;
temp->front = pre;
}
nhash[x] = temp;
}
int main() {
int n;
cin >> n;
//构造头节点,以便输出
linked* head = new linked(0);
//第一个车子的放置
int in;
cin >> in;
linked* fi = new linked(in);
head->next = fi;
fi->front = head;
nhash[in] = fi;
while (--n) {
int x, y, z;
cin >> x >> y >> z;
if (z == 0) {
addleft( x, y);
}
else {
addright(x, y);
}
}
linked* pre = head->next;
//输出
while (pre != NULL) {
cout << pre->index << " ";
pre = pre->next;
}
return 0;
}
修改之后,便可以通过
ps:吐槽一下蓝桥杯的训练系统,不能用unordered_map,nullptr,是真的拉