田忌赛马

时间限制: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. iOS多线程的初步研究(六)

    iOS多线程的初步研究(六) iOS平台提供更高级的并发(异步)调用接口,让你可以集中精力去设计需完成的任务代码,避免去写与程序逻辑无关的线程生成.运行等管理代码.当然实质上是这些接口隐含生成线程和管 ...

  2. 迷你MVVM框架 avalonjs 0.85发布

    迷你MVVM框架 avalonjs 0.85发布 本版本对循环绑定做了巨大改进,感谢@soom, @limodou, @ztz, @Gaubee 提供的大量测试文件. fix scanNodes, 在 ...

  3. linux 原生系统发送电子邮件 (在本地与因特网)

    有用的资料在 Linux mail 命令 http://www.cnblogs.com/JemBai/archive/2012/01/24/2329136.html 还有这里 Linux系统下mail ...

  4. cocos2d-x protobuf; cocos2dx protocol buffer

    昨天了解到项目要用到protocol buffer,今天晚上看了一下,了解protobuf本质上就是一个信息表达协议+编辑,解析库. linux开源软件都一个模式,先./configure --hel ...

  5. Ubuntu12.04 Eclipse 提示框背景色修改

    I had to edit these files: /usr/share/themes/Ambiance/gtk-3.0/settings.ini /usr/share/themes/Ambianc ...

  6. C# 中判断字符串是不是汉字

    //1.用ASCII码判断 //在 ASCII码表中,英文的范围是0-127,而汉字则是大于127,具体代码如下: string text = "是不是汉字,ABC,柯乐义"; ; ...

  7. C++中的异常

    一,异常的推演 1.函数与异常 平时我们在函数中出现异常情况时通常通过return终止函数并返回一个值,然后在函数上层来获取值并判断是什么异常情况.因为函数是栈结构的,所以return的时候是通过栈结 ...

  8. C++ 头文件系列(queue)

    简介 这个头文件定义了两个跟队列有关的类----quque.priority_queue,分别实现的是队列 和 优先队列这两个概念. 但是与这两个类模版与其它类模版(vector.array等)最大的 ...

  9. vim编辑器的简单使用

    写这篇文章是因为在更新我的一篇博客 Git的其他用法 的时候,里面的修改已经提交的commit说明这一部分需要用到vim. 在使用git config --global --edit或者git reb ...

  10. PHP关于foreach使用引用变量的坑

    写PHP好多年,但仍然会犯低级错误,今天遇到个 foreach中引用变量时的坑,PHP版本为 5.6.12 代码如下: <?php $arr = ['a', 'b', 'c', 'd', 'e' ...