Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40477    Accepted Submission(s): 12148

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

思路:
一共要分 n 组比赛,每次分三种情况:
  1. 用田忌的坏马与齐王的坏马比较,能获胜继续下一组,否则进行 2 。 
  2. 用田忌的好马与齐王的好马比较,能获胜继续下一组, 否则进行 3 。
  3. 用田忌的坏马与齐王的好马比较,这时候只有两种情况,坏马<好马 和 坏马=好马。
 
要注意坏马与坏马比较时候, 就算相等也不是最好的情况, 应该用这匹坏马与齐王的好马比。 这样才能给后面的分组带来更大的优势。
 
#include <iostream>
#include <stdio.h>
#include <algorithm> using namespace std; int main()
{
int a[],b[],n,win,lose; while(scanf("%d",&n) && n)
{
for(int i=; i<n; i++)
{
scanf("%d", &a[i]);
}
for(int i=; i<n; i++)
{
scanf("%d", &b[i]);
} sort(a, a+n);
sort(b, b+n); int Tslow=, Tfast=n-, Kslow=, Kfast=n-;
win=;
while(n--)
{
if(a[Tslow] > b[Kslow])
{
win++;
Tslow++;
Kslow++;
}
else if(a[Tfast] > b[Kfast])
{
win++;
Tfast--;
Kfast--;
}
else if(a[Tslow]<b[Kfast])
{
win--;
Tslow++;
Kfast--;
}
else if(a[Tslow]==b[Kfast])
continue;
else printf("e");
} printf("%d\n", win*); }
return ;
}
 
 

hdu_1052 Tian Ji -- The Horse Racing 贪心的更多相关文章

  1. POJ-2287.Tian Ji -- The Horse Racing (贪心)

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17662   Acc ...

  2. HDU 1052 Tian Ji -- The Horse Racing(贪心)

    题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...

  3. UVaLive 3266 Tian Ji -- The Horse Racing (贪心)

    题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...

  4. HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论

    题目链接:https://cn.vjudge.net/problem/HDU-1052 题意 田忌赛马问题扩展版 给n匹马,马的能力可以相同 问得分最大多少 思路 贪心做得还是太少,一开始一点思虑都没 ...

  5. UVa LA 3266 - Tian Ji -- The Horse Racing 贪心,不只处理一端,也处理另一端以理清局面 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. 【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...

  7. HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

    算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...

  8. HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. HDU 1052:Tian Ji -- The Horse Racing(贪心)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine)

    A. Dead Pixel(思路) 思路 题意:给我们一个m*n的表格,又给了我们表格中的一个点a,其坐标为(x, y),问在这个表格中选择一个不包括改点a的最大面积的矩形,输出这个最大面积 分析:很 ...

  2. Debug 是门艺术

    最近想结合发生在身边码农身上的一些小故事,尝试表达一个观点“Coding 是门技术,Debug 是门艺术”. 上期的分享<Coding 是门技术>主要通过引入身边 Code farmer ...

  3. 如何将MagicaVoxel模型导入UE4中(2)

    前言 当可以把MagicaVoxel的静态模型导入到UE4后,我又开始不满足了.默认第三人称蓝图的"汽车碰撞人偶(雾)"与场景中的体素画风格格不入,于是,我便想着用自己建造的体素画 ...

  4. PTA数据结构与算法题目集(中文) 7-16

    PTA数据结构与算法题目集(中文)  7-16 7-16 一元多项式求导 (20 分)   设计函数求一元多项式的导数. 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000 ...

  5. 《操作系统》课程笔记(Ch01-导论)

    Ch01 - 导论 操作系统的功能 用户视角:在乎使用方便,不在乎资源利用 系统视角:资源分配器.控制程序 计算机系统的运行 启动:利用固件(Firmware)中的引导程序(Bootstrap Pro ...

  6. Google GMS介绍

    Google GMS介绍GMS全称为GoogleMobile Service.GMS目前提供有Search.Search by Voice.Gmail.Contact Sync.Calendar Sy ...

  7. Linux网络安全篇,进入SELinux的世界(一)

    SELinux 即安全强化的Linux. 一.基本概念 SELinux是通过MAC(强制访问控制,,可以针对特定的进程与特定的文件资源来进行访问权限的控制!也就是说即使你是root,在使用不同的进程时 ...

  8. [ddt02篇]十年测试老鸟帮您解析:ddt结合txt,excel,csv,mysql实现自动化测试数据驱动

    一.前言: 阅读此文之前请先阅读: [ddt01篇]十年测试老鸟帮您解析:ddt数据驱动入门基础应用:https://www.cnblogs.com/csmashang/p/12679448.html ...

  9. spark sql createOrReplaceTempView registerTempTable

    createOrReplaceTempView2.x版本以上. registerTempTable1.5.x val data1 = dataSelect1(sqlContext, sparkMode ...

  10. char类型及ASCII码之间比较

    在JAVA中,char类型可以直接运算,char在ASCII等字符编码表中有对应的数值对char类型字符运行时,直接当做ASCII表对应的整数来对待 参考 https://blog.csdn.net/ ...