Er ... and ?
I don't understand it. Every method I use gives me the wrong output! What to do with the RANK sequence? :/
Of course I understand the way you get
2 3 1 4 6 8 10 9 5 7 from
3 1 2 4 9 5 10 6 8 7 , but what to do with this
2 3 1 4 6 8 10 9 5 7 ??
I hope you understand what don't I understand
Please help
[pascal]
type
elem = record
index:integer;
count:integer;
end;
var
right_seq: array [1..20] of integer;
seq: array [1..20] of elem;
test, num, nn,i: integer;
procedure BuildCounts;
var
i,j:integer;
begin
for i:=1 to nn do begin
seq.count := 1;
for j:=1 to i-1 do
if (seq.index > seq[j].index) and
(seq.count < seq[j].count+1) then
seq.count := seq[j].count+1;
end;
end;
function GetMax:integer;
var
i,j:integer;
begin
j:=seq[1].count;
for i:=2 to nn do
if (j < seq.count) then j := seq.count;
GetMax := j;
end;
begin
read(input, nn);
for i:=1 to nn do
read(input, right_seq);
test := 0;
while not eof(input) do begin
if eoln(input) then begin
readln(input);
continue;
end;
for i:=1 to nn do begin
read(input, num);
seq[num].index := right_seq;
end;
if test=0 then test:=1 else writeln;
BuildCounts;
num:=GetMax;
write(output, num:1);
end;
end.
[/pascal]
But this program got WA and I rewrote this problem with the same algorithm on C++:
[cpp]#include <stdio.h>
#define MAX 21
typedef struct ELEM {
int index;
int count;
};
int nn;
int right_seq[MAX];
ELEM seq[MAX];
FILE *fIn, *fOut;
This is the second sample input.
But the numbers represent the ranking of the events, not the actual order of the events themselves. So the actual correct ordering of the events in this input case is:
2 3 1 4 6 8 10 9 5 7
This is the actual event numbers and the leftmost event is first and the rightmost event is last.
Now, for one of the student orderings. 2 10 1 3 8 4 9 5 7 6 is not the order of the events, just the ranking. So to convert it into an ordering:
3 1 4 6 8 10 9 5 7 2
Once again, the leftmost event is first and the rightmost event is last.
Then it becomes clear why the answer is 9. See, you can't just take the length of the LCS of the correct and student sequences as they appear in the input and expect to get the right answer. Surprised me too.
(I've written this all as carefully as possible but I may have made a mistake)
Computer Science is no more about computers than Astronomy is about telescopes.
-- E. W. Dijkstra
Each number r in the sequence means that event i has rank r. So let's look at the correct sequence as given in the input.
3 1 2 4 9 5 10 6 8 7
This means:
event 1 has rank 3
event 2 has rank 1
event 3 has rank 2
event 4 has rank 4
event 5 has rank 9
event 6 has rank 5
event 7 has rank 10
event 8 has rank 6
event 9 has rank 9
event 10 has rank 7
So if we order the sequence of events in terms of ranks, this is what we get:
2 3 1 4 6 8 10 9 5 7 (Please verify this yourself)
The leftmost event is first (lowest rank) and the rightmost event is last (highest rank). In other words, 2 is the earliest event and 7 is the latest event.
So now let's see a student sequence from the same input.
2 10 1 3 8 4 9 5 7 6
Doing the same translation:
event 1 has rank 2
event 2 has rank 10
event 3 has rank 1
event 4 has rank 3
event 5 has rank 8
event 6 has rank 4
event 7 has rank 9
event 8 has rank 5
event 9 has rank 7
event 10 has rank 6
So if we order this sequence of events in terms of rank, this is what we get:
3 1 4 6 8 10 9 5 7 2 (Once again please verify this)
Once again, the numbers in this new sequence represent the ordering of the events so 3 is the earliest event and 2 is the latest event.
The sequence "3 1 4 6 8 10 9 5 7" is the longest contiguous sequence of events that are ordered correctly relative to each other (longest common subsequence). Now do you see why the answer is 9? Again, you can't just take the LCS of the sequences as they appear in the input.
Computer Science is no more about computers than Astronomy is about telescopes.
-- E. W. Dijkstra
If you're using the length of LCS algorithm on the sequences exactly as they appear in the input, you are out of luck. There is a subtlety to this problem that you haven't accounted for. I compiled your code on my own system and run the Sample Input 2 on it and gotten the wrong answer. Sample Input 2 is precisely the input data that clued me in to how the problem should really be solved (I was doing the same as you before). There's another thread about #111 - History Grading on here where I detail exactly how length of LCS can be applied to this problem.
Computer Science is no more about computers than Astronomy is about telescopes.
-- E. W. Dijkstra