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): 11572 Accepted Submission(s): 3239
"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.
二、如果a的最慢速度小于b的最慢,则用a的最慢浪费b的最快,输一场;
三、如果a的最慢速度等于b的最慢,则:
1.如果a的最快速度大于b的最快,则直接a的最快与b的最快进行比赛,赢一场;
2.如果a的最快速度小于b的最快,则用a的最慢浪费b的最快,输一场;
3.如果a的最快速度等于b的最快,即a与b的最慢与最快分别相等,则:
a.如果a的最慢速度小于b的最快,则用a的最慢浪费b的最快,输一场;
b.如果a的最慢速度等于b的最快,即a的最慢、a的最快、b的最慢、b的最快相等,
说明剩余未比赛的马速度全部相等,直接结束比赛。
/*
HDU 1052
一、如果a的最慢速度大于b的最慢,则直接a的最慢与b的最慢比赛,赢一场;
二、如果a的最慢速度小于b的最慢,则用a的最慢浪费b的最快,输一场;
三、如果a的最慢速度等于b的最慢,则:
1.如果a的最快速度大于b的最快,则直接a的最快与b的最快进行比赛,赢一场;
2.如果a的最快速度小于b的最快,则用a的最慢浪费b的最快,输一场;
3.如果a的最快速度等于b的最快,即a与b的最慢与最快分别相等,则:
a.如果a的最慢速度小于b的最快,则用a的最慢浪费b的最快,输一场;
b.如果a的最慢速度等于b的最快,即a的最慢、a的最快、b的最慢、b的最快相等,
说明剩余未比赛的马速度全部相等,直接结束比赛。 */
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAXN=;
int a[MAXN],b[MAXN];
int main()
{
int n;
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 al=,ah=n-;
int bl=,bh=n-;
int ans=;
while(al<=ah&&bl<=bh)
{
if(a[al]>b[bl])
{
ans+=;
al++;bl++;
}
else if(a[al]<b[bl])
{
ans-=;
al++;bh--;
}
else
{
if(a[ah]>b[bh])
{
ans+=;
ah--;bh--;
}
else if(a[ah]<b[bh])
{
ans-=;
al++;bh--;
}
else
{
if(a[al]<b[bh])
{
ans-=;
al++;bh--;
}
else if(a[al]==b[bh])//所有的都一样了
{
break;
}
}
}
}
printf("%d\n",ans);
}
return ;
}
HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)的更多相关文章
- HDU 1052 Tian Ji -- The Horse Racing(贪心)
题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...
- 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 (J ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...
- hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】
题目 这道题主要是需要考虑到各种情况:先对马的速度进行排序,然后分情况考虑: 1.当田忌最慢的马比国王最慢的马快则赢一局 2.当田忌最快的马比国王最快的马快则赢一局 3.当田忌最快的马比国王最快的马慢 ...
- 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)
http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
- POJ-2287.Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 17662 Acc ...
随机推荐
- week6 作业
week6 作业 1.每12小时备份并压缩/etc/目录至/backup目录中,保存文件名称格式为"etc-年-月-日-时-分.tar.gz" crontab -e */1 * * ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 时间限制: 10 Sec 内存限制: 259 MB 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他 ...
- auth 认证组件的补充
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...
- 【python】windows更改jupyter notebook(ipython)的默认打开工作路径
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- vue初级 总结
mvvm m:代表 data v 代表 view vm 代表 Vue 的实例 v-cloak 指令 解决闪烁的问题 需要在 style 标签中加入 [v-cloak];{ display:none } ...
- 二十二:视图之add_url_rule和app.route
flask注册视图有两种方式 一:add_url_rule()add_url_rule源码:rule:接口地址view_func=视图函数endpoint=终结点,可以理解为给当前视图取的别名,最直观 ...
- 【DVWA】Brute Force(暴力破解)通关教程
日期:2019-08-01 14:49:47 更新: 作者:Bay0net 介绍:一直以为爆破很简单,直到学习了 Burp 的宏录制和匹配关键词,才发现 burp 能这么玩... 0x01. 漏洞介绍 ...
- Platform区分不同平台
用于区分平台 OS 属性 表示当前的平台类型.只有 ios 与 android 两个值.如可以使用为同一个属性在不同的平台上赋不同的值 const styles = StyleSheet.create ...
- 【VS开发】ActiveX控件如何定制属性?
在很多场合下会存在这样的需求,那就是使用方在实际使用控件之前就想控件已经做了相应的处理比如加载的控件版本不正确等,或者需要在加载时才确定能够使用的功能集:这个时候传统的配置文件已经无法满足这种类型的需 ...
- vue --》路由query 编程式导航传值与监听
1.首先在一个页面设置一个按钮,用于路由跳转 <template> <div> <button @click="handleToRouter"> ...