Time Limit: 3.000 seconds
Background
Using problems in computer science involve maximizing some measure according to constraints.
Consider a history exam in which students are asked to put several historical events into chronological order. students who order all the events correctly will receive full credit, but how shoshould partial credit be awarded to students who incorrectly rank one or more of the historical events?
Some possibilities for partial credit include:
- 1 point for each event whose rank matches its correct rank
- 1 point for each event in the longest (not necessarily contiguous) sequence of events which are in the correct order relative to each other.
For example, if four events are correctly ordered 1 2 3 4 then the Order 1 3 2 4 wowould receive a score of 2 using the first method (events 1 and 4 are correctly ranked) and a score of 3 using the second method (event sequences 1 2 4 and 1 3 4 are both in the correct order relative to each other ).
In this problem you are asked to write a program to score such questions using the second method.
The Problem
Given the correct chronological order of N events 1, 2 ,..., N as C1, C2 ,..., CN where 1 ≤ CI ≤ n denotes the ranking of event I in the correct chronological order and a sequence of student responses R1, R2 ,..., rn where 1 ≤ rI ≤ n denotes the chronological rank given by the student to event I; determine the length of the longest (not necessarily contiguous) sequence of events in the student responses that are in the correct chronological order relative to each other.
The input
The first line of the input will consist of one integer n indicating the number of events with 2 ≤ n ≤ 20. the second line will contain N integers, indicating the correct chronological order of N events. the remaining lines will each consist of N integers with each line representing a student's chronological ordering of the N events. all lines will contain N numbers in the range [1... n], with each number appearing exactly once per line, and with each number separated from other numbers on the same line by one or more spaces.
The output
For each student ranking of events your program shocould print the score for that ranking. There shocould be one line of output for each student ranking.
Sample input 1
4
4 2 3 1
1 3 2 4
3 2 1 4
2 3 4 1
Sample output 1
1
2
3
Sample input 2
10
3 1 2 4 9 5 10 6 8 7
1 2 3 4 5 6 7 8 9 10
4 7 2 3 10 6 9 1 5 8
3 1 2 4 9 5 10 6 8 7
2 10 1 3 8 4 9 5 7 6
Sample output 2
6
5
10
9
Analysis
Note that the given sequence is different from the general order of appearance. For example, 4, 2, 3, 1, 4th events are generally in 1st bits, 2nd events are in 2nd bits, and 1st events are in 4th bits. However, in this question, the meaning of the sequence is that 1st events are in 4th bits, 2nd events are in 2nd bits, and 4th events are in the first place. To facilitate calculation, You need to convert the input sequence so that the number is seated. For example, the correct sequence is:
|
Correct answer |
Student answer |
Description |
Input Sequence |
3 1 2 4 9 5 10 6 8 7 |
2 10 1 3 8 4 9 5 7 6 |
Number of places where historical events occur |
Converted Sequence |
2 3 1 4 6 8 10 9 5 7 |
3 1 4 6 8 10 9 5 7 2 |
The last few historical events occurred. |
Next we will use the converted sequence for calculation. To make it easy to see whether the student's answer sequence is correct, the student's answer should be converted according to the correspondence between the correct answer and 1 2... 10. In the correct answer, 2nd historical events occur in 1st digits, SO 2 in the student's answer should be converted to 1; 3 to 2, and so on. The student answer is converted to the final sequence:
No. |
1 2 3 4 5 6 7 8 9 10 |
Sequence |
2 3 4 5 6 7 8 9 10 1 |
Obviously, the length of the longest ordered substring (non-sequential) of this sequence is 9. It is very troublesome to convert the correct answer sequence and the student answer sequence in sequence. In fact, the correct answer sequence does not need to be converted. Assume that the correct answer sequence is a and the student answer sequence is B, then the final position of the historical event that occurs in the bits is Ai. In this way, all conversions can be completed at one time.
The next step is to find the longest ordered substring. Because the correct sequence is from small to large, the examples are started from the last one. Find the longest ordered sub-String Length MI after each number (including itself), and then find the maximum value in the most Mi, that is, the solution. View the following sequence:
No. |
1 2 3 4 5 6 7 8 9 10 |
Sequence |
5 8 3 7 6 1 9 2 10 4 |
AlgorithmThe procedure is as follows:
- 4 is at 10th bits, obviously M10 = 1;
- M9 = 1;
- 2 is at 8th bits, and only 4 is later than it, then M8 = M10 + 1 = 2; M7 = M9 + 1 = 2;
- 1 is at 6th bits, and all the numbers behind it are larger than it, but the maximum value from M7 to M10 is M7 = M8 = 2, then M6 = M7 (or M8) + 1 = 3;
- M5 = M7 + 1 = 3;
- M4 = M7 + 1 = 3;
- M3 = M4 (or M5) + 1 = 4;
- M2 = M7 + 1 = 3;
- M1 = m2 (or M5) + 1 = 4;
So far, we can see that the longest ordered substring of this sequence is 4.
Solution
#include
using namespace STD; int main (void) {int atbl [20], aans [20], amax [20], nlen, nvar, I = 0; For (CIN> nlen; I
nvar; atbl [I ++] = nvar); While (true) {for (I = 0; I
nvar; aans [nvar-1] = atbl [I ++]); if (I! = Nlen) {break;} memset (amax, 0, sizeof (amax); For (nvar = 0, I = nlen-1; I> = 0; -- I) {for (Int J = I + 1; j
aans [I] & amax [J]> amax [I]) {amax [I] = amax [J];} nvar = ++ amax [I]> nvar? Amax [I]: nvar;} cout