Tian Ji -- The Horse Racing

Problem Description
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.

 
Input
The input consists of up to 50 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. The input ends with a line that has a single 0 after the last test case.
 
Output
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
 
Sample Input
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
 
Sample Output
200
0
0

分析:题意就是田忌赛马的扩展,田忌和国王每个人有n匹马,然后两个人每匹马两两对应匹配,谁的马速度快谁就赢一局,求怎么匹配使得田忌赢的局数尽可能多。

题目有个误解让人以为是二分图最大匹配,其实是个贪心,但对我来说不好想。分三种情况:

1.田忌最慢的马比国王最慢的马还慢,则拿去与国王最快的马PK,反正都是输,不如拿去把对方最快的比掉,则后面马赢的机会就更大。

   2.田忌最慢的马比国王最慢的马快,则田忌赢一局。

    3.田忌最慢的马与国王最慢的马速度相等,则不能打平,因为打平与一平一负效果等价,还不如把田忌最慢的马去与国王最快的马比,为后面的马争取更大的赢的机会。

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
#define INF 100000
typedef long long ll;
const int maxn=;
int tian[maxn],king[maxn];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n)){
if(n==) break;
memset(tian,,sizeof(tian));
memset(king,,sizeof(king));
for(int i=;i<n;i++)
scanf("%d",&tian[i]);
for(int i=;i<n;i++)
scanf("%d",&king[i]);
sort(tian,tian+n);
sort(king,king+n);
int win=,lose=;
int t_low=,t_best=n-,k_low=,k_best=n-;
while(t_low<=t_best){
if(tian[t_low]>king[k_low]){
win++;
t_low++;
k_low++;
}
else if(tian[t_low]<king[k_low]){
lose++;
t_low++;
k_best--;
}
else{
if(tian[t_best]>king[k_best]){
win++;
t_best--;
k_best--;
}
else{
if(tian[t_low]<king[k_best])
lose++;
t_low++;
k_best--;
}
}
}
int ans=(win-lose)*;
printf("%d\n",ans);
}
return ;
}

HDU-1052(贪心策略)的更多相关文章

  1. HDU 1052 贪心+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...

  2. HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

    算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...

  4. E - 不爱学习的lyb HDU - 1789(贪心策略)

    众所周知lyb根本不学习.但是期末到了,平时不写作业的他现在有很多作业要做. CUC的老师很严格,每个老师都会给他一个DDL(deadline). 如果lyb在DDL后交作业,老师就会扣他的分. 现在 ...

  5. Tian Ji -- The Horse Racing HDU - 1052

    Tian Ji -- The Horse Racing HDU - 1052 (有平局的田忌赛马,田忌赢一次得200块,输一次输掉200块,平局不得钱不输钱,要使得田忌得到最多(如果只能输就输的最少) ...

  6. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  7. hdu 1009 贪心基础题

    B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bi ...

  8. LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)

    Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...

  9. HDU - 1789 贪心

    贪心策略:按照分数降序排列,如果分数相同将截止时间早的排在前面.每次让作业尽量晚完成,因此需要逆序枚举判断这一天是否已经做了其他作业,如果没时间做这个作业说明不能完成,否则将这一天标记. AC代码 # ...

  10. poj1328 Radar Installation(贪心 策略要选好)

    https://vjudge.net/problem/POJ-1328 贪心策略选错了恐怕就完了吧.. 一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...

随机推荐

  1. BZOJ3230: 相似子串

    3230: 相似子串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 223[Submit][Status]Descripti ...

  2. android中如何实现离线缓存

    离线缓存就是在网络畅通的情况下将从服务器收到的数据保存到本地,当网络断开之后直接读取本地文件中的数据. 将网络数据保存到本地: 你可以自己写一个保存数据成本地文件的方法,保存在android系统的任意 ...

  3. ubuntu 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)

    在用sudo apt-get install kmymoney2安装软件kmymoney2时,由于速度太慢,想换个软件源,直接关闭了终端,apt-get但进程没有结束,结果终端提示:"E: ...

  4. 问题记录:spark读取hdfs文件出错

    错误信息: scala> val file = sc.textFile("hdfs://kit-b5:9000/input/README.txt") 13/10/29 16: ...

  5. Spring ’14 Wave Update: Installing Dynamics CRM on Tablets for Windows 8.1

    One of the added bonuses of Dynamics CRM is its ability go where you go! With the Spring ’14 Wave Up ...

  6. 【转】ldconfig和ldd用法

    ldconfig和ldd用法 一.ldconfig ldconfig --helpUsage: ldconfig [OPTION...]Configure Dynamic Linker Run Tim ...

  7. PHP函数补完:var_export()

    var_export() 函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码.var_export必须返回合法的php代码, 也就是 ...

  8. NTC(负温度)热敏电阻.阻值的计算方式

    来源 :http://blog.csdn.net/blue0432/article/details/8690190 现在低成本测温方案中NTC热敏电阻用的比较多,一般采用查表的方法获取温度值,这就牵涉 ...

  9. MyCat集群部署(HAProxy + MyCat)

    本文档内容的依赖龙果学院<基于Dubbo的分布式系统架构实战>课程 二.软件版本 操作系统:CentOS-6.6-x86_64 JDK版本:jdk1.7.0_72 HAProxy版本:ha ...

  10. Emmet:一个Html/Css快速编辑神器的插件

    一.介绍:Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度 二.使用 ...