HDOJ-1052 田忌赛马(贪心)
田忌赛马
时间限制: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.
样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
0
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 田忌赛马(贪心)的更多相关文章
- HDU 1052(田忌赛马 贪心)
题意是田忌赛马的背景,双方各有n匹马,下面两行分别是田忌和齐王每匹马的速度,要求输出田忌最大的净胜场数*每场的赌金200. 开始的时候想对双方的马匹速度排序,然后比较最快的马,能胜则胜,否则用最慢的马 ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
- POJ 2287 田忌赛马 贪心算法
田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...
- hdu1052 田忌赛马 —— 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...
- hdu1052(田忌赛马 贪心)
好坑的一道题,不过确实是贪心的一道好题,想了好久一直无法解决平局的情况. 参考了别人的思路,然后结合了自己的想法,总算是想出来了. 这题有些步骤是必须要执行的,有四个步骤 一.当期状态田忌的最慢的马 ...
- hdoj 1257 DP||贪心
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)
瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 #include<cstdio> #include ...
- hdu 1052 田忌赛马
贪心,排序从大到小.. 先比大的.跑只是就拿最小的来送死.. , 假设是平局就比后面的... 若后面也是平局就拿去跟前面的去跑. .. #include<stdio.h> #include ...
- luogu P1650 田忌赛马 |贪心
题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这里取得200银币. ...
- HDU 1052
http://acm.hdu.edu.cn/showproblem.php?pid=1052 田忌赛马本质就是一个贪心 res表示田忌的胜利场次 1.田忌最快马快于王的最快马,两个最快马比,res++ ...
随机推荐
- JavaScript判断对象的类型
JavaScript判断对象的类型 最近阅读了一些关于JavaScript判断对象类型的文章.总结下来,主要有constructor属性.typeof操作符.instanceof操作符和Object. ...
- python cookbook学习1
python cookbook学习笔记 第一章 文本(1) 1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t ...
- python cookbook学习笔记 第一章 文本(2)
1.6合并字符串 ka=list('kaluoc') #字符串转成字符串列表 print ''.join(ka) #大量的字符串相连,join是最高效的 print '%s%s something % ...
- Hibernate的clear(),flush(),evict()方法详解
1.Clear 方法 无论是Load 还是 Get 都会首先查找缓存(一级缓存) 如果没有,才会去数据库查找,调用Clear() 方法,可以强制清除Session缓存. 例: 这里虽然用了2个get方 ...
- x86中的页表结构和页表项格式
一.页表结构 分页转换功能由驻留在内存中的表来描述,该表称为页表(page table),存放在物理地址空间中.页表可看做简单的220个物理地址数组.线性到物理地址的映射功能可以简单地看做进行数组查找 ...
- 手机端H5 header定义样式
<meta content="width=device-width,initial-scale=1.0, maximum-scale=1.0, user-scalable=0" ...
- MPICH3.2 单机编译、安装及测试
MPI,即信息传递接口(Message Passing Interface),是基于消息传递这种并行计算模型的一个并行程序设计标准,可以直接通过C/C++.Fortran调用,目前最主要的实现由MPI ...
- OO的五大原则:SRP、OCP、LSP、DIP、ISP
OO的五大原则是指SRP.OCP.LSP.DIP.ISP. SRP -- (Single Responsibility Principle 单一职责原则) OCP--开闭原则(Closed for M ...
- 关于 CentOS 自启动(服务、脚本)
/etc/init.d /etc/rc.local 其实是软连接,所以,实际上看 /etc/rc.d/ 这个文件夹就好了 rc.local 是自启动脚本 正常情况下,用户自定义的服务在 init.d ...
- 【USACO】滑雪课程
滑雪课程贝西去科罗拉多州去滑雪,不过还她不太会玩,只是个能力为 1 的渣渣.贝西从 0 时刻进入滑雪场,一到 T 时刻就必须离开.滑雪场里有 N 条斜坡,第 i 条斜坡滑行一次需要 Di 分钟,要求游 ...