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[15], b[15], ans[1000010];

void Input()
{
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    char ch;
    for (;;)
    {
        ch = getchar();
        if (ch == ‘\n‘)
            break;
        a[ch-‘0‘]++;
    }
    for (;;)
    {
        ch = getchar();
        if (ch == ‘\n‘)
            break;
        b[ch-‘0‘]++;
    }
}

void Work()
{
    int cnt = 0, maxOne = -1, iOne, jOne;
    for (int i = 1; i < 10; ++i)
    {
        if (a[i] == 0)
            continue;
        for (int j = 1; j < 10; ++j)
        {
            if (b[j] == 0)
                continue;
            if (maxOne < (i+j)%10)
            {
                maxOne = (i+j)%10;
                iOne = i;
                jOne = j;
            }
        }
    }
    if (maxOne != -1)//第一位出现0加一个数的情况
    {
        ans[cnt] = maxOne;
        cnt++;
        a[iOne]--;
        b[jOne]--;
    }

    for (int k = 9; k >= 0; k--)
    {
        for (int i = k; i >= 0; --i)
        {
            while (a[i] && b[k-i])
            {
                ans[cnt] = k;
                cnt++;
                a[i]--;
                b[k-i]--;
            }
        }
        for (int i = k; i < 10; ++i)
        {
            while (a[i] && b[k+10-i])
            {
                ans[cnt] = k;
                cnt++;
                a[i]--;
                b[k+10-i]--;
            }
        }
    }
    int i = 0;
    while (ans[i] == 0 && 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 = 1; times <= T; ++times)
    {
        printf("Case #%d: ", times);
        Input();
        Work();
    }
    return 0;
}

ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

时间: 2024-10-11 16:56:16

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

hdu 4726 Kia&#39;s Calculation(贪心)

题目链接:hdu 4726 Kia's Calculation 题目大意:给出两个数,然后两个数进行没有进位的加法,加数的各个位的数可以重新调整位置,但是不能有前导0的情况,要求加完之后的结果最大. 解题思路:从9开始配,直到0,但是因为9可能可以用0和9相加获得,所以一开始输出一个数,后面就可以统一操作. 0 9 9 55 55 0 #include <cstdio> #include <cstring> #include <algorithm> using name

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

Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows(“?” denotes exclus

HDU 4726 Kia&#39;s Calculation (贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 思路:贪心,尽量先组大的数字,注意考虑前导零的情况 代码: #include <stdio.h> #include <string.h> const int N = 1000005; int t, v1[10], v2[10], ans[N]; char s1[N], s2[N]; void solve() { int n = strlen(s1); if (n == 1) {

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x1)+f(x2)+...+f(xn)的最大值. 首先由于是树,所以有n-1条边,然后每条边连接两个节点,所以总的度数应该为2(n-1). 此外每个结点至少应该有一个度. 所以r1+r2+...rn = 2n-2.ri >= 1; 首先想到让ri >= 1这个条件消失: 令xi = ri,则x1+x

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. 此外题目中说大数据只有10组,小数据最多n只有100.(那么c*n^2的复杂度应该差不多) 于是可以考虑枚举i和j,然后匹配k. 于是可以先把所有s[k]全部存进一个字典树, 然后枚举s[i]和s[j],由于i.j.k互不相等,于是先从字典树里面删掉s[i]和s[j],然后对s[i]+s[j]这个

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的值都求出来,对于这个规模的n是不可行的. 然后之前有过类似的题,求最大的,有一种方法用到了线性基. 那么线性基能不能表示第k大的呢? 显然,因为线性基可以不重复的表示所有结果.它和原数组是等价的. 对于一个满秩矩阵 100000 010000 001000 000100 000010 000001

ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

Problem Description In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,an representing the size of the water source. Given a set of queries eac