NYOJ 364 田忌赛马(模拟分析)

田忌赛马

时间限制:3000 ms  |           内存限制:65535 KB
难度:3
 
描述
Here is a famous story in Chinese history.
"That was about 2300 years  ago. General Tian Ji was a high official in the country Qi. He likes to play  horse racing with the king and others."
"Both of Tian and the king have  three horses in different classes, namely, regular, plus, and super. The rule is  to have three rounds in a match; each of the horses must be used in one round.  The winner of a single round takes two hundred silver dollars from the  loser."
"Being the most powerful man in the country, the king has so nice  horses that in each class his horse is better than Tian's. As a result, each  time the king takes six hundred silver dollars from Tian."
"Tian Ji was  not happy about that, until he met Sun Bin, one of the most famous generals in  Chinese history. Using a little trick due to Sun, Tian Ji brought home two  hundred silver dollars and such a grace in the next match."
"It was a  rather simple trick. Using his regular class horse race against the super class  from the king, they will certainly lose that round. But then his plus beat the  king's regular, and his super beat the king's plus. What a simple trick. And how  do you think of Tian Ji, the high ranked official in China?"
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more,  were he sitting in the ACM contest right now, he may discover that the horse  racing problem can be simply viewed as finding the maximum matching in a  bipartite graph. Draw Tian's horses on one side, and the king's horses on the  other. Whenever one of Tian's horses can beat one from the king, we draw an edge  between them, meaning we wish to establish this pair. Then, the problem of  winning as many rounds as possible is just to find the maximum matching in this  graph. If there are ties, the problem becomes more complicated, he needs to  assign weights 0, 1, or -1 to all the possible edges, and find a maximum  weighted perfect matching...
However, the horse racing problem is a very  special case of bipartite matching. The graph is decided by the speed of the  horses --- a vertex of higher speed always beat a vertex of lower speed. In this  case, the weighted bipartite matching algorithm is a too advanced tool to deal  with the problem.
In this problem, you are asked to write a program to  solve this special case of matching problem.
 
输入
The input consists of many test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses.
输出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
0
0
思路1:对各人马的speed排序(升序),1:先慢马,若田大于king,tw++继续下一匹,否则:
2:比较快马,若 田>king tw++继续下一匹,否则:
3:用田的慢马,去和king的快马比较 tw--;
4:假如以上条件均不满足,既得田和king马速全部一致,tw不计数,进行下一匹
代码1:
(提示:题中没有显示结束标志是什么,如果自己不做判断,将会超时,一般有两种方法:eof,~)(自己不太了解就看书咯。。。。)
 1  
 2 #include<stdio.h>
 3 #include<algorithm>
 4 using namespace std;
 5  int main()
 6  {
 7 
 8      int n,i,t[1050],g[1050];
 9      while(~scanf("%d",&n))
10      {
11          for(i=0;i<n;i++)
12             scanf("%d",&t[i]);
13          for(i=0;i<n;i++)
14             scanf("%d",&g[i]);
15          sort(t,t+n);
16          sort(g,g+n);
17         int t1=0,g1=0;
18         int t2=n-1,g2=n-1,tw=0;
19          while(g1<=g2)
20          {
21              if(t[t1]>g[g1]) {
22                     t1++;
23                     g1++;
24                     tw+=200;
25                     }//先比较最慢
26              else if(t[t2]>g[g2]){
27                      t2--;
28                      g2--;
29                      tw+=200;
30                     }//比较最快
31              else {
32                     if(t[t1]<g[g2]) tw-=200;
33                     g2--;
34                     t1++;
35                   }
36          }
37      printf("%d\n",tw);
38      }
39      return 0;
40  }
41 
42 
43         
View Code

思路2:与上面恰好相反,降序排列,先快后慢

代码2:

 1  
 2  
 3 
 4 
 5 
 6 
 7 #include<stdio.h>
 8 #include<algorithm>
 9 using namespace std;
10 bool cmp(int a,int b)
11 {
12     return a>b;
13 }
14 int main()
15 {
16     int n,m,i,a[1050],b[1050];
17     while(scanf("%d",&n) !=EOF)
18     {
19         for( i = 0 ; i < n ;++ i)
20             scanf("%d",&a[i]);
21         for( i = 0 ; i < n ;++ i)
22             scanf("%d",&b[i]);
23         sort(a,a+n,cmp);
24         sort(b,b+n,cmp);
25 
26         int a1=0,a2=n-1,tw=0;
27         int b1=0,b2=n-1;
28         while(a1 <= a2)
29         {
30             if(a[a1] > b[b1]) //首先比较最快的马
31             {
32                 tw+=200; //若田忌大于国王
33                 a1++;    //下一匹马
34                 b1++;
35             }
36             else if( a[a2] > b[b2])
37             {
38                  tw+=200;
39                  --a2;
40                  --b2;
41             }
42             else
43             {
44             if(a[a2] < b[b1])
45                 tw-=200;
46                 ++b1;
47                 --a2;
48             }
49         }
50         printf("%d\n",tw);
51     }
52 }
53                 
View Code

 

posted on 2013-08-31 21:43  落水寒冰  阅读(254)  评论(0编辑  收藏  举报

导航