-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz2.pas
220 lines (190 loc) · 3.61 KB
/
z2.pas
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
program z2(input, output);
type
interval = record
low: real;
high: real;
end;
pok = ^elem;
elem = record
int: interval;
next: pok;
end;
var
lista: pok;
n, i: integer;
procedure ispisiListu(lista: pok);
begin
writeln('--------------');
while lista<>nil do
begin
writeln('[', lista^.int.low:1:2, ', ', lista^.int.high:1:2, ']');
lista:=lista^.next;
end;
writeln('--------------');
end;
function min(a, b: real): real;
begin
if (a<b) then
min:=a
else
min:=b
end;
function max(a, b: real): real;
begin
if (a>b) then
max:=a
else
max:=b
end;
function poklapanjeIntervala(i1, i2: interval): boolean;
begin
poklapanjeIntervala:=((i1.high<=i2.high) and (i1.high>=i2.low)) or
((i2.high<=i1.high) and (i2.high>=i1.low));
end;
function spojiIntervale(i1, i2: interval): interval;
var
i: interval;
begin
i.low:=min(i1.low, i2.low);
i.high:=max(i1.high, i2.high);
spojiIntervale:=i;
end;
procedure dodajInterval(var lista: pok; i: interval);
var
novi, tek, posl, preth: pok;
p: boolean;
noviInt: interval;
begin
p:=false; //ako se ne preklopi ni sa jednim bice false, i onda se interval dodaje na kraj liste
tek:=lista;
posl:=nil;
noviInt:=i;
while tek<> nil do
begin
posl:=tek;
if poklapanjeIntervala(tek^.int, i) then
begin
noviInt:=spojiIntervale(tek^.int, noviInt);
p:=true;
end;
tek:=tek^.next;
end;
if not p then
begin
new(novi);
novi^.int:=i;
novi^.next:=nil;
if lista=nil then
lista:=novi
else
posl^.next:=novi;
end
else
begin
tek:=lista;
preth:=nil;
while tek<>nil do
begin
if poklapanjeIntervala(tek^.int, i) then
begin
if preth=nil then
begin
preth:=tek;
tek:=tek^.next;
dispose(preth);
lista:=tek;
preth:=nil;
end
else
begin
preth^.next:=tek^.next;
dispose(tek);
tek:=preth^.next;
end;
end
else
begin
preth:=tek;
tek:=tek^.next;
end;
end;
new(novi);
novi^.int:=noviInt;
novi^.next:=nil;
if lista=nil then
lista:=novi
else
preth^.next:=novi;
end;
end;
procedure ucitajIntervale(var lista: pok; n: integer);
var
i: integer;
noviInterval: interval;
begin
for i:=1 to n do
begin
read(noviInterval.low, noviInterval.high);
dodajInterval(lista, noviInterval);
end;
end;
procedure brisiListu(var lista: pok);
var
stari: pok;
begin
stari:=lista;
while lista<>nil do
begin
lista:=lista^.next;
dispose(stari);
stari:=lista;
end;
end;
procedure sort(lista: pok);
var
tek, sled: pok;
tmp: interval;
begin
tek:=lista;
if tek<>nil then
sled:=tek^.next
else
sled:=nil;
while sled<> nil do
begin
while sled<>nil do
begin
if (sled^.int.low<tek^.int.low) then
begin
tmp:=sled^.int;
sled^.int:=tek^.int;
tek^.int:=tmp;
end;
sled:=sled^.next;
end;
tek:=tek^.next;
sled:=tek^.next;
end;
end;
function total(lista: pok): real;
var
s: real;
begin
s:=0;
while lista<>nil do
begin
s:=s+lista^.int.high-lista^.int.low;
lista:=lista^.next;
end;
total:=s;
end;
begin
readln(n);
ucitajIntervale(lista, n);
ispisiListu(lista);
sort(lista);
ispisiListu(lista);
writeln('Total: ', total(lista):1:2);
brisiListu(lista);
readln(n);
end.