ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
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的情况,需要特判。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; int a[], b[], ans[]; void Input()
{
memset(a, , sizeof(a));
memset(b, , sizeof(b));
char ch;
for (;;)
{
ch = getchar();
if (ch == '\n')
break;
a[ch-'']++;
}
for (;;)
{
ch = getchar();
if (ch == '\n')
break;
b[ch-'']++;
}
} void Work()
{
int cnt = , maxOne = -, iOne, jOne;
for (int i = ; i < ; ++i)
{
if (a[i] == )
continue;
for (int j = ; j < ; ++j)
{
if (b[j] == )
continue;
if (maxOne < (i+j)%)
{
maxOne = (i+j)%;
iOne = i;
jOne = j;
}
}
}
if (maxOne != -)//第一位出现0加一个数的情况
{
ans[cnt] = maxOne;
cnt++;
a[iOne]--;
b[jOne]--;
} for (int k = ; k >= ; k--)
{
for (int i = k; i >= ; --i)
{
while (a[i] && b[k-i])
{
ans[cnt] = k;
cnt++;
a[i]--;
b[k-i]--;
}
}
for (int i = k; i < ; ++i)
{
while (a[i] && b[k+-i])
{
ans[cnt] = k;
cnt++;
a[i]--;
b[k+-i]--;
}
}
}
int i = ;
while (ans[i] == && i < cnt)//排除第一位出现0的情况
i++;
if (i == cnt)//所有位都是0的情况
{
printf("0\n");
return;
}
for (; i < cnt; ++i)
printf("%d", ans[i]);
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
getchar();
for (int times = ; times <= T; ++times)
{
printf("Case #%d: ", times);
Input();
Work();
}
return ;
}
ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)的更多相关文章
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
- HDU 4726 Kia's Calculation (贪心算法)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 4726 Kia's Calculation(贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...
随机推荐
- Struts2学习一----------Struts2的工作原理及HelloWorld简单实现
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...
- rtems 4.11 启动流程(arm, beagle)
请参照官方的 bsp_howto 文档,对arm来说,首先执行的文件是start.S start.S c/src/lib/libbsp/arm/shared/start/start.S 1.从 _st ...
- 奇妙的go语言(開始篇)
[ 声明:版权全部.欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 从前接触脚本语言不多,可是自从遇到go之后,就開始慢慢喜欢上了这个脚本语言.go语言是goog ...
- 数据结构:最小生成树--Kruskal算法
Kruskal算法 Kruskal算法 求解最小生成树的还有一种常见算法是Kruskal算法.它比Prim算法更直观.从直观上看,Kruskal算法的做法是:每次都从剩余边中选取权值最小的,当然,这条 ...
- 九度OJ 1058:反序输出 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...
- process_thread_action
import psycopg2 import threading conn_fmac = psycopg2.connect(database='filter_useless_mac', user='u ...
- slide.js
define(['jquery'], function (jquery) { function buildSmooth(config, motivateCallBack) { var timer = ...
- 性能测试--Jmeter随机生成/随机选取/csv读取关键字
Jmeter随机生成/随机选取/csv读取关键字 一.随机生成关键字 随机生成关键字,需要组件:随机变量配置元件(Random Variable) 该组件的作用是生成字符+随机数字格式的字符串,并保 ...
- VC里OnPaint几点要注意的地方(没有invalidate,系统认为窗口没有更新的必要,于是就对发来的WM_PAINT消息不理不睬)
写在属于自己的体会,哪怕只是一点点,也是真的懂了.否则有那么多书,如果只是不过脑子的学一遍看一遍,又有谁真的掌握了这些知识呢? 这样你或许就明白了为什么不能直接用SendMessage和PostMes ...