作为一名大三的学生现在开始学习acm,或许太晚。感叹时光蹉跎。。。。我的blog将以讲解的形式的发布,以专题的形式的形式介绍一些基本的知识和经典的题目。虽然感觉自己所剩时间无多,但也希望起到前人种树的作用,让我们一起加油!        接下来让我们以一道古老而又有趣的题目点燃我们对acm的热情吧。(此处向孙膑献上orz)

 xdoj1240:http://acm.xidian.edu.cn/problem.php?id=1240

1240: Godv与女朋友赛马

时间限制: 2 Sec  内存限制: 128 MB
提交: 13  解决: 8
[提交][状态][讨论版]

题目描述

到七夕啦!在这样的日子里,Godv当然要去陪女朋友玩耍啦~~~(不要问我敬老师的手环送没送出去!)于是,资产阶级的Godv选择了带女朋友去玩赛马~~~

话说,两个人各有n匹马,每匹马各有各的速度,两个人一共比n场,且每匹马恰参加一次比赛。两个人事先约定好,对于每场比赛,获胜的得一分,平局或失败不得分,最后输掉比赛的要请对方晚上看电影。

Godv这样既聪明又萌萌哒又体贴女朋友的人,当然是不想让女朋友输啦。于是他事先要对比赛做了手脚,这样每一场比赛的参赛的马儿都是被Godv钦定好了的。

下面,你能帮Godv算一算,Godv最多能让女朋友赢多少分吗?

输入

多组数据,数据组数不超过10组,请处理到文件结束。每组数据第一行包含一个数n(1<=n<=1e5),第二行包含n个正整数 ai (1<=ai<=1e9),表示Godv的第i匹马的速度,第三行包含n个正整数 bi (1<=bi<=1e9),表示Godv的女朋友的第i匹马的速度。

输出

对于每组数据都输出一行。如果Godv的女朋友只能得到0分,请输出”Godv too strong”,否则,输出Godv的女朋友最多能得到的分数。

样例输入

  1. 3
  2. 1 2 3
  3. 1 2 3
  4. 1
  5. 2
  6. 1

样例输出

  1. 2
  2. Godv too strong

题目分析:两个人各n匹马相互比赛,赢了得一分,输了平了不扣分。这里我们采取贪心策略,先将马的速度进行排序,若女朋友最快的马大于glory最快的马,则与它相配。因为女朋友最快的的马反正都要得分,那我就用最快的消耗对方最大的,否则不是亏了吗(形象一点,老大很厉害,但他只能上场一次,他就挑了对方老大,剩余的留给小弟)。如果女朋友的最快马小于glory最快的马,相当于女朋友所有的马跟它比都赢不了,那我就用我最慢的马跟你比。 这样就能保证得到最大分了。献上代码。

    

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int N=1e5+;
  5. long long x[N];
  6. long long y[N];
  7. int n;
  8. bool mycmp (int a,int b) {
  9. return (a>b);
  10. }
  11. int main ()
  12. {
  13. while (scanf("%d",&n)!=EOF) {
  14. for (int i=;i<=n;i++)
  15. scanf("%lld",&x[i]);
  16. for (int i=;i<=n;i++)
  17. scanf("%lld",&y[i]);
  18. sort(x+,x++n,mycmp);
  19. sort(y+,y++n,mycmp);
  20. int l1,l2;
  21. int h1,h2;
  22. h1=h2=;
  23. l1=l2=n;
  24. int ans=;
  25. while (n--) {
  26. if (y[l2]>x[l1]) {
  27. ans++;
  28. l1--;
  29. l2--;
  30. }
  31. else {
  32. l2--;
  33. h1++;
  34. }
  35. }
  36. if (ans==) printf("Godv too strong\n");
  37. else printf("%d\n",ans);
  38. }
  39. return ;
  40. }

这只是小试牛刀,让我们看看hdoj 1052:http://acm.split.hdu.edu.cn/showproblem.php?pid=1052

