田忌赛马

时间限制: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. LinqToXml高级用法介绍

    LinqToXml高级用法介绍 一.函数构造 什么是函数构造?其是指通过单个语句构建XML树的能力. 那么它有什么作用呢? 作用1.用单个表达式快速创建复杂的XML树 见实例代码CreateXml( ...

  2. 数模学习笔记(五)——BP神经网络

    1.BP神经网络是一种前馈型网络(各神经元接受前一层的输入,并输出给下一层,没有反馈),分为input层,hide层,output层 2.BP神经网络的步骤: 1)创建一个神经网络:newff a.训 ...

  3. 大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建

    大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建 大数据应用之Windows平台Hbase客户端Eclipse环境搭建-Java版 作者:张子良 版权所有,转载请注明出处 引子 ...

  4. linux下http服务器开发

    linux下http服务器开发 1.mystery引入 1)超文本传输协议(HTTP)是一种应用于分布式.合作式.多媒体信息系统的应用层协议 2)工作原理 1)客户端一台客户机与服务器建立连接后,会发 ...

  5. [置顶] MyElipse9.0 M1安装svn(测试100%通过)

    为什么标题要写100%通过呢?原因是以前的方法(直接复制到plugin里(MyEclipse 6.0可以,我试过),link安装)都不好用了,9.0M1不吃这一套,所以告诉大家这么做一定能够装上!! ...

  6. C++ 全局构造函数调用的顺序

    C++的全局类和静态类的构造函数是在main函数之前调用的.但是,不同的类的构造函数以什么顺序调用呢? 对于g++编译器来说,这个顺序是由链接时,文件顺序决定的. 我们用一个例子来说明这一点. 我们有 ...

  7. phper談談最近重構代碼的感受(2)

    重构代码更多的是对程序的可读性和可扩展性上做一些优化. 首先我对可读性进行细化.借鉴大神川山甲的重构系列文http://www.cnblogs.com/baochuan/archive/2012/03 ...

  8. Hybrid App(一)App开发选型

    1.几种app开发模式概述 Native App 即传统的原生APP开发模式,Android基于Java语言,底层调用Google的 API;iOS基于OC或者Swift语言,底层调用App官方提供的 ...

  9. H5与CS3权威下.19 选择器(2)结构性伪类选择器

    1.CSS中的伪类选择器及伪元素 (1)与自定义的class类选择器不同,伪类选择器是CSS中已经定义好的选择器. eg:a:link{color:#ff0000;} (2)伪元素的使用方法: 选择器 ...

  10. 驱动05.lcd设备驱动程序

    参考s3c2410fb.c总结出框架 1.代码分析 1.1 入口函数 int __devinit s3c2410fb_init(void) { return platform_driver_regis ...