Tian Ji -- The Horse Racing(考虑周到或者省去相等)

2024-06-01 03:48

本文主要是介绍Tian Ji -- The Horse Racing(考虑周到或者省去相等),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Tian Ji -- The Horse Racing

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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

Source

#include<stdio.h>
#include<stdlib.h>


int cmp(const void *a,const void *b)
{
    return *(int *)b - *(int *)a;
}
int main()
{
    int i,j,k,s,x,n,t[1000],q[1000];
    while (scanf("%d",&n),n)
    {
          for (i=0;i<n;i++)
                scanf("%d",&t[i]);
          for (i=0;i<n;i++)
                scanf("%d",&q[i]);


          qsort(t,n,sizeof(t[0]),cmp);
          qsort(q,n,sizeof(q[0]),cmp);


          for (i=0;i<n;i++)
          {
              if(t[0] >= q[i]) break;           //找第一个田忌能赢的国王的马
          }


          for (s=-200*n;i<n;i++)
          {
                x=-i*200;                                         //前面不可能战胜的国王的马
                for (j=i,k=0;j<n;j++,k++)
                {
                      if (t[k]>q[j])                 //田忌赢
                            x+=200;
                      else if (t[k]<q[j])               //国王赢
                            x-=200;
                }
                if (x>s)                                       //记录最大值
                      s=x;
          }
          printf("%d\n",s);
    }
    return 0;
}

这篇关于Tian Ji -- The Horse Racing(考虑周到或者省去相等)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1019907

相关文章

nyoj 1037 Postscript of Tian Ji racing

一道卡贪心的题。 也算一道改编题。 此题的解法推荐为二分图的最大匹配。 首先将输入数据转换一下,然后将满足题意的一组牌建立条边,最终边的覆盖数即为 LN 最后可得的分数。 然后求出最大匹配即可。 代码如下: #include<stdio.h>#include<string.h>char card[30][5];char s[5];int map[30][30];

LEAN 类型理论之注解(Annotations of LEAN Type Theory)—— 定义上相等(Definitional Equality)

定义上相等(Definitional Equality)指的是意义上相等,即同义,包括了,定义的缩写(Abbreviatory Definition),alpha转换,相同替代(substituting equals for equals)等。下面是LEAN给定的何谓 定义上相等。          注:罗列的推演规则中,如自明其义的,则不多加解析其前提、结果、或特定注解。

力扣(距离相等的条形码)

1054. 距离相等的条形码 提示 在一个仓库里,有一排条形码,其中第 i 个条形码为 barcodes[i]。 请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。 你可以返回任何满足该要求的答案,此题保证存在答案。 思路: 首先利用一个数组保存每个元素出现次数,即dp[i]表示i出现的次数。 然后先处理出现次数最多的元素,处理方法是从0开始,每隔两个存放一个该元素直到全

C# 7个方法比较两个对象是否相等

前言 在现实中的编程生活里,我们时常遇到一个棘手的问题:如何比较两个相同类型的对象是否 “相等”,比如在 ERP 系统中,企业的信息非常重要,每一次更新维护,都需要系统自动地详细记录更新前后企业不一致的信息、更新时间和更新人等等。 但是,直接比较通常只能告诉我们它们是否指向同一个内存地址,而不能告诉我们它们的内容是否一致,所以即使两个相同类型的对象的值都一致,程序还是偏偏说:“对不起,他们

《高等代数》行(列)和相等行列式

说明:此文章用于本人复习巩固,如果也能帮助到大家那就更加有意义了。 注:1)行(列)和相等行列式的求解方法是将其于行都加到第一行(列),然后再提取第一行                 (列),使得第一行(列)变成“1”,再用第一行(列)将行列式化为三角形行列式。

【大数据算法】时间亚线性算法之:串相等判定算法。

串相等判定算法 1、引言2、串相等判定算法2.1 定义2.2 核心原理2.3 应用场景2.4 算法公式2.4.1 Rabin-Karp算法2.4.2 哈希函数 2.5 代码示例 3、总结 1、引言 小屌丝:鱼哥, 啥是串相等判定算法啊 小鱼:这个… en…en… 小屌丝:咋了,这个问题难住你了? 不能吧 小鱼:难住了,难住了, 我现在饿的迷糊了。 小屌丝:我~ 这个真是的。 这时

453.最小操作次数使数组元素相等

453.最小操作次数使数组元素相等 给你一个长度为 n 的整数数组,每次操作将会使 n - 1 个元素增加 1 。返回让数组所有元素相等的最小操作次数。 示例 1: 输入:nums = [1,2,3] 输出:3 解释: 只需要3次操作(注意每次操作会增加两个元素的值): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 示例 2: 输入:nums = [1,1

两个浮点数相等比较

你想问的是这个: 浮点数判断需要注意,float 和double 的精度范围,超过范围的数字会被忽略 (1)  浮点数大小判断 如果没有等号关系在里面,也就必然一大一小,那么直接用  > 或者 < (2) 浮点数相等判断 因为 浮点数在内存中存放,可能无法精确的储存,所以同一个值,可能有不同的内存数据,所以要使用以下的方法: 以float 为例,32位APP中精度为 6-7,所以取

【412】【统计近似相等数对 II】

差130个样例,等佬解 class Solution:def ifqual(self,str1,str2):return int(str1)==int(str2)def change(self,str1,str2):str1 = list(str1)n=len(str1)t=0for i in range(n):for j in range(i+1,n):str1[i],str1[j]=st