-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanidade.c
128 lines (108 loc) · 2.5 KB
/
sanidade.c
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
/*
iagorrr ;)
*/
#include <stdio.h>
#include <stdlib.h>
#define less(a, b) a.value < b.value
#define lesseq(a, b) a.value <= b.value
#define exchange(a , B) {Item t = a; a = b; b = t;}
typedef struct Item
{
int value;
int prev;
int next;
} Item;
void merge(Item *v, int l, int r);
void merge_sort(Item *v, int l , int r);
int bb(Item *v, int l, int r, int x)
{
while(l <= r)
{
int mid = l + (r-l)/2;
if(v[mid].value == x) return mid;
else if(v[mid].value > x) r = mid -1;
else l = mid+1;
}
return -1; // 0-0
}
Item input[(int)1e5+666];
Item v[(int)1e5+666];
int main()
{
int n, prev, next;
int p = 0;
while(scanf("%x %x %x", &input[p].value, &input[p].prev, &input[p].next) != EOF)
{
v[p] = input[p];
++p;
}
Item ptr1 = v[0]; Item ptr2 = v[1];
merge_sort(&v[0], 0, p-1);
int current = ptr1.value;
int pt1 = 0, pt2 = 1;
while(1)
{
if(current == ptr2.value)
{
pt1 = 1;
break;
}
int cn = v[bb(&v[0], 0, p-1, current)].next; if (cn == 0) break;
int npv = v[bb(&v[0], 0, p-1, cn)].prev;
// printf("current: %x, next: %x, next.prev: %x\n", current, cn, npv);
if(current != npv)
{
break;
}
current = cn;
}
/*
current = ptr2.value;
while(1)
{
if(current == ptr1.value)
{
pt2 = 1;
break;
}
int cp = v[bb(&v[0], 0, p-1, current)].prev; if (cp == 0) break;
int ppn = v[bb(&v[0], 0, p-1, cp)].next;
// printf("current: %x, previous: %x, previous next: %x\n", current, cp, ppn);
if(current != ppn)
{
break;
}
current = cp;
}
*/
printf("%s\n", pt2 && pt1 ? "sana" : "insana");
}
void merge_sort(Item *v, int l , int r){// [l, r]
if(l >= r) return;
int mid = l + (r-l)/2;
merge_sort(v, l, mid);
merge_sort(v, mid+1 ,r);
merge(v, l, r);
}
void merge(Item *v, int l, int r){// [l,r]
int mid = l + (r-l)/2;
int pl = l;
int pr = mid+1;
int p = 0;
Item *aux = malloc(sizeof(Item)*(r-l+1));
while(pl <= mid && pr <= r){
if(less(v[pl], v[pr]))
aux[p++] = v[pl++];
else
aux[p++] = v[pr++];
}
while(pl <= mid)
aux[p++] = v[pl++];
while(pr <= r)
aux[p++] = v[pr++];
p = 0;
for(int i = l; i <= r; ++i)
v[i] = aux[p++];
free(aux);
}
// AC.