热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SDAU课程练习problemC

题目描述HereisafamousstoryinChinesehistory.“Thatwasabout2300yearsago.GeneralTianJiwasahighoffi

题目描述

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

Sample Output

200
0
0

题目大意

田忌与king赛马,在赛马过程中使用贪心。

思路

将总的问题分解为一个个小问题,每个小问题都选择最佳,最佳的选择一定是从田忌的最快最慢与king的最快最慢中选择对决马匹。分别记为tf,ts,kf,ks。分三种情况:


  1. tf>kf 此时选择tf与kf打是最优的。因为tf反正都要赢,最好去赢对方最快的

  2. tf
  3. tf=kf
    1. ts>ks 此时选择ts与ks打。因为ks反正要输,让我方最慢的和它打为最优选择

    2. ts
    3. ts=ks
      1. ts
      2. ts=kf 游戏结束 game over

      3. ts>kf 不可能情况

在做题过程中出现了一个插曲,我写的代码怎么也过不了。我在一个博客中找到的代码和我思路差不多(比较最慢的)
用他的代码一边就A了。真是见了鬼了
下面贴出两个代码

AC代码

  1. #include
  2. #include
  3. using namespace std;
  4. int main(){
  5. int n, i, j;
  6. int a[1000];
  7. int b[1000];
  8. while(scanf("%d", &n) && n){
  9. for(i = 0; i < n; ++i){
  10. scanf("%d", &a[i]);
  11. }
  12. for(i = 0; i < n; ++i){
  13. scanf("%d", &b[i]);
  14. }
  15. sort(a, a + n);
  16. sort(b, b + n);
  17. int c = 0, d = 0, a = n - 1, b = n - 1, ans = 0;
  18. for(i = 0; i < n; ++i){
  19. if(a[c] > b[d]){
  20. ++c;
  21. ++d;
  22. ++ans;
  23. }else if(a[c] < b[d]){
  24. ++c;
  25. --b;
  26. --ans;
  27. }else if(a[a] > b[b]){
  28. --a;
  29. --b;
  30. ++ans;
  31. }else if(a[a] < b[b]){
  32. ++c;
  33. --b;
  34. --ans;
  35. }else if(a[c] < b[b]){
  36. ++c;
  37. --b;
  38. --ans;
  39. }else{
  40. break;
  41. }
  42. }
  43. printf("%d\n", ans * 200);
  44. }
  45. return 0;
  46. }

我的代码

  1. #include
  2. #include
  3. using namespace std;
  4. bool cmp(int &e, int &f)
  5. {
  6. return e > f;
  7. }
  8. int main()
  9. {
  10. //freopen("date.in", "r", stdin);
  11. //freopen("date.out", "w", stdout);
  12. int *a ,*b,*c, *d;//a c为田最大最小,b,d为王最大最小
  13. int t[1000], w[1000];
  14. int jie=0,N;
  15. while (cin>>N&&N)
  16. {
  17. jie = 0;
  18. for (int i = 0; i < N; i++)
  19. cin >> t[i];
  20. for (int i = 0; i < N; i++)
  21. cin >> w[i];
  22. sort(t, t + N,cmp);
  23. sort(w, w + N,cmp);
  24. a = t;
  25. c = t+N-1;
  26. b= w;
  27. d= w+N - 1;
  28. for (int i = 0; i<N;i++)
  29. {
  30. if (*a > *b)
  31. {
  32. jie++;
  33. a++;
  34. b++;
  35. }
  36. else
  37. {
  38. if (*a < *b)
  39. {
  40. jie--;
  41. c--;
  42. b++;
  43. }
  44. else
  45. if (*c < *d)
  46. {
  47. jie--;
  48. c--;
  49. b++;
  50. }
  51. else
  52. if (*c > *d)
  53. {
  54. jie++;
  55. c--;
  56. d--;
  57. }
  58. else
  59. if (*c < *b)
  60. {
  61. jie--;
  62. c--;
  63. b++;
  64. }
  65. else
  66. {
  67. jie = 0;
  68. goto shan;
  69. }
  70. }
  71. }
  72. shan:cout << jie * 200 << endl;
  73. }
  74. }

真是见鬼了,这两段代码耗费了我一个星期六。。
先贴在这里,过两天再研究,先往下刷题吧!



来自为知笔记(Wiz)



推荐阅读
  • hadoop1.2.1文档中这样写:Nowcheckthatyoucansshtothelocalhostwithoutapassphrase:$sshlocalhostIfyou ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • PatchODAX8: ... [详细]
  • 原文地址http://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/最开始时 ... [详细]
author-avatar
ssl87
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有