Description
Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 1234+9876, she will get 0. Ghee is angry about this, and makes a hard problem for her to solve:
Now Kia has two integers A and B, she can shuffle the digits in each number as she like, but leading zeros are not allowed. That is to say, for A = 11024, she can rearrange the number as 10124, or 41102, or many other, but 02411 is not allowed.
After she shuffles A and B, she will add them together, in her own way. And what will be the maximum possible sum of A "+" B ?
 
Input
The rst line has a number T (T <= 25) , indicating the number of test cases.
For each test case there are two lines. First line has the number A, and the second line has the number B.
Both A and B will have same number of digits, which is no larger than 10 6, and without leading zeros.
 
Output
For test case X, output "Case #X: " first, then output the maximum possible sum without leading zeros.
 
Sample Input
 1
5958
3036
 
Sample Output
 Case #1: 8984

题目要求是给定两个数,能加合成另一个数,要求这个数最大,然而第一位不能有0加合成。
然而题目给的AB两个数长度达到10^6(一开始这个条件理解错了,以为是AB上界是10^6,然后果断暴力超时了)
不过,虽然AB长度到达10^6,但是每一位毕竟是由0到9数字构成的,而且题目的加合运算是每位进行的。可以考虑统计0到9的个数然后进行贪心。
由于考虑到第一位不能有0加合,对第一位加合情况进行枚举求最大的。
然后就是对后面的位数进行贪心了:

对于不超过10的情况,自然是从9开始贪心,然后8、7、6……
而且由于对于加合成k的情况,每个能加合成k的对都是不同的,自然互不影响,所以对于每一个能加合成k的情况,就把所有的对全部用完,直到A组和B组中有一个减到0。

对于需要模10的情况。如果同样是模10加合成k,那么肯定先考虑模10得到k的,才会去考虑不模得到k-1的,所以考虑完不模的情况就马上考虑模的情况。

此外这题还有注意点,就是A和B中有一组只有0的情况或两组都是只有0的情况,需要特判。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int a[], b[], ans[];
  11.  
  12. void Input()
  13. {
  14. memset(a, , sizeof(a));
  15. memset(b, , sizeof(b));
  16. char ch;
  17. for (;;)
  18. {
  19. ch = getchar();
  20. if (ch == '\n')
  21. break;
  22. a[ch-'']++;
  23. }
  24. for (;;)
  25. {
  26. ch = getchar();
  27. if (ch == '\n')
  28. break;
  29. b[ch-'']++;
  30. }
  31. }
  32.  
  33. void Work()
  34. {
  35. int cnt = , maxOne = -, iOne, jOne;
  36. for (int i = ; i < ; ++i)
  37. {
  38. if (a[i] == )
  39. continue;
  40. for (int j = ; j < ; ++j)
  41. {
  42. if (b[j] == )
  43. continue;
  44. if (maxOne < (i+j)%)
  45. {
  46. maxOne = (i+j)%;
  47. iOne = i;
  48. jOne = j;
  49. }
  50. }
  51. }
  52. if (maxOne != -)//第一位出现0加一个数的情况
  53. {
  54. ans[cnt] = maxOne;
  55. cnt++;
  56. a[iOne]--;
  57. b[jOne]--;
  58. }
  59.  
  60. for (int k = ; k >= ; k--)
  61. {
  62. for (int i = k; i >= ; --i)
  63. {
  64. while (a[i] && b[k-i])
  65. {
  66. ans[cnt] = k;
  67. cnt++;
  68. a[i]--;
  69. b[k-i]--;
  70. }
  71. }
  72. for (int i = k; i < ; ++i)
  73. {
  74. while (a[i] && b[k+-i])
  75. {
  76. ans[cnt] = k;
  77. cnt++;
  78. a[i]--;
  79. b[k+-i]--;
  80. }
  81. }
  82. }
  83. int i = ;
  84. while (ans[i] == && i < cnt)//排除第一位出现0的情况
  85. i++;
  86. if (i == cnt)//所有位都是0的情况
  87. {
  88. printf("0\n");
  89. return;
  90. }
  91. for (; i < cnt; ++i)
  92. printf("%d", ans[i]);
  93. printf("\n");
  94. }
  95.  
  96. int main()
  97. {
  98. //freopen("test.in", "r", stdin);
  99. int T;
  100. scanf("%d", &T);
  101. getchar();
  102. for (int times = ; times <= T; ++times)
  103. {
  104. printf("Case #%d: ", times);
  105. Input();
  106. Work();
  107. }
  108. return ;
  109. }

ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)的更多相关文章

  1. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  2. HDU 4726 Kia's Calculation (贪心算法)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  3. HDU 4726 Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU 4726 Kia's Calculation(贪心构造)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...

  5. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  6. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  7. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  8. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  9. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

随机推荐

  1. Spring Cloud(十三):Spring Cloud Sleuth服务链路追踪(zipkin)(转)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...

  2. 开发ActiveX控件调用另一个ActiveX系列3——ActiveX调用另一个ActiveX

    终于进入正题了,怎样在ActiveX中调用另一个ActiveX.我们的项目需要调用华视电子身份证识别仪的ActiveX控件 在这里有很多识别仪ActiveX插件下载:http://www.idukaq ...

  3. php设计模式中的类型安全 指--只接受特定的对象 ---以避免发生错误

    在百度百科中---类型安全代码指访问被授权可以访问的内存位置

  4. HDFS源码分析数据块汇报之损坏数据块检测checkReplicaCorrupt()

    无论是第一次,还是之后的每次数据块汇报,名字名字节点都会对汇报上来的数据块进行检测,看看其是否为损坏的数据块.那么,损坏数据块是如何被检测的呢?本文,我们将研究下损坏数据块检测的checkReplic ...

  5. 1_Jsp标签_简单自定义

    一 简介 主要用于移除jsp页面中的java代码 编写一个实现Tag接口的Java类,为避免需要实现不必要的方法,只需继承TagSupport类, 把页面java代码移到这个标签处理类中, 然后编写标 ...

  6. java 给多人发送、抄送

    关键技术: 1.MimeMessage的setRecipients方法设置邮件的收件人,其中Message.RecipientType.TO常量表示收件人类型是邮件接收者,Message.Recipi ...

  7. angularcli填坑系列(持续更新...)

    1.在xx.ts中引入css样式无效 @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls ...

  8. apache vhosts 虚拟主机设置

    编辑vhosts文件:/alidata/server/httpd-2.4.10/conf/extra/httpd-vhosts.conf <VirtualHost *:80> <Lo ...

  9. 前端面试题(一)JS篇

    内置类型 JS 中分为七种内置类型,七种内置类型又分为两大类型:基本类型和对象(Object). 基本类型有六种: null,undefined,boolean,number,string,symbo ...

  10. 【题解】[P4178 Tree]

    [题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...