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. Oracle 导出空表的新方法(彻底解决)

    背景 使用Exp命令在oracle 11g 以后不导出空表(rowcount=0),是最近在工作中遇到一个很坑的问题,甚至已经被坑了不止一次,所以这次痛定思痛,准备把这个问题彻底解决.之所以叫新方法, ...

  2. py_faster_rcnn识别出来的结果好多红框重叠

    py_faster_rcnn识别出来的结果好多红框重叠, 可以通过调节demo.py中的NMS_THRESH的值进行限制. NMS_THRESH表示非极大值抑制,这个值越小表示要求的红框重叠度越小,0 ...

  3. 「6月雅礼集训 2017 Day7」电报

    [题目大意] 有n个岛屿,第i个岛屿有有向发射站到第$p_i$个岛屿,改变到任意其他岛屿需要花费$c_i$的代价,求使得所有岛屿直接或间接联通的最小代价. $1 \leq n \leq 10^5, 1 ...

  4. idea编写的java代码,在cmd运行乱码解决方案

    1.解决方案 使用txt打开,另存为的时候选择编码为ANSI 即可.

  5. 【Python学习】解决pandas中打印DataFrame行列显示不全的问题

    在使用pandas的DataFrame打印时,如果表太长或者太宽会自动只给前后一些行列,但有时候因为一些需要,可能想看到所有的行列. 所以只需要加一下的代码就行了. #显示所有列 pd.set_opt ...

  6. python基础===成员访问__len__()和__getitem__()

    class A: def __init__(self,*args): self.name = arg pass def __len__(self): return len(self.name) a = ...

  7. java===java习题---Josephu问题

    package testbotoo; /** * * @author */ public class Demo4 { public static void main(String[] args) { ...

  8. centos_7.1.1503_src_2

    farstream02-0.2.3-3.el7.src.rpm 05-Jul-2014 12:59 1.2M   fcoe-utils-1.0.29-9.el7.src.rpm 31-Mar-2015 ...

  9. gnu app url[web][5星]

    http://www.gnu.org/software/software.zh-cn.html http://linux.chinaunix.net/news/2010/12/07/1175310.s ...

  10. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...