-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathA1097.cpp
More file actions
60 lines (51 loc) · 1.44 KB
/
Copy pathA1097.cpp
File metadata and controls
60 lines (51 loc) · 1.44 KB
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
#include <cstdio>
#include <algorithm>
#define MAXN 100010
#define MAX_KEY 10005
using namespace std;
// step 1: define
struct Node {
int addr, data, next, order;
} list[MAXN];
bool is_existed[MAX_KEY] = {};
int cmp(Node a, Node b)
{
return a.order < b.order;
}
int main()
{
// step 2: initialize
for (int i = 0; i < MAXN; i++)
list[i].order = 2 * MAXN; // set all as invalid data
int head, n;
scanf("%d %d", &head, &n);
for (int i = 0; i < n; i++)
{
int addr;
scanf("%d", &addr);
scanf("%d %d", &list[addr].data, &list[addr].next);
list[addr].addr = addr;
}
// step 3: purge
int remain_cnt = 0, removed_cnt = 0, p = head;
while (p != -1)
{
int data = list[p].data * (list[p].data < 0 ? -1 : 1);
if (is_existed[data])
list[p].order = MAXN + removed_cnt++;
else
{
list[p].order = remain_cnt++;
is_existed[data] = true;
}
p = list[p].next;
}
// step 4: sort
sort(list, list + MAXN, cmp);
// step 5: output
for (int i = 0; i < remain_cnt; i++)
printf(i == remain_cnt - 1 ? "%05d %d -1\n" : "%05d %d %05d\n", list[i].addr, list[i].data, list[i+1].addr);
for (int i = remain_cnt; i < remain_cnt + removed_cnt; i++)
printf(i == remain_cnt + removed_cnt - 1 ? "%05d %d -1\n" : "%05d %d %05d\n", list[i].addr, list[i].data, list[i+1].addr);
return 0;
}