练习题 Team T-shirt Selection / Just like Russia’s National Flag

完整题目描述:

Monroe, Brad and William have been participating in Samsung TeamProgramming as a team.
As always, they are getting ready for this year’s programmingcontest.
But this time, they plan to wear the same t-shirt to boost a can-doattitude.
As a matter of fact, most programming contest organizers suggestparticipants to wear the same t-shirts.
Monroe, Brad and William joined the programming contest for Ntimes.
This indicates that they have the same N types of t-shirts.
They want to choose one out of N types of t-shirts to wear duringthe practice.
Each of the three young men has the order of their favoritet-shirts and badly wants their favorite t-shirt to be selected.
To settle this, they decided to select based on the followingrules.
- A t-shirt to wear during the practice is selected from N typesof t-shirts.
- Monroe, Brad and William will take turns to exclude one t-shirtat a time. The last t-shirt will be the one that they will wear during the practice.
- Excluding t-shirts will start from Monroe to Brad and William.
- Each of them knows the order of favorite t-shirts of others.
- Each of them will do their best to make their favorite t-shirtsto be selected when excluding other t-shirts.
- Each of them knows the order of favorite t-shirts of others.
So, they will consider possible choices of others to make the bestdecision.
The three young men will obey the above rules and do their best tomake their favorite t-shirt to be selected.
Which t-shirt will be finally selected?
As mentioned above, each of them knows the order of favoritet-shirts of others.
Keeping this in mind, they will do their best to make the bestdecision when excluding t-shirts.
For example, there are 4 types of t-shirts. Each of them isnumbered from 1 to 4.
The order of favorite t-shirts of Monroe is 1 3 4 2, Brad is 2 1 43 and William is 4 3 2 1.
 In this case, a total of 3t-shirts will be excluded.
If Monroe excludes 2, then Brad will exclude either 1 or 3.
(If 4 is excluded, 1 and 3 will be left. Then, William willexclude one that he hates more which is 1. This is not the best choice as 3will be left in the end.)
William excludes 3 when 4 and 3 are left but excludes 1 when 4 and1 are left.
To sum up, Monroe’s choice of excluding 2 will make 4, his 3rdfavorite t-shirt, to be selected in the end.
If Monroe excludes 4, 2 will be selected and this does not changeeven excluding 3.
 If 1 is excluded, 4 will beselected. Thus, excluding 3 and 4 is not the best choice as his least favoritet-shirt 2 will be selected.
Monroe’s best choice is excluding 2 or 1 to make his 3rdfavorite t-shirt 4 to be selected.
 
[Input]
The first line contains T, the number of test cases.
The first line of each test case has an integer N(1≤N≤13).
Each of the following 3 lines has space-separated N integers.
Each t-shirt is represented in integers between 1 and N.
No same integer is appeared on a single line.
The first line describes the order of favorite t-shirts of Monroe,the second line describes Brad’s and the third line describes William’s.
 
[Output]
For each test case, print space-separated #T and the number on thefinal t-shirt.

Input Example
3 //Total test cases
3 //N of the 1st TC
1 2 3 //Order of favorite t-shirts of Monroe
1 2 3 //Order of favorite t-shirts of Brad
1 2 3 //Order of favorite t-shirts of William
4 //N of the 1st TC
1 3 2 4 // Order of favorite t-shirts of Monroe
4 1 2 3 // Order of favorite t-shirts of Brad
1 2 3 4 // Order of favorite t-shirts of William
5
1 3 2 4 5
5 3 2 1 4
1 3 2 4 5

Output Example
#1 1
#2 1
#3 3

题解:DFS+状态压缩

 1 /* MH61  Team T-shirt Selection
 2 题意:三个选手有13件一样的衣服,给出每个选手对衣服的喜爱程度排序,
 3 三个选手依次从可选的衣服中去掉一件,求最后的一件衣服
 4 选手去除衣服的原则是,使剩下的衣服是自己更为喜爱的
 5 */
 6 #include <stdio.h>
 7 const int TSHIRT = 13;
 8 const int PEOPLE = 3;
 9 const int STATUS = 1 << TSHIRT;
