热门标签 | 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)



推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
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社区 版权所有