Scores of marbles

来源:互联网 发布:字母小写转大写js 编辑:程序博客网 时间:2024/06/08 17:04

There are two boxes, A and B with shelves, a and b. Following the , there are marbles with numbers written from 1 to N to every shelf of the two boxes.


Box A

1

2

1

4

Box B

1

3

2

2


You must always take out each marble from the left of the box in turn and each time you have one marble, you will get a score as below:
1. Take out a marble from Box A only. Your score is 0.
2. Take out a marble from Box B only. Your score is 0.
3. Take out each marble from Box A and B at the same time. At this moment, if the marble number is i from Box A and the marble number is j from Box B, you have a score as much by the row I & column j in the scoreboard below. The score can be negative numbers.


2

2

-3

3

1

3

2

4

-1

1

4

5

6

2

-4

-1


When the scoreboard is given as in the , in the case of the if you take the first marble each out of Box A and B at the same time, the number is (1, 1) so you score 2 by the row 1 & column 1. If the marbles numbers are (1, 3), you score -3 by the row 1 & column 3.
When a scoreboard is given by inputs and marble numbers are given in two boxes, A and B, make the accumulated scores of given scores maximal.

Time limit : 2 seconds (java : 4 seconds)

Input format


Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 20) are given in a row.
The maximum number of marbles, N, the number of marbles in the box A that is a, and the number of marbles in the box B that is b are given in order with a space separately on the first row of each test case. (1 ≤ N, a, b ≤ 1,000) Score tables are given from the second row to N number rows. Each score is given by being separated with a blank. The range of each number is over -1,000 below 1,000.
On the next row, marble numbers in the box A are given in order from the left.
On the final row, marble numbers in the box B are given in order from the left.

Output format

Output the maximum value of accumulated scores given on the first row of each test case.

Example of Input

2
4 4 4
2 2 -3 3
1 3 2 4
-1 1 4 5
6 2 -4 -1
1 2 1 4
1 3 2 2
5 5 5
5 9 9 -1 -9
-2 -10 -2 4 -3
5 0 2 0 -8
-6 -7 5 6 6
-4 8 8 -9 -10
1 1 2 1 3
3 3 1 3 5

Example of Output

8
27 


C

/*Notice that "You must always take out each marble from the left of the box in turn".So scores[i][j] means the scores when a:1-->i, b:1-->j have been brought out,-->scores[i][j] = max{data[i][j]+scores[i-1][j-1], scores[i][j-1], scores[i-1][j]};Now we can use DP method to solve it.*/#include <stdio.h>#include <string.h>#define SIZE 1001int input[SIZE][SIZE];int data[SIZE][SIZE];int scores[SIZE][SIZE];int N, a, b;int A[SIZE], B[SIZE];int main(void){int tc, T, i, j, k;setbuf(stdout, NULL);scanf("%d", &T);for(tc = 0; tc < T; tc++){scanf("%d %d %d", &N, &a, &b);for(i=1;i<=N;i++)for(j=1;j<=N;j++)scanf("%d", &input[i][j]);for(i=1;i<=a;i++)scanf("%d", &A[i]);for(i=1;i<=b;i++)scanf("%d", &B[i]);for(i=1;i<=a;i++)for(j=1;j<=b;j++)data[i][j] = input[A[i]][B[j]];memset(scores, 0, sizeof(scores));for(i=1;i<=a;i++)for(j=1;j<=b;j++){scores[i][j] = scores[i-1][j-1]+data[i][j];if(scores[i][j-1]>scores[i][j])scores[i][j] = scores[i][j-1];if(scores[i-1][j]>scores[i][j])scores[i][j] = scores[i-1][j];}printf("%d\n", scores[a][b]);}return 0;//Your program should return 0 on normal termination.}

C++

#include <algorithm>#include <cstdio>#include <iostream>#include <vector>using ::std::max;using ::std::printf;using ::std::scanf;using ::std::vector;const int inf = 1e9 + 7;int main() {  int t;  scanf("%d\n", &t);  while (t--) {    int n, a, b;    scanf("%d %d %d\n", &n, &a, &b);    vector< vector<int> > result(a + 1, vector<int>(b + 1));    vector< vector<int> > score(n + 1, vector<int>(n + 1));    vector<int> A(a + 1);    vector<int> B(b + 1);    for (int i = 1; i <= n; ++i) {      for (int j = 1; j <= n; ++j) {         scanf("%d ", &score[i][j]);       }       }        for (int i = 1; i <= a; ++i) {      scanf("%d ", &A[i]);     }    for (int i = 1; i <= b; ++i) {      scanf("%d ", &B[i]);    }    for (int i = 1; i <= a; ++i) {      for (int j = 1; j <= b; ++j) {        result[i][j] = score[A[i]][B[j]] + result[i - 1][j - 1];        result[i][j] = max(result[i][j], result[i - 1][j]);        result[i][j] = max(result[i][j], result[i][j - 1]);      }    }    printf("%d\n", result[a][b]);  }  return 0;}


0 0