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. u3d局域网游戏网络(c# socket select 模型)

    之前写了一篇. 发完之后第二天实际应用到游戏之后还是发现了一些小毛病. 比如网络模块有重复使用(多对象)的情况.所以将静态类该成了普通类. 比如安卓下会有些异常出现导致游戏逻辑不正常.所以网络相关的函 ...

  2. 【POJ】1830 开关问题(高斯消元)

    http://poj.org/problem?id=1830 高斯消元无解的条件:当存在非法的左式=0而右式不等于0的情况,即为非法.这个可以在消元后,对没有使用过的方程验证是否右式不等于0(此时因为 ...

  3. 【Luogu】P3930 SAC E#1 - 一道大水题 Knight

    [题目]洛谷10月月赛R1 提高组 [题意]给定n*n棋盘和<=16个棋子,给几个棋子种类和攻击范围,现我方只有一马,求能否吃王. [算法]状压+BFS [题解]16种棋子中,马不能吃马,直接处 ...

  4. Vuejs - 强大的指令系统

    在 Vuejs 中,指令(Directives)是带有 v- 前缀的特殊属性.指令属性的预期值是 单个 Javascript 表达式(v-for 是个例外).指令的职责是,当表达式改变时,将其产生的连 ...

  5. Spring 框架的设计理念与设计模式分析(山东数漫江湖)

    Spring 的骨骼架构 Spring 总共有十几个组件,但是真正核心的组件只有几个,下面是 Spring 框架的总体架构图: 图 1 .Spring 框架的总体架构图 从上图中可以看出 Spring ...

  6. bzoj 1197 DP

    我们可以将这个问题转化为在n维空间中一共放m个n维球,求这m个球最多将这个空间分为不同的几个部分. 那么我们设w[i][j]代表i为空间放j个球分为的部分,那么w[i][j]=w[i][j-1]+w[ ...

  7. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  8. 使用yo -v查看yeoman版本号

    使用yo -v无法查看yeoman版本,这是旧版本的方法 新版本使用yo --version即可查看

  9. IBM InfoSphere DataStage and QualityStage

    Info coms from https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_9.1.0/com.ibm.swg.im.iis.ds.nav ...

  10. VPS性能测试(1):CPU物理个数、内核、超线程、多核心

    1.登录VPS界面,执行:cat /proc/cpuinfo,就会显示出VPS主机的CPU详细参数,如内核.频率.型号等等 2.主要参数physical_id表示物理CPU个数,cpu cores是内 ...