Tian Ji -- The Horse Racing
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12490   Accepted: 3858

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

Source

 
思路:现将两人马按速递增排列;
1)若田忌最慢的马快于齐王的最慢的马(a1>b1),将a1与b1比,因为齐王最慢的马b1一定输,输给田忌最慢的马最好;
2)若田忌最慢的马慢于齐王的最慢的马(a1<b1),将a1与bn比,因为田忌最慢的马a1一定输,输给齐王最快的马最好;
3)若田忌最快的马快于齐王的最快的马(an>bn),将an与bn比,因为田忌最快的马an一定赢,赢齐王最快的马最好;
4)若田忌最快的马慢于齐王的最快的马(an<bn),将a1与bn比,因为齐王最快的马bn一定赢,赢田忌最慢的马最好;
5)当田忌最慢的马与齐王最慢的马相等(a1=b1),且田忌最快的马比齐王最快的马快时(an>bn),将an与bn比;相反,若(an<bn),则让a1与bn比;
6)田忌最快的马与齐王最快的马相等时(an==bn),将a1与bn比有最优解;
 
 
过程:田忌第一步的贪心选择是派出最快的或最慢的马与齐王的最慢的马比,得出的第一个子问题的最优解;第二步的贪心选择是在剩下的n-1匹马中派出最快的马或者最慢的马与齐王次慢的马比,
得出第二个子问题的最优解……每次贪心选择都将当前问题归纳为更小的相似问题,而每个贪心选择都仅做一次,所有子问题的最优解构成整个问题的最优解。
 
在poj上AC了,但在zoj上WA了。。。。有毒;
这道题还有种dp的解法稍后补上;
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.141592653589792128462643383279502
int main(){
//#ifdef CDZSC_June
//freopen("in.txt","r",stdin);
//#endif
//std::ios::sync_with_stdio(false);
int n;
int tian[],qi[];
while(scanf("%d",&n),n){
for(int i=;i<=n;i++)
cin>>tian[i];
for(int i=;i<=n;i++)
cin>>qi[i];
sort(tian+,tian+n+);
sort(qi+,qi+n+);
int t1=,tn=n,q1=,qn=n;
int sum=; while(t1<=tn){
if(tian[t1]>qi[q1]){
t1++;q1++;sum+=;
}
else if(tian[t1]==qi[q1]){
while(t1<=tn&&q1<=qn){
if(tian[tn]>qi[qn]){
tn--;qn--;sum+=;
}
else {
if(tian[t1]<qi[qn]) sum-=;
t1++;qn--;break;
}
}
}
else{
t1++;qn--;sum-=;
}
}
cout<<sum<<endl;
}
return ;
}
 

poj 2287(贪心)的更多相关文章

  1. POJ 2287 田忌赛马 贪心算法

    田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...

  2. POJ 2287 Tian Ji -- The Horse Racing(贪心)

    题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...

  3. (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing

    /* * POJ_2287.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...

  4. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  5. POJ 2376 贪心

    题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...

  6. poj 1328 贪心

    /* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...

  7. poj 2287 动态规划

    用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...

  8. Yogurt factory(POJ 2393 贪心 or DP)

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 De ...

  9. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

随机推荐

  1. Informatica _组件使用介绍及优化

    转载 http://blog.csdn.net/yongjian1092/article/details/52588434 有空自己会写一个关于这方面的文章.

  2. python初步学习-python控制流

    语句书写规范 缩进在python语言书写中非常重要,如果缩进不规范,执行程序将会报错 引用维基百科中的叙述: Python開發者有意讓違反了縮排規則的程序不能通過編譯,以此來強迫程序員養成良好的編程習 ...

  3. css3动画总结

  4. 网页实现插入图片—css与html的区别

    Q1.二者有何区别?A1.写在css里面的图片是以背景图形式存在的,而写在html里的是以<img>标签形式存在的,在网页加载的过程中,以css背景图存在的图片会等到结构加载完成(网页的内 ...

  5. 自定义ToolBar

    一.Toolbar的简介 Toolbar 是 android 5.0 引入的一个新控件,Toolbar出现之前,我们很多时候都是使用ActionBar以及ActionActivity实现顶部导航栏的, ...

  6. Join vs merge vs lookup

    The obvious benefit of merge over join is the ability to add reject links. I can't upload pictures. ...

  7. Perl6 Bailador框架(3):路径匹配

    use v6; use Bailador; =begin pod 注意的是, 当/:one设置时 虽然你有/admin或/about, 但这个/:one不会跟现有的匹配 只跟没有的匹配: 也就是说, ...

  8. 自动化测试===Httprunner测试框架介绍

    项目地址: https://github.com/HttpRunner/HttpRunner 中文手册: http://cn.httprunner.org/ 首先是环境搭建: pip install ...

  9. Oracle 合并 merger into

    merge into copy_emp1 c  using employees e  on (c.employee_id=e.employee_id)when matched then  update ...

  10. Log4Net中配置文件的解释

    一个完整的配置文件的例子如下所示 <log4net> <!-- 错误日志类--> <logger name="logerror"> <le ...