zoukankan      html  css  js  c++  java
  • HDU 1052

    Problem Description
    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.
     
    Input
    The input consists of up to 50 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. The input ends with a line that has a single 0 after the last test case.
     
    Output
    For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
     

    Sample Input

    3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0
     

    Sample Output

    200 0 0
    题意分析:
      田忌赛马,赢了拿200,输了赔200,问你能赚多少钱。
    解题思路:
     贪心,以下有两个代码:
      第一个代码:
      排序完从快到慢第一次比较,然后将田忌的慢马一匹一匹拉上来和齐王快马比(请见循环),找出赢得最多的就是了。(那个乱入的快排请随意)
      田忌速度:
         开始:5 4 3 2 1
       开始循环:1 5 4 3 2
            1 2 5 4 3
            1 2 3 5 4
            1 2 3 4 5
      如此,选赢的最多的即可。 
      第二个代码:
       排序完,本着把要输的慢马和齐王的快马比的原则。
      如果慢马比齐王快,那赢一场;
      如果快马比齐王快,那赢一场;
      如果慢马和齐王快马一样快,那平局。
      剩下的都是输。
     
     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     void qsort(int c[],int,int);
     6     int n,ans;
     7     int a[1500];
     8     int b[1500];
     9     while(scanf("%d",&n),n)
    10     {
    11         for(int i=1;i<=n;i++)    scanf("%d",&a[i]);
    12         for(int i=1;i<=n;i++)    scanf("%d",&b[i]);
    13         qsort(a,1,n);
    14         qsort(b,1,n);
    15         int ans=0;
    16         for(int i=1;i<=n;i++){
    17             if(a[i]>b[i]) ans+=200;
    18             else if(a[i]<b[i]) ans-=200;
    19         }
    20         for(int i=1;i<=n-1;i++){
    21             int t,max=0;
    22             t=a[n];//把慢马放前面,然后依次往下放 
    23             for(int j=n;j>i;j--)    a[j]=a[j-1];
    24             a[i]=t;
    25             for(int j=1;j<=n;j++){
    26                 if(a[j]>b[j]) max+=200;
    27                 else if(a[j]<b[j]) max-=200;
    28             }
    29             if(max>ans){
    30                 ans=max;
    31             }
    32         }
    33         printf("%d
    ",ans);
    34     }
    35  } 
    36  void qsort(int c[],int left,int right)//快排 
    37  {
    38      if(left<right){
    39      int i=left;
    40      int j=right;
    41      int x=c[i];
    42      while(i<j){
    43          while(i<j && c[j]<x)  j--;
    44          if(i<j){
    45              c[i]=c[j];
    46              i++;
    47          }
    48          while(i<j && c[i]>x)  i++;
    49         if(i<j){
    50             c[j]=c[i];
    51             j--;
    52         }
    53      }
    54      c[i]=x;
    55      qsort(c,left,i-1);
    56      qsort(c,i+1,right);
    57     }
    58  }
     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 int main(){
     5     int n;
     6     while(scanf("%d",&n),n){
     7         int t[1005],q[1005];
     8         for(int i=0;i<n;i++) scanf("%d",&t[i]);
     9         for(int i=0;i<n;i++) scanf("%d",&q[i]);
    10         sort(t,t+n); sort(q,q+n);
    11         int mit=0,mat=n-1;//tian_min、max 
    12         int miq=0,maq=n-1;//qi_min、max 
    13         int ans=0;
    14         while(mit<=mat){
    15             if(t[mit]>q[miq]) {ans++;mit++;miq++;}
    16             else if(t[mat]>q[maq]) {ans++;mat--;maq--;}
    17             else if(t[mit]==q[maq]){mit++;maq--;}
    18             else {ans--;mit++;maq--;}
    19         } printf("%d
    ",ans*200);
    20     } return 0;
    21 }
    我自倾杯,君且随意
  • 相关阅读:
    第一篇:fastadmin的页面是如何生成的?
    thinkphp join 表前缀
    python selenium firefox 添加cookie add_cookie
    python 多网站采集,解决编码问题
    How To Set Up vsftpd on CentOS 6
    How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6
    捕获浏览器的前进、后退事件 window.onhashchange 并区别于点击链接
    node之querystring
    详解html-webpack-plugin配置
    详解css-loader配置
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5158600.html
Copyright © 2011-2022 走看看