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 ...
随机推荐
- 天气预报service
https://weather.com/ https://api.weather.com/v2/turbo/vt1dailyForecast?apiKey=c1ea9f47f6a88b9acb43ab ...
- 主成分分析(PCA)及其在R里的实现
主成分分析(principal component analysis,PCA)是一种降维技术,把多个变量化为能够反映原始变量大部分信息的少数几个主成分.设X有p个变量,为n*p阶矩阵,即n个样本的p维 ...
- OSI与TCP/IP各层的结构与功能,都有哪些协议
前言: 今天更新一下计算机网络的一些非常重要的知识,可能很多人都不知学计算机网络有什么用,我想说的是它真的比较重要,像咱们学校只要是学计算机这个专业都要学习这门课程.另外大家要是去一些像BAT,阿里, ...
- js中的return
retrun true: 返回正确的处理结果. return false:分会错误的处理结果,终止处理. return:把控制权返回给页面(如果条件满足,后面的逻辑就不执行了). if(this.in ...
- Python模块学习 - IPy
简介 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6地址的类与工 ...
- C++之容器
容器,迭代器与容器适配器 所谓容器,即是将最常运用的一些数据结构(data structures)用类模板实现出来,用于容纳特定类型的对象.根据数据在容器中排列的特性,容器可概分为序列式(sequen ...
- 内核抢占实现(preempt) 【转】
转自:http://blog.chinaunix.net/uid-12461657-id-3353217.html 一.什么叫抢占所谓抢占,说白了就是进程切换.linux的用户空间,进程A在执行中,来 ...
- ftrace 的使用【转】
转自:http://blog.csdn.net/wang6077160/article/details/7814279 ftrace 的使用 ftrace 在内核态工作,用户通过 debugfs 接口 ...
- 64_d2
dtc-1.4.4-2.fc26.x86_64.rpm 20-Jun-2017 11:04 89890 dtdinst-20131210-7.fc26.noarch.rpm 11-Feb-2017 0 ...
- Linux系统各发行版镜像下载(持续更新)
Linux系统各发行版镜像下载(持续更新) http://www.linuxidc.com/Linux/2007-09/7399.htm Linux系统各发行版镜像下载(2014年10月更新),如果直 ...