• nyoj 364 田忌赛马


     

    田忌赛马

    时间限制: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
    
    View Code
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 int cmp(const void*x,const void*y)
     4 {
     5       return *(int*)y-*(int*)x;
     6 }
     7 
     8 int main()
     9 {
    10       int n;
    11       int i;
    12       int tian[1010],king[1010];
    13       while(~scanf("%d",&n))
    14       {
    15             for(i=0;i<n;i++)
    16             {
    17                   scanf("%d",&tian[i]);
    18             }
    19             for(i=0;i<n;i++)
    20             {
    21                   scanf("%d",&king[i]);
    22             }
    23             qsort(tian,n,sizeof(tian[0]),cmp);
    24             qsort(king,n,sizeof(king[0]),cmp);
    25             int tian_first=0,tian_last=n-1,king_first=0,king_last=n-1;
    26             int win=0,lost=0;
    27             while(tian_first<=tian_last)
    28             {
    29                   if(tian[tian_first]>king[king_first])
    30                   {
    31                         win++;
    32                         tian_first++;
    33                         king_first++;
    34                   }
    35                   else if(tian[tian_last]>king[king_last])//之前漏了这一段,以为打不过就消耗国王好马,其实可能大的打平手。
    36                   {
    37                         win++;
    38                         tian_last--;
    39                         king_last--;
    40                   }
    41                   else
    42                   {
    43                         if(tian[tian_last]<king[king_first])
    44                         lost++;
    45                         tian_last--;
    46                         king_first++;
    47 
    48                   }
    49 
    50 
    51             }
    52             printf("%d\n",(win-lost)*200);
    53       }
    54 
    55 }
  • 相关阅读:
    创建供应商-采购模块
    定义容差组
    前台创建供应商-财务角度
    对供应商账户组分配编号范围
    拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model
    拓端数据tecdat|R语言建模收入不平等:分布函数拟合及洛伦兹曲线(Lorenz curve)
    拓端数据tecdat|R语言中的多项式回归、局部回归、核平滑和平滑样条回归模型
    拓端数据tecdat|R语言ARIMA,SARIMA预测道路交通流量时间序列:季节性、周期性
    拓端数据tecdat|ARIMA模型预测CO2浓度时间序列
    拓端数据tecdat|R语言基于递归神经网络RNN的温度时间序列预测
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2764897.html
Copyright © 2020-2023  润新知