zoukankan      html  css  js  c++  java
  • 田忌赛马

    田忌赛马
    时间限制:3000 ms | 内存限制:65535 KB
    难度:1
    描述
    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

     题目大意:就是给你一个整数N。随后输入两行,一行N个数据,其中第一行是田忌马的速度。第二行是齐王马的速度。对于每场比赛,如果田忌输了,那么他就需要付给国王200块钱的彩头(话说那时候没有RMB,估计也就是200两银子吧)。你需要做的工作就是让田忌尽可能的收益最大。

     解题步骤

    首先对国王和田忌的马进行排序,然后对这些马进行适当的安排,以求收益最大。

    安排的规则;

    让田忌最慢的马跟齐王最慢的马进行比较

    1如果此时田忌的马速度大于齐王的马,让他们进行PK。毫无疑问,这场田忌胜 利。

    2如果此时田忌的马速度等于齐王的马,反正这场是赢不了了。我不如把齐王最快的马来下水。(集中自己弱势兵力跟敌人优势兵力同归于尽)你可能会疑问,这样收益不是变小了吗?呃····来,咱们来慢慢分析一下。

    如果我用最慢的马把齐王最快的马拉下水,那么我收益现在减少了200(相对平局而言),但是我最快的马就有机会干掉齐王最快的马了(可能之前这匹马不能干掉齐王之前最快的马)。此时我的总收益为0,但是跟之前第一场平局,最后一场负相比,我还是增加了200的收益。

    当然你会说,如果我的最快的马比齐王最快的马还快,我就不用换了,对,没错,既然我的实力远高于你齐王,我就可以完虐你,无需队友的牺牲。(在绝对实力面前,一切阴谋诡计都变得不堪一击)。

    3如果田忌最慢的马比齐王最慢的马还慢,还笨。反正都是输,我就拉着齐王最快的马一起下水吧,消耗了敌人的优势兵力,为队友创造机会。

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    bool comp(int x,int y)//sort排序

     if(x<y)
      return true;
        else
      return false;
     }
    int main()
    {    
     int i,j,k,l,n,m;
     int a[1005],b[1005];
     while(~scanf("%d",&n))    
     {  
      memset(a,0,sizeof(a));        
      memset(b,0,sizeof(b));        
      for(i=0;i<n;i++)          
       scanf("%d",&a[i]);
      sort(a,a+n,comp);
      for(i=0;i<n;i++) 
       scanf("%d",&b[i]); 
      sort(b,b+n,comp);    
      int ts=0,tq=n-1,ks=0,kq=n-1,sum=0;  
      while(ts<=tq)        
      {           
       if( a[ts] > b[ks] )//当田忌最慢的马比齐王最慢的马速度快的时候   
       {             
        ts++;     
        ks++;         
        sum+=200;     
       }     
       else if(a[ts]<b[ks])//当田忌最慢的马比齐王最慢的马还慢的时候。拉齐王最快的马下水。
       {  
        ts++;  
        kq--;    
        sum-=200;   
       }          
       else     //当田忌最慢的马跟齐王最慢的马速度相等时。   
       {              
        if(a[tq]>b[kq])//当田忌最快的马比齐王最快的马还快的时候,不需要牺牲  
        {            
         tq--;     
         sum+=200;  
         kq--;     
        }          
        else    
        {         
         if(a[ts]<b[kq])//田忌的最慢的马不一定比齐王最快的马弱,有可能相等、
          sum-=200;            
         ts++;        
         kq--;         
        }  
       }
            }      
      printf("%d\n",sum); 
       }    
     return 0;
    }
     

    可以用贪心来做:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define N 1001
    int tian[N],king[N];
    int n;//一边马的数目
    int cmp(const void *a,const void *b)
    {
    return *(int *)a-*(int *)b;
    }
    int main()
    {
    int i,j;
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)>0,n)
    {
       for(i=0;i<n;i++)
        scanf("%d",tian+i);
       for(i=0;i<n;i++)
        scanf("%d",king+i);
       qsort(tian,n,sizeof(int),cmp);
       qsort(king,n,sizeof(int),cmp);
       int t=n-1;
       int total=0;
       int pin=0;
       int l=0,lq=0;
       int lose=0;
            for(i=n-1;i>=l;i--)
       {
        for(j=t;j>=lq;j--)
        {
         if(tian[i]>king[j])
         {
          total++;
          t=j-1;
          break;
         }
         else if(tian[i]==king[j])
         {
          if(tian[l]>king[lq])//田忌最慢的比齐王最慢的快
          {
           total++;
           i++;//这里i++得体会一下
           l++;//l++表示第l 匹马已经比赛了
           lq++;
           break;
          }
          else//田忌最慢的比齐王最慢的慢或相等
          {
           if(tian[l]<king[j])//这里错了几次。有出现相等的情况,相等的时候lose不加
                             lose++;
           l++;
           t=j-1;
           i++;
           break;
          }
         }
         else //田忌用最慢的马对齐王最快的马
         {
          lose++;
          t=j-1;
          l++;
         }
        }
       }
       printf("%d\n",(total-lose)*200);
    }
    return 0;
    }

     
  • 相关阅读:
    05Mybatis_入门程序——根据id查询用户
    04Mybatis_搭建Mybatis的开发环境
    03Mybatis_mybatis框架原理——执行流程
    02Mybatis_原生态jdbc编程中的问题总结——从而引生出为什么要用Mybatis
    01Mybatis_课程安排
    21SpringMvc_异步发送表单数据到Bean,并响应JSON文本返回(这篇可能是最重要的一篇了)
    20SpringMvc_结果的转发可共享参数;重定向不能共享参数
    19SpringMvc_在业务控制方法中收集List集合中包含JavaBean参数
    18SpringMvc_在业务控制方法中收集数组参数
    阿里架构师,讲述基于微服务的软件架构模式(附资料)
  • 原文地址:https://www.cnblogs.com/feiguo/p/2496875.html
Copyright © 2011-2022 走看看