← Back to topics
Topic

Bojenje

m
matijazzz
my code does 5/10 .. I have two ideas and both of them do 5/10 ..
well, the first idea is to find a color, which has the largest distance between the index of it' first and last appear, then I continue with that idea finding next number and so on, but in new subsequences ...
The other idea is to count appearence of a color and than I get subsequences between that first color and continue with this idea with those new subsequences ..
If someone has a same idea he/she could look at my code but does anybody has any other ideas?
m
matijazzz
oh, and here's one of my codes ...

program Bojenje;

var
a,p,q,c,s:array [1..300] of integer;
poz:array [0..300,1..300] of integer;
queue:array [1..2,0..300] of integer;
u,i,n,k:integer;

procedure Paint;
var
i,j,z,r,x,y,l,h,dx,dy:integer;
begin
l:=0; h:=1;
queue[1,0]:=1;
queue[2,0]:=n;
while (l < h) do begin
dx:=queue[1,l]; dy:=queue[2,l]; l:=l+1;
for i:=1 to k do poz[0,i]:=0;
r:=0;
for i:=dx to dy do begin
if (poz[0,a[i]] = 0) then begin
r:=r+1;
s[r]:=a[i];
end;
inc(poz[0,a[i]]);
poz[a[i],poz[0,a[i]]]:=i;
end;
if (r > 1) then begin
z:=0;
for i:=1 to r do begin
if (poz[0,s[i]] > z) then begin
z:=poz[0,s[i]];
j:=i;
end;
end;
u:=u+1;
p[u]:=poz[s[j],1];
q[u]:=poz[s[j],poz[0,s[j]]];
c[u]:=s[j];
for i:=1 to poz[0,s[j]]-1 do begin
x:=poz[s[j],i]+1;
y:=poz[s[j],i+1]-1;
if (x < y) then begin
queue[1,h]:=x;
queue[2,h]:=y;
h:=h+1;
end;
if (x = y) then begin
u:=u+1;
p[u]:=x;
q[u]:=y;
c[u]:=a[x];
end;
end;
end else begin
u:=u+1;
p[u]:=dx;
q[u]:=dy;
c[u]:=a[dx];
end;
end;
end;

begin

readln(n,k);
for i:=1 to n do readln(a[i]);
Paint;
writeln(u);
for i:=1 to u do writeln(p[i],' ',q[i],' ',c[i]);

end.
h
harta01
well I have checked your code.
Test it with
9 3
1
2
3
1
2
3
1
2
3


The answer is 7
m
matijazzz
Thank you Harta01 ..
really helped, I' m now using the idea that was explained in post and not shown in code ..
Now my code fails test cases 4 and 7 ...
h
harta01
I noticed that your program got Runtime Error when the input is:
25 47
10
7
24
47
24
7
24
7
10
7
47
7
10
24
10
7
47
7
24
10
47
10
24
47
24
M
MilosRadic
the same idea as the first one of matijazz fails on 2nd 4th and 7th test...can someone send me test cases or explain a bit why:D
solution:
http://www.z-trening.com/submit.php?submit=7100142588&subm_code=1
d
dejandenib
Sorry for late response. Here is test case 2

7 32
6
32
6
29
32
29
6
Answer
4
1 7 6
2 2 32
4 6 29
5 5 32
M
MilosRadic
ok thanks but i found the test cases:D
but still cant pass 4,7 test case and dont know what to do to improve the code...
if someone has solved this one plz help:D
and thanks again:D