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. Android ImageView的属性android:scaleType

    ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType) imageView.setScaleType(Im ...

  2. linux c遍历文件夹 和文件查找的方法

    linux c遍历文件夹的方法比较简单,使用c来实现 #include <iostream> #include <stdio.h> #include <sys/types ...

  3. 【CSS】Intermediate6:Display

    1display :inline/block/none 2.inline value Cause all list items in a list to appear next to each oth ...

  4. window上使用GIT的个人经验(入门级)

    0.安装 使用google上的msysgit http://code.google.com/p/msysgit/downloads/list 尽量用最新版的吧 1.KEY 关于 key,.ssh里面的 ...

  5. md5可能会被破解咋办?

    所谓加Salt,就是加点“佐料”.其基本想法是这样的——当用户首次提供密码时(通常是注册时),由系统自动往这个密码里撒一些“佐料”,然后再散列.而当用户登录时,系统为用户提供的代码撒上同样的“佐料”, ...

  6. Spark在集群中的安装

    今天由于所以要安装spark做一些实验.我已有的环境是: 操作系统:CentOS6.5 hadoop:hadoop2.4.1 JDK:1.7 集群环境:四个节点   闲话不说,以下是我的安装步骤: 说 ...

  7. HW2.18

    public class Solution { public static void main(String[] args) { System.out.println("a" + ...

  8. Dynamices CRM JS 类库 神器 XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library

    XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library http://xrmservic ...

  9. c语言 函数返回二位数组 函数参数为二维数组

    通过typedef可以简单实现.也可以直接写. 写了两个简单的矩阵操作的函数简单示例. #include <stdio.h> #include <stdlib.h> const ...

  10. JavaScript- 获得TreeView CheckBox里选中项的值

    获得TreeView CheckBox里选中项的值,对JSDOM控制还不是很熟,感觉不太容易.试了很多次终于成功了. 代码如下 <body> <form id="form1 ...