-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitLinkedList.cpp
125 lines (109 loc) · 2.35 KB
/
splitLinkedList.cpp
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
#include<vector>
#include<unordered_map>
#include<iostream>
using namespace std;
struct node
{
int val;
string next, add;
};
int main(){
string fadd;
int l,k;
cin>>fadd>>l>>k;
unordered_map<int, node> umap;
for(int i=0;i<l;i++){
string add,nadd;
int value;
cin>>add>>value>>nadd;
umap[stoi(add)]=node{value,nadd,add};
}
vector<node> nv[3];
node *np = &umap[stoi(fadd)];
while (stoi(np->next)!=-1)
{
if(np->val<0){
nv[0].push_back(*np);
}
else if(np->val<=k)
{
nv[1].push_back(*np);
}
else
{
nv[2].push_back(*np);
}
np=&umap[stoi(np->next)];
}
if(np->val<0){
nv[0].push_back(*np);
}
else if(np->val<=k)
{
nv[1].push_back(*np);
}
else
{
nv[2].push_back(*np);
}
int flag = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < nv[i].size(); j++) {
if (flag == 0) {
printf("%s %d ", nv[i][j].add.c_str(), nv[i][j].val);
flag = 1;
} else {
printf("%s\n%s %d ", nv[i][j].add.c_str(), nv[i][j].add.c_str(), nv[i][j].val);
}
}
}
printf("-1");
}
int main1(){
string fadd;
int l,k;
cin>>fadd>>l>>k;
unordered_map<int, node> umap;
for(int i=0;i<l;i++){
string add,nadd;
int value;
cin>>add>>value>>nadd;
umap[stoi(add)]=node{value,nadd,add};
}
vector<node> nv;
vector<node> kv;
vector<node> gkv;
node *np = &umap[stoi(fadd)];
while (stoi(np->next)!=-1)
{
if(np->val<0){
nv.push_back(*np);
}
else if(np->val<=k)
{
kv.push_back(*np);
}
else
{
gkv.push_back(*np);
}
np=&umap[stoi(np->next)];
}
if(np->val<0){
nv.push_back(*np);
}
else if(np->val<k)
{
kv.push_back(*np);
}
else
{
gkv.push_back(*np);
}
nv.insert(nv.end(),kv.begin(),kv.end());
nv.insert(nv.end(),gkv.begin(),gkv.end());
for(int i=0;i<nv.size()-1;i++){
printf("%s %d %s\n",nv[i].add.c_str(), nv[i].val,nv[i+1].add.c_str());
}
printf("%s %d -1",nv[nv.size()-1].add.c_str(), nv[nv.size()-1].val);
}