Problem C
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?"
C" title="Problem C">
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.
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.
containing a single number, which is the maximum money Tian Ji will
get, in silver dollars.
71
74
0
46MS)
#include
#include
#include
#define maxn 1005
using namespace std;
struct horses
{
int
speed;
char
s;
};
bool comp(horses &a,horses&b)
{
if(a.speed!=b.speed)
return a.speed>b.speed;
else
return a.speed>b.speed;
}
int main()
{
//freopen("in.txt", "r", stdin);
int
n,money=0,tb,te,gb,ge;
horses
ti[maxn],god[maxn];
while(~scanf("%d",&n)&&n)
{
money=0;
tb=gb=0;
te=ge=n-1;
for(int i=0;i
scanf("%d",&ti[i].speed);
for(int i=0;i
scanf("%d",&god[i].speed);
sort(&ti[0],&ti[n],comp);
sort(&god[0],&god[n],comp);
while (tb <= te)
{
if (ti[te].speed > god[ge].speed)//田忌最快的马比大王的快就赢一局
{
money++;
te--;
ge--;
}
else if (ti[te].speed < god[ge].speed)//田忌最快的马比大王的慢就输一局
{
money--;
te--;
gb++;
}
else//最快的马一样快
{
if(ti[tb].speed > god[gb].speed)//田忌最慢的马比大王的快就赢一局
{
money++;
tb++;
gb++;
}
else
{
if (ti[te].speed < god[gb].speed)//田忌的慢就输
money--;
te--;
gb++;
}
}
//注意:这些最快最慢都是相对于当前还没有进行过比赛的马,每比一次状态转移
//一次
}
printf("%d\n",money*200);
}
return
0;
}
Problem C的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- 在github上实现页面托管预览功能
1.建立个人github pages 仓库 创建新仓库,命名规则为----"你的github账号.github.io", 如图所示: 我的账号是zxpsuper,所以我的个人域名仓 ...
- 【运维】CPU负载
最近对我的本本(4核8线程)用top命令看系统状况出现了CPU利用率超过200%的情况,非常诧异,查了下相关资料,把这个问题弄清楚了.首先来分析下CPU Load load average: 0.09 ...
- 支持向量机SVM(一)
[转载请注明出处]http://www.cnblogs.com/jerrylead 1 简介 支持向量机基本上是最好的有监督学习算法了.最开始接触SVM是去年暑假的时候,老师要求交<统计学习理论 ...
- Python自学笔记-logging模块详解
简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...
- Python系列之反射、面向对象
一.反射 说反射之前先介绍一下__import__方法,这个和import导入模块的另一种方式 1. import commons 2. __import__('commons') 如果是多层导入: ...
- WPF第三方控件Telerik
帮助文档:http://www.telerik.com/help/wpf/gridview-selection-via-checkbox.html Telerik专注于微软.Net平台的表示层与内容管 ...
- 邮件实现详解(四)------JavaMail 发送(带图片和附件)和接收邮件
好了,进入这个系列教程最主要的步骤了,前面邮件的理论知识我们都了解了,那么这篇博客我们将用代码完成邮件的发送.这在实际项目中应用的非常广泛,比如注册需要发送邮件进行账号激活,再比如OA项目中利用邮件进 ...
- web前端上传图片的几种方法
1.表单上传 最传统的图片上传方式是form表单上传,使用form表单的input[type=”file”]控件,打开系统的文件选择对话框,从而达到选择文件并上传的目的. form表单上传 表单上传需 ...
- NopCommerce 1. NopCommerce Application_Start启动过程
这里简单介绍整个启动过程,其他具体的后续讲解 从Application_Start中执行开始,一开始执行EngineContext.Initialize(false); EngineContext 是 ...
- 1、Http概述
1.1 Web客户端和服务器 HTTP 客户端和 HTTP 服务器共同构成了万维网的基本组件,客户端向服务器发送 HTTP 请求, 服务器会在 HTTP 响应中回送所请求的数据. 示意图: 1.2 媒 ...