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++ ...
随机推荐
- iOS多线程的初步研究1
iOS多线程的初步研究(一) 对于多线程的开发,iOS系统提供了多种不同的接口,先谈谈iOS多线程最基础方面的使用.产生线程的方式姑且分两类,一类是显式调用,另一类是隐式调用. 一.显示调用的类为NS ...
- IL反编译的实用工具
初识Ildasm.exe——IL反编译的实用工具 Ildasm.exe 概要: 一.前言: 微软的IL反编译实用程序——Ildasm.exe,可以对可执行文件(ex,经典的控制台Hello Wor ...
- IS动态左侧菜单-01
<%@ Page Language="C#" CodeFile="Default3.aspx.cs" Inherits="Default3&qu ...
- OpenCV249 for python278 最简配置方案
跟很多开源软件一样OpenCV也提供了完善的Python接口,非常便于调用.OpenCV 包含了超过 2500 个算法和函数,几乎任何一个能想到的成熟算法都可以通过调用OpenCV的函数来实现,超级方 ...
- xls===>csv tables===via python ===> sqlite3.db
I've got some files which can help a little bit to figure out where people are from based on their I ...
- Codeforces Round #193 (Div. 2)
题目地址: http://codeforces.com/contest/332 第一题:题目又臭又长,读了好长时间才读懂. n个人,你是0号,从0开始到n-1循环做动作,只要你前面三个人动作一样,你就 ...
- POJ 1915 经典马步 双向bfs
拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...
- siverlight 后台动态设置图片路径的总结
最近碰到了个问题,需要给一个用户控件中的image动态设置图片资源 1.图片资源属性为resource时,静态引用无任何问题,但是动态设置时,就什么也不显示 后来找到问题所在, 必须把此图片属性项中“ ...
- JS,CSS是前端,JAVA PHP ASP是后端,数据库是后端的处理对象,非代表前后底
大海-mysql-oracle(529513481) 19:02:18 象我这边,前台都是php,而php做数据分析是不太理想的,做中间件没人力,难办 横瓜(601069289) 19:20:15 ...
- Android打开系统设置
今天在做项目过程中,遇到一个问题:用户体验某个功能时需要查看用户是否已经打开了GPS定位服务,若没有则要求进入定位服务设置界面. 下面就直接贴出代码 以下代码是放在了Button的监听事件里,只贴出重 ...