Tian Ji -- The Horse Racing

                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                         Total Submission(s): 31709    Accepted Submission(s): 9624

Problem 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
 
喜欢中文的同学,这里来。题目相似,只不过这里赢了得一分,平了不得分,输了扣一分。
可是这个扣一分,将问题又上升一个档次。
首先若我的最快马大于你最快的马,我就用我最快的马消耗你最快的马,这一点没问题。相同如果赢不了对方最快的马我就用我最慢的消耗它行吗,这个是不行的。因为我们要尽量保持失败扣分。前一个是不扣分所以没关系。举个简单的例子 2匹马(5 4) (5 3)采用一种策略 (4-5 5-3)不得分 ,实际上我们可以得到(4-3,5-5)一分。只是我们的策略是尽可能多的赢,尽可能的平,实在不行就输。策略如下:
1 若我最快的马大于对方最快的马,则消耗它。
2 1不成立下,若我最慢的马大于你最慢的马,所以我的所有马都能赢对方最慢的马,那我就用我 最慢的马消耗你。
3  1不成立下,若我最慢的马小于你最慢的马,反正我这匹马都要输就输给你最快的那一匹;
4   最难的,1不成立下,两匹最慢的马速度相同 ,分两种情况,
如对方最快的马大于我最快的马,反正都输我用我最慢的马消耗你最快的马
若两匹快马也相同, (6 43)(6 5 3)若我最慢的马采用平局(3-3)(4-6)(6-5)得分只能是0;
若用最慢的消耗对方最快的(3-6) (4-3) (6-5) 可以得一分;
意思就是说我最慢的小于你最快的,同样你之中肯定有一个小于我最快的,也许是比最慢的快一点,这样一胜一平,但结果是我吧对方 最慢的留了下来。这样更好嘛(也许有些混乱,我太菜了,欢迎交流qq-821474143)。
献上代码
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int N=;
  5. int a[N];
  6. int b[N];
  7. int n;
  8. int main ()
  9. {
  10. while (~scanf("%d",&n)&&n!=) {
  11. for (int i=;i<=n;i++)
  12. scanf ("%d",&a[i]);
  13. for (int i=;i<=n;i++)
  14. scanf ("%d",&b[i]);
  15. sort (a+,a++n);
  16. sort (b+,b++n);
  17. int ans=;
  18. int h1,l1;
  19. int h2,l2;
  20. h2=h1=n;
  21. l2=l1=;
  22. while (n--) {
  23. if (a[h2]>b[h1]) {
  24. ans++;
  25. h2--;
  26. h1--;
  27. }
  28. else if(a[l2]>b[l1]) {
  29. ans++;
  30. l2++;
  31. l1++;
  32. }
  33. else {
  34. if (a[l2]<b[h1]) ans--;
  35. l2++;
  36. h1--;
  37. }
  38. }
  39. printf("%d\n",ans*);
  40. }
  41. return ;
  42. }

小时候以为田忌赛马是一件太过简单的东西,此处向孙膑再次献上我的orz...

下期专题递归,欢迎留言哦!!!!!!!!!!!!!!!

