hdu_1052 Tian Ji -- The Horse Racing 贪心
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
"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.
95
#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 贪心的更多相关文章
- POJ-2287.Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 17662 Acc ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)
题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...
- UVaLive 3266 Tian Ji -- The Horse Racing (贪心)
题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...
- HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论
题目链接:https://cn.vjudge.net/problem/HDU-1052 题意 田忌赛马问题扩展版 给n匹马,马的能力可以相同 问得分最大多少 思路 贪心做得还是太少,一开始一点思虑都没 ...
- UVa LA 3266 - Tian Ji -- The Horse Racing 贪心,不只处理一端,也处理另一端以理清局面 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【贪心】[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 ...
- HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】
算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...
- 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 ...
- 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 ...
随机推荐
- cls
class : python中cls代表的是类的本身,相对应的self则是类的一个实例对象. class Person(object): def __init__(self, name, age): ...
- python正则表达式去除文本中间的换行符
例如文本: 在这个活动中,请各位同学做一下自我介绍,也看看别的同学的自我介绍,看看谁的介绍更有个性!也许你能在这里找到志趣相投的学习伙伴.也希望大家能够彼此关注,相互鼓励,共同学习. 你的发帖可以包含 ...
- php连接数据库,php连接mysql并查询的几种方式,PHP PDO连接以及预处理
PHP连接数据库 面向过程 $config = [ 'host'=>'127.0.0.1', //数据库地址 'name'=>'test', //库名 'user'=>'root', ...
- github的学习使用以及将自己开发的app传上去。
主要参考的网址如下: https://www.cnblogs.com/sdcs/p/8270029.html https://www.cnblogs.com/sjhsszl/p/8708471.htm ...
- MySQL数据库二
筛选条件 比较运算符: 等于: = (注意!不是==) 大于等于: >= IS NULL 不等于: != 或 <> 小于: ...
- 7L-双线链表实现
链表是基本的数据结构,尤其双向链表在应用中最为常见,LinkedList 就实现了双向链表.今天我们一起手写一个双向链表. 文中涉及的代码可访问 GitHub:https://github.com/U ...
- C语言数据结构栈
#include<stdio.h>#include<stdlib.h>typedef struct Node{ int data; struct Node* pnext;}no ...
- 这可能是 Github 上最全面的 Flutter 教程
引语 晚上好,我是猫咪,我的公众号「程序媛猫咪」会推荐 GitHub 上好玩的项目,挖掘开源的价值,欢迎关注我. 刚下班到家,金三银四,虽然今天行情尤其地不好,但身边的同事也是走了一波,不免会受到影响 ...
- 《民国奇探》的弹幕有点逗比,用 Python 爬下来看看
电视剧<民国奇探>是一部充斥着逗比风的探案剧,剧中主要角色:三土.四爷.白小姐,三土这个角色类似于<名侦探柯南>中的柯南但带有搞笑属性,四爷则类似于毛利小五郎但有大哥范且武功高 ...
- ZooKeeper 如何保证数据一致性?
在分布式场景中,ZooKeeper 的应用非常广泛,比如数据发布和订阅.命名服务.配置中心.注册中心.分布式锁等. 在分布式场景中,ZooKeeper 的应用非常广泛,比如数据发布和订阅.命名服务.配 ...