田忌赛马

时间限制:3000 ms | 内存限制:65535 KB 
难度:3 
描述:

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. 
输入 
The input consists of many 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. 
输出 
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

样例输入


92 83 71 
95 87 74 

20 20 
20 20 

20 19 
22 18

样例输出

200 

0

贪心策略:

如果 田忌最弱>大王最弱: 
田忌最弱PK大王最弱

如果 田忌最弱<大王最弱: 
田忌最弱PK大王最强

如果 田忌最弱=大王最弱{ 
如果田忌最强>大王最强 田忌最强PK大王最强 
否则:田忌最弱PK大王最强 
}

 #include <stdio.h>
#include <stdlib.h>
int compare(const void * p1, const void * p2) {
return *(int *)p2 - *(int *)p1;
}
int main(void)
{
int n;
while (~scanf("%d", &n) && n) {
int tian[];
int king[];
for (int i = ; i < n; i++) {
scanf("%d", &tian[i]);
}
qsort(tian, n, sizeof(int), compare);
for (int i = ; i < n; i++) {
scanf("%d", &king[i]);
}
qsort(king, n, sizeof(int), compare);
if (tian[] < king[n - ]) {
printf("%d\n", -( * n));
continue;
}
else if (tian[n - ] > king[]) {
printf("%d\n", * n);
continue;
}
int min_t = n - , min_k = n - ;
int max_t = , max_k = ;
int win = ;
while (n--) {
if (tian[min_t] > king[min_k]) {
//田弱vs王弱
win++;
min_t--;
min_k--;
continue;
}
else if (tian[min_t] < king[min_k]) {
//田弱vs王强
if (tian[min_t] != king[max_k])
win--;
min_t--;
max_k++;
continue;
}
else {
if (tian[max_t] > king[max_k]) {
//田强vs王强
win++;
max_t++;
max_k++;
continue;
}
else {
//田弱vs王强
if (tian[min_t] != king[max_k])
win--;
min_t--;
max_k++;
continue;
}
}
}
printf("%d\n", win * );
}
}

HDOJ-1052 田忌赛马(贪心)的更多相关文章

  1. HDU 1052(田忌赛马 贪心)

    题意是田忌赛马的背景,双方各有n匹马,下面两行分别是田忌和齐王每匹马的速度,要求输出田忌最大的净胜场数*每场的赌金200. 开始的时候想对双方的马匹速度排序,然后比较最快的马,能胜则胜,否则用最慢的马 ...

  2. hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

    思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...

  3. POJ 2287 田忌赛马 贪心算法

    田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...

  4. hdu1052 田忌赛马 —— 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...

  5. hdu1052(田忌赛马 贪心)

    好坑的一道题,不过确实是贪心的一道好题,想了好久一直无法解决平局的情况.  参考了别人的思路,然后结合了自己的想法,总算是想出来了. 这题有些步骤是必须要执行的,有四个步骤 一.当期状态田忌的最慢的马 ...

  6. hdoj 1257 DP||贪心

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)

    瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 #include<cstdio> #include ...

  8. hdu 1052 田忌赛马

    贪心,排序从大到小.. 先比大的.跑只是就拿最小的来送死.. , 假设是平局就比后面的... 若后面也是平局就拿去跟前面的去跑. .. #include<stdio.h> #include ...

  9. luogu P1650 田忌赛马 |贪心

    题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这里取得200银币. ...

  10. HDU 1052

    http://acm.hdu.edu.cn/showproblem.php?pid=1052 田忌赛马本质就是一个贪心 res表示田忌的胜利场次 1.田忌最快马快于王的最快马,两个最快马比,res++ ...

随机推荐

  1. 目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的选择

    目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的选择 ASP.NET Web API能够根据请求激活目标HttpController ...

  2. Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题

    1.下面是不生效的布局: selector_btn_red.xml: <?xml version="1.0" encoding="utf-8"?> ...

  3. Microsoft.VisualBasic.dll的妙用and 改善C#公共程序类库质量的10种方法

    Microsoft.VisualBasic.dll的妙用(开发中肯定会用到哦) 前言 做过VB开发的都知道,有一些VB里面的好的函数在.NET里面都没有,而Microsoft.VisualBasic. ...

  4. LAMP on ubuntu12.04 PHP, Apache2, MySQL, Linux ( with phpmyadmin installed)

    there are several procedure which include: 1. Install the packages sudo apt-get install php5 php5-gd ...

  5. http概览

    http概览 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它可以使浏览器更加高效,使网络传输减少.它不仅 ...

  6. mysql数据类型简介

    MySQL的数据表类型很多,其中比较重要的是MyISAM,InnoDB这两种. 这两种类型各有优缺点,需要根据实际情况选择适合的,MySQL支持对不同的表设置不同的类型.下面做个对比: MyISAM表 ...

  7. [置顶] “河软CSDN2011级表彰暨实习动员大会”顺利召开!

    9点30分 伴随着激昂的开场曲,主持人走到台前!“河软CSDN2011级表彰暨 实习动员大会即将开始,请各位嘉宾入场!”他们分别是“CSDN教育事业部总经 理李天山先生”“河北软件职业技术学院 软件工 ...

  8. CentOS6.5编译安装Python-2.7

    zlib.x86_64 * yum install zlib.x86_64 openssl-devel.x86_64 * yum install openssl-devel.x86_64 Python ...

  9. CSS3特性修改(自定义)浏览器默认滚动条

    前言:我们做前端时,会遇到一些需求,要求把默认浏览器的滚动条样式给改写了,诶.好好的改它干啥了,也带不来用户体验,就是好看点嘛!实现原理其实是用了伪元素,webkit的伪元素实现很强,可以把滚动条当成 ...

  10. 类xml数据格式解析

    需要解析一种类xml的数据文件,数据格式1如下: <head> //文件头 <type>xtype</type> <condition> key1=va ...