D - D 田忌赛马
D - D 田忌赛马 解题报告
hdu 1052 Tian Ji -- The Horse Racing
链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/D
题目:
Description
"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
Output
Sample Input
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
Sample Output
0
0
#include<cstdio>
#include<iostream>
#include<algorithm>
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 tq=n-,ts=,kq=n-,ks=;
int sum=;
for(int i=;i<n;i++)
{
if(a[tq]>b[kq])
{
sum+=;
tq--;
kq--;
}
else
{
if(a[tq]<b[kq])
{
sum-=;
ts++;
kq--;
}
else
{
if(a[ts]>b[ks])
{
sum+=;
ts++;
ks++;
}
else
{
if(a[ts]<b[kq])
sum-=;
ts++;
kq--;
}
}
}
}
printf("%d\n",sum);
}
return ;
}
D - D 田忌赛马的更多相关文章
- nyoj 364 田忌赛马(贪心)
田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Here is a famous story in Chinese history. "That ...
- [codevs2181]田忌赛马
[codevs2181]田忌赛马 试题描述 中国古代的历史故事"田忌赛马"是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金 ...
- ACM 田忌赛马
田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Here is a famous story in Chinese history. "That ...
- TYVJ P1048 田忌赛马 Label:dp
描述 中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱.现在每匹马的速 ...
- HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDOJ-1052 田忌赛马(贪心)
田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述: Here is a famous story in Chinese history. "That was ...
- [洛谷P1650] 田忌赛马
贪心难题:总结贪心问题的一般思路 传送门:$>here<$ 题意 田忌和齐王各有n匹马,赛马时一一对应.赢+200,输-200,平+0. 问最多多少钱? 数据范围:$n \leq 2000 ...
- Python3-大魔王小项目-田忌赛马
本人今天第一次接触项目,花了4小时,不包括学习时间,特此留个纪念 记录一下那些年走过的坑,以资鼓励 英语不怎么好,随缘看看 内容: 类似田忌赛马,三盘两胜,属性人物在一定范围内随机,就这样了 code ...
- python田忌赛马
一,简介 田忌赛马的故事大家都知道我就不展开说了,田忌能用同全面被碾压的马赢了齐威王(公子),我觉得这是十分具有智慧的.但是,如果说这里的条件改为:1,田忌的马比齐威王同等次的马弱一点但是比齐威王下一 ...
随机推荐
- java反编译命令javap
1. 输出所有类和成员 javap -private XX.class 2. 输出分解后的代码 javap -c XX.class
- nyist 500 一字棋
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=500 这太并不难,只要把情况分清楚就可以了,本人由于考虑不是很周全,WA了n次....悲 ...
- liunx 同步时间
ntpdate stdtime.gov.hk (美国) time.nist.gov (复旦)(国内用户推荐) ntp.fudan.edu.cn 微软公司授时主机(美国) time.windows.co ...
- SPOJ 7258 Lexicographical Substring Search(后缀自动机)
[题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...
- Eclipse连接sql server 2012数据库编程一条龙
一.java通过jdbc连接sql server 2012 原帖地址:http://blog.csdn.net/stewen_001/article/details/19553173/ 1.sql s ...
- 迭代器(iterators)
1.迭代器的概念 迭代器是一种抽象的设计概念.在设计模式中,迭代器模式定义为:提供一种方法,使之能够依序访问某个容器中所含的各个元素,而又无需暴露该容器的内部组织结构. 迭代器可以看做一种行为类似指针 ...
- 关于Windows Phone的资源文件Build Action属性
最近一朋友问了一个问题,他想引用一个本地图片到页面上的Image控件,可是发现用Application的GetResourceStream得到的结果是个null值,当时第一个想到的就是他图片的Buil ...
- [每日一题] 11gOCP 1z0-052 :2013-09-10 ABOUT ALERTS...............................................A50
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11546561 正确答案:AD 使用服务器生成的警报系统,从Oracle10g版本开始,Or ...
- .net mvc System.Web.Optimization 、System.Data.Entity.Infrastructure找不到
在MVC4的开发中,如果在App_Start目录下BundleConfig.cs类没有找不到引用System.Web.Optimization,可以使用程序包管理控制台进行安装到使用的项目 打开 工具 ...
- 【微信公众号】WeixinJSBridge.call('closeWindow')无效
公众号上面使用iframe嵌套子页面,然而子页面无法使用WeixinJSBridge.call('closeWindow') 这时候必须在函数前面加上parent.,比如parent.WeixinJS ...