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 ...
随机推荐
- MySQL数据库设计基础
为什么需要规范的数据库设计? 什么是数据库设计? 数据库设计就是将数据库中的数据实体及这些数据实体之间的关系,进行规划和结构化的过程. 数据库设计非常重要! 数据库中创建的数据结构的种类,以及在数据实 ...
- hibernate学习手记(1)
1. java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more ...
- bzoj4557【JLOI2016】侦查守卫
这道题对于我来说并不是特别简单,还可以. 更新一下blog 树形DP f[i][j]表示i的子树中,最高覆盖到i向下第j层的最小花费. g[i][j]表示i的子树全部覆盖,还能向上覆盖j层的最小花费. ...
- Google Authenticator 如何集成(U盾的实现原理相同)
Google Authenticator是一个类似U盾的二次验证工具,Google提供了它的开源客户端(https://github.com/google/google-authenticator)里 ...
- Springmvc+Spring+Mybatis实现员工登录注册功能
ssm实现用户注册以及登录功能..转载请标明出处 http://www.cnblogs.com/smfx1314/p/smfx1314.html 前端bootstrap 所使用的IDE是eclips ...
- Jquery添加和全部删除
<html> <head> <meta charset="utf-8" /> <title></title> <s ...
- 搭建Hadoop平台(新手入门)
刚刚大学毕业,接触大数据有一年的时间了,把自己的一些学习笔记分享给大家,希望同热爱大数据的伙伴们一起学习,成长! 资料准备: Hadoop-2.7.1下载:http://pan.baidu.com/s ...
- UEFI启动视频详解:启动分析+N项操作实例
============================================================= ※※※※最给力的视频解说※※※※ 2011hiboy全部共享资料:立刻去 ...
- Javascript常见浏览器兼容问题
常见浏览器原生javascript兼容性问题主要分为以下几类: 一.Dom 1.获取HTML元素,兼容所有浏览器方法:document.getElementById("id")以I ...
- JS中replace()用法举例
语法: string.replace(regexp,replacement) 参数: regexp:声明了要替换的模式的RegExp对象.如果该参数是一个字符串,则将它作为要检索的直接量文本模式,而不 ...