poj 2287(贪心)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 12490 | Accepted: 3858 |
Description
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
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
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
#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(贪心)的更多相关文章
- POJ 2287 田忌赛马 贪心算法
田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...
- POJ 2287 Tian Ji -- The Horse Racing(贪心)
题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...
- (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
/* * POJ_2287.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...
- POJ - 1017 贪心训练
Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59725 Accepted: 20273 Descrip ...
- POJ 2376 贪心
题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...
- poj 1328 贪心
/* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...
- poj 2287 动态规划
用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...
- Yogurt factory(POJ 2393 贪心 or DP)
Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8205 Accepted: 4197 De ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
随机推荐
- Informatica _组件使用介绍及优化
转载 http://blog.csdn.net/yongjian1092/article/details/52588434 有空自己会写一个关于这方面的文章.
- python初步学习-python控制流
语句书写规范 缩进在python语言书写中非常重要,如果缩进不规范,执行程序将会报错 引用维基百科中的叙述: Python開發者有意讓違反了縮排規則的程序不能通過編譯,以此來強迫程序員養成良好的編程習 ...
- css3动画总结
- 网页实现插入图片—css与html的区别
Q1.二者有何区别?A1.写在css里面的图片是以背景图形式存在的,而写在html里的是以<img>标签形式存在的,在网页加载的过程中,以css背景图存在的图片会等到结构加载完成(网页的内 ...
- 自定义ToolBar
一.Toolbar的简介 Toolbar 是 android 5.0 引入的一个新控件,Toolbar出现之前,我们很多时候都是使用ActionBar以及ActionActivity实现顶部导航栏的, ...
- Join vs merge vs lookup
The obvious benefit of merge over join is the ability to add reject links. I can't upload pictures. ...
- Perl6 Bailador框架(3):路径匹配
use v6; use Bailador; =begin pod 注意的是, 当/:one设置时 虽然你有/admin或/about, 但这个/:one不会跟现有的匹配 只跟没有的匹配: 也就是说, ...
- 自动化测试===Httprunner测试框架介绍
项目地址: https://github.com/HttpRunner/HttpRunner 中文手册: http://cn.httprunner.org/ 首先是环境搭建: pip install ...
- Oracle 合并 merger into
merge into copy_emp1 c using employees e on (c.employee_id=e.employee_id)when matched then update ...
- Log4Net中配置文件的解释
一个完整的配置文件的例子如下所示 <log4net> <!-- 错误日志类--> <logger name="logerror"> <le ...