思路

这道题,来自洛谷官方题单中的线性表,大概猜测是线性的数据结构。

一开始看到这道题,我直接想到的是 链表 ,因为有序+频繁的插入和删除操作,于是直接写了一发链表的代码,基本可以作为链表的模版代码了(虽然acm里没啥人用链表吧)

下面还是把 不能完全通过 的代码贴出来吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

struct node{
int v;
node* next;
node(int x) : v(x),next(nullptr){}
};

void insAtHead(node*& head,int x){
node* newnode = new node(x);
newnode->next = head;
head = newnode;
}

void insAtTail(node*& head,int x){
node* newnode = new node(x);
if(!head){
head = newnode;
return;
}
node* p = head;
while(p->next) p = p -> next;
p->next = newnode;
}

void insAtP(node*& head,int pos, int x){
if(pos == 0){
insAtHead(head,x);
return;
}

node* p = head;
int i = 0;
while(p && i < pos - 1){
p = p -> next;
i++;
}
if(i != pos- 1){ //越界
return;
}
node* newnode = new node(x);
newnode->next = p ? p -> next : nullptr;
if(p) p->next = newnode;
}

void delByVal(node*& head,int x){
if(!head) return;
if(head->v == x){
node* t = head;
head = head->next;
delete t;
return;
}
node* p = head;
while(p->next && p->next->v != x){
p = p -> next;
}
if(p -> next){
node* t = p->next;
p->next = t->next;
delete t;
}
}

void delByPos(node*& head,int pos){
if(!head) return;
if(pos == 0){
node* t = head;
head = head->next;
delete t;
return;
}
node* p = head;
int i = 0;
while(p ->next && i < pos - 1){
p = p ->next;
i++;
}
if(p ->next){
node* t = p->next;
p->next = t->next;
delete t;
}
}

node* findv(node*& head, int x){
node* p = head;
while(p){
if(p->v == x) return p;
p = p -> next;
}
return nullptr;
}

int findp(node*& head, int x){
node* p = head;
int pos = 0;
while(p){
if(p->v == x) return pos;
p = p -> next;
pos ++;
}
return pos;
}

void print(node*& head){
node* p = head;
while(p){
cout << p->v << " ";
p = p ->next;
}
cout << endl;
}

signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
node* head = nullptr;
insAtHead(head,1);
//print(head);
for(int i = 2 ; i <= n ; i++){
int k , p;
cin >> k >> p;
node* t = findv(head,k);
int pos = findp(head,k);
if(p == 0){
insAtP(head, pos,i);
//print(head);
}
else{
insAtP(head, pos + 1, i);
//print(head);
}
}
int m;
cin >> m;
while(m--){
int x;
cin >> x;
delByVal(head, x);
//print(head);
}
print(head);
return 0;
}

这个代码交上去是2绿3t,拿40分,因为想想就大概知道,如果每次插入都是在末尾,那么这样的话每次查找都需要完整遍历一遍当前的链表,时间复杂度是 $1 + 2 + 3 + ··· +(n - 1)$ ,大概是 $O(n^2)$ ,显然不能通过这道 $1e5$ 的题。


那么接下来就要考虑,到底是什么数据结构能完成:有序 + 频繁的插入删除 + 快速查找 呢

基于红黑树的 map 和基于哈希表的 umap 好像是能完成后两个,STL库里的 list 好像只能完成前两个,而常用的 vector 好像只能完成第一个

可恶,难道就要到此为止了吗?……

不,我们还有 静态双向链表 (数组模拟)

什么是静态双向链表,其实是3个数组,llink 记录的是每个元素左边的元素,rlink 记录的是右边的元素,而 exi 数据记录元素是否还在链表中

这样就可以将插入、删除、遍历的时间都降到 $O(1)$

最后还要打印一次链表,总时间复杂度为 $O(n)$

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define N 100005
using namespace std;

int llink[N], rlink[N];
bool exi[N] = {false};

signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
exi[1] = true;
llink[1] = 0, rlink[1] = 0; //0代表空
for(int i = 2 ;i <= n; i++){
int k, p;
cin >> k >> p;
if(p == 0){
int l = llink[k];
rlink[i] = k;
llink[i] = l;
if(l) rlink[l] = i; //l可能是空
llink[k] = i;
}
else{
int r = rlink[k];
rlink[i] = r;
llink[i] = k;
if(r) llink[r] = i; //r可能是空
rlink[k] = i;
}
exi[i] = true;
}
int m;
cin >> m;
while(m--){
int x;
cin >> x;
if (!exi[x]) continue;
int l = llink[x];
int r = rlink[x];
if(l) rlink[l] = r;
if(r) llink[r] = l;
exi[x] = false;
}
int head = 1;
while(llink[head]) head = llink[head]; //从1开始往左找到第一个节点
while(head){
cout << head << " "; //从第一个节点开始往后遍历
head = rlink[head];
}
cout << endl;
return 0;
}