我的第一篇blog—— 一起来赛马呀的更多相关文章

  1. 空格哥的第一篇Blog

    首先十分感谢博客园在这里给我的平台,我在这里学习到了很多东西,响应的,我也想要在这里记录下自己的心路历程!在学习的过程中,希望博客园一直陪伴我,小弟在这里不胜感激!这是小弟的第一篇博客,很多东西都不是 ...

  2. 人生的第一篇blog

    开始写博客了,人生第一篇博客啊,要写些什么呢?想想也没有什么头绪,随便写写吧. 这学期要使用代码管理工具了,要写团队项目了.一直以来都是自己一个人在默默编程,没有过合作经历.对于代码的管理也只是一直在 ...

  3. 第一篇blog

    之前不用blog,但是在杭电oj,poj上刷题,总会自己总结题型和使用什么算法,算法模板,自己在笔记本上写,耗时费力,感觉用键盘敲得总结,分享,大家相互学习提高.有时遇到不会做的,或者总是在oj上跑的 ...

  4. 我的第一篇blog

    加入博客园两年多了,学习.从事编程也两年多了,一直是在网上找资料,都没有认真写写博客. 博客园里面好多功能都还不会用,今天起我也要在博客园写自己的blog了.感觉很高大上的样纸!!

  5. 记录从现在开始,我的第一篇blog

    最近在读刘未鹏的<暗时间>,深受作者的启发,决定开始书写blog. 书写是为了更好的思考,希望自己能持之以恒的坚持做这件事情. 这本书很推荐给所有同学,不仅关于时间管理,执行力,心理学, ...

  6. Hello World!(这不是第一篇)

    如题,这不是第一篇blog,但是为了表示这个闲置了1年多的blog现在被我正式启用了,我还是走个过场吧. #include <iostream> using namespace std; ...

  7. “Hello, my first blog”------第一篇博客的仪式感

    本人在校大学生一枚,开通博客,主要是想记录自己的学习过程,分享自己的学习经历.记得大一的时候,很多不懂的操作和知识,都是在博客上找到了相应的解决办法.但比较讽刺的是,很多时候,曾经解决了的问题,当再次 ...

  8. 我的第一篇博客blog,笑哭

    我的第一篇博客blog Markdown学习 一级标题:#加一个空格 加 文字, 二级标题:加2个##以此类推 字体 粗体:hello world!字体前有二个星号,字体后有二个星号 斜体:hello ...

  9. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

随机推荐

  1. Wireshark使用drcom_2011.lua插件协助分析drcom协议

    drcom_2011.lua是来源于Google code上的一个开源项目中的一个插件,感谢网络大神的分享 需要使用drcom_2011.lua分析drcom协议的话,需要把drcom_2011.lu ...

  2. HAproxy功能配置

    author:JevonWei 版权声明:原创作品 haproxy配置文档 https://cbonte.github.io/haproxy-dconv/ 环境 前端HAProxy 172.16.25 ...

  3. 富文本编辑器UEditor的配置使用方法

    将下载的富文本编辑器的文件解压后放到 webcontent 下 如果 文件中的jsp文件夹下的controller.java文件报错的话    就将jsp下的lib文件夹中的文件都复制到  web-i ...

  4. 【DDD】领域驱动设计实践 —— 框架实现

    本文主要了在社区服务系统(ECO)中基于SpringMVC+mybatis框架对DDD的落地实现.本文为系列文章中的其中一篇,其他内容可参考:通过业务系统的重构实践DDD. 框架实现图 该框架实现基本 ...

  5. setTimeout和setInterval不容易注意到的一些细节

    今天没事翻了翻JS高程,看到了setTimeout部分有这么一句话:调用setTimeout()之后,该方法会返回一个数值ID,表示超时调用.这个超时调用ID是计划执行代码的唯一标识符,可以通过它来取 ...

  6. jdbc预编译

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp20 JAVA_JDBC预编译 相关知识点 什么是预编译语句? 预编译语句P ...

  7. 第1阶段——u-boot分析之make指令(2)

    通过make 100ask24x0_config 指令配置好芯片选型后,使用make指令来生成uboot.bin文件 本文学习目标: 对Makefile文件进行基本了解,掌握make指令是怎么实现生成 ...

  8. poj 1948二维01背包

    题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积. 思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600  ...

  9. hibernate 教程(3)—NHibernate查询语言HQL

    NHibernate之旅(3):探索查询之NHibernate查询语言(HQL) 本节内容 NHibernate中的查询方法 NHibernate查询语言(HQL) 1.from子句 2.select ...

  10. Eclipse rap 富客户端开发总结(5): RAP国际化之路

    Eclipse RCP 中的plugin.xml国际化实现 1.  在工程的根目录下面建立一个plugin.properties资源文件:在此资源文件中写入需要国际化的内容(键/值对),举例如下: h ...