10 int T, N, full;
11 int fav[TSHIRT][PEOPLE];
12 int dp[STATUS][PEOPLE];
13 int pos[TSHIRT][PEOPLE];
14  
15 int dfs(int person, int status){ //实际传参是一个数组,使用状态压缩
16     if (dp[status][person] >= 0)return dp[status][person];
17     int nperson = (person == PEOPLE - 1) ? nperson = 0 : person + 1;
18     int min = N, best, nstatus, last_num;
19     for (int i = 0; i < N; ++i){
20         if ((status & (1 << i)) == 0){
21             nstatus = status | (1 << i);
22             if (nstatus == full){
23                 dp[status][person] = fav[i][person];
24                 return fav[i][person];
25             }
26             last_num = dfs(nperson, nstatus);
27             if (pos[last_num][person] < min){
28                 min = pos[last_num][person];
29                 best = last_num;
30             }
31         }
32     }
33     dp[status][person] = best;
34     return best;
35 }
36 int main(void){
37     scanf("%d", &T);
38     for (int tc = 1; tc <= T; ++tc){
39         scanf("%d", &N);
40         full = (1 << N) - 1;
41         for (int i = 0; i < PEOPLE; ++i){
42             for (int j = 0; j < N; ++j){
43                 scanf("%d", &fav[j][i]);
44                 pos[fav[j][i]][i] = j;//
45             }
46             for (int j = 0; j < STATUS; ++j)
47                 dp[j][i] = -1;
48         }
49         printf("#%d %d\n", tc, dfs(0, 0));
50     }
51     return 0;
52 }

 

Just like Russia’s National Flag 完整英文题目描述:

As the first step, you took out an old flag from storage. The flag is divided into N rows and M columns. Each cell is colored either in white, blue or red. You want to re-color a certain number cells to transform the old flag into Russia’s national flag. To do so, you have to meet the following conditions. Some of the top lines (at least more than one) should all be colored in white. Some of the next lines (at least more than one) should all be colored in blue. Rest of the lines (at least more than one) should all be colored in red. Print a min value of cells that should be newly colored to make the flag looks like Russia’s.

[Input] The first line contains a number of test cases known as T. The first line of each test case has two space-separated integers of N,M(3≤N,M≤50). A N number of lines have a string comprised of a M number of characters. A (j)th character in the (i)th line represents a cell color of (i)th column and the (i)th row in the flag. “W” stands for white while “B” as in blue and “R” as in red. Characters other than W, B and R can’t be input.

[Output] For each test case, find a min value of cells that should be newly colored to make something like Russia’s national flag and print it with ‘#x’(test case number) ahead across the T line.

Input Example
2
4 5
WRWRW
BWRWB
WRWRW
RWBWR
6 14
WWWWWWWWWWWWWW
WWRRWWBBBBBBWW
WRRRWWWBWWWWRB
WWBWBWWWBWRRRR
WBWBBWWWBBWRRW
WWWWWWWWWWWWWW

Output Example

#1 11

#2 44

 

题解:动态规划入门

 1 /* [MH50] Just like Russia’s National Flag
 2  * only need to count colors in rows, not each point
 3  * (the num of color need to change) be initialized as m, to avoid when the point is white, add red and blue
 4  * use dynamic programming when decide each row in which color
 5 */
 6 #include<stdio.h>
 7 #define M 100000009
 8 int n, m;
 9 char a[1009];
10 int b[1009][3];
11 int d[1009][3];
12 int minn(int p, int q) { return p < q ? p : q; }
13  
14 int main() {
15     int t, tv = 0;
16     int i, j, k, l;
17     scanf("%d", &t);
18     while (t--) {
19         scanf("%d %d", &n, &m);
20         for (i = 0; i < n; i++) {
21             scanf("%s", a);
22             b[i][0] = b[i][1] = b[i][2] = m;
23             for (j = 0; j < m; j++) {
24                 if (a[j] == 'W')b[i][0]--;
25                 if (a[j] == 'B')b[i][1]--;
26                 if (a[j] == 'R')b[i][2]--;
27             }
28             d[i][0] = d[i][1] = d[i][2] = M;
29         }
30         d[0][0] = b[0][0];
31         for (i = 1; i < n; i++) {
32             d[i][0] = d[i - 1][0] + b[i][0];
33             d[i][1] = minn(d[i - 1][0], d[i - 1][1]) + b[i][1];
34             d[i][2] = minn(d[i - 1][1], d[i - 1][2]) + b[i][2];
35         }
36         printf("#%d %d\n", ++tv, d[n - 1][2]);
37     }
38 }

 

 

 

posted @ 2017-12-28 14:04  proscientist  阅读(196)  评论(0编辑  收藏  举报