当前位置:   article > 正文

HDU 6351 Beautiful Now(记忆化搜索+回溯)_gcd 记忆化

gcd 记忆化

HDU 6351 Beautiful Now(记忆化搜索+回溯)

Beautiful Now

Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1208 Accepted Submission(s): 435

Problem Description

Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautiful after k swaps of digits.
Let the decimal representation of n as (x1x2⋯xm)10 satisfying that 1≤x1≤9, 0≤xi≤9 (2≤i≤m), which means n=∑mi=1xi10m−i. In each swap, Anton can select two digits xi and xj (1≤i≤j≤m) and then swap them if the integer after this swap has no leading zero.
Could you please tell him the minimum integer and the maximum integer he can obtain after k swaps?

Input

The first line contains one integer T, indicating the number of test cases.
Each of the following T lines describes a test case and contains two space-separated integers n and k.
1≤T≤100, 1≤n,k≤109.

Output

For each test case, print in one line the minimum integer and the maximum integer which are separated by one space.

Sample Input

5
12 1
213 2
998244353 1
998244353 2
998244353 3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Sample Output

12 21
123 321
298944353 998544323
238944359 998544332
233944859 998544332
  • 1
  • 2
  • 3
  • 4
  • 5

这题不想说什么了,比赛的时候用暴搜无脑TLE,剪枝就WA,心态爆炸。后来看了同学的代码,他用记忆化搜索过了,后来写了一份。菜是原罪。

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 105;

int len;
char num[maxn],n[maxn];
map<pii, int> m;

int getmin(int cnt)
{
    int x = atoi(num);
    if (cnt == 0) return x;
    if (m[pii(x, cnt)]) return m[pii(x, cnt)];  //记忆化
    int res = x;
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (num[i] < num[j])
            {
                swap(num[i], num[j]);
                if (num[0] != '0')//前导零
                {
                    int tmp = getmin(cnt - 1);
                    res = min(tmp, res);
                }
                swap(num[i], num[j]);
            }
        }
    }
    return m[pii(x, cnt)] = res;  //记忆化
}

int getmax(int cnt)
{
    int x = atoi(num);
    if (cnt == 0) return x;
    if (m[pii(x, cnt)]) return m[pii(x, cnt)];
    int res = x;
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (num[i] > num[j])
            {
                swap(num[i], num[j]);
                int tmp = getmax(cnt - 1);
                res = max(tmp, res);
                swap(num[i], num[j]);
                break;
            }
        }
    }
    return m[pii(x, cnt)] = res;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int k;
        scanf("%s%d",&n,&k);
        len = strlen(n);
        k = min(k, 10);//九位数,不会交换太多次

        strcpy(num,n);
        m.clear();
        int minn = getmin(k);

        strcpy(num,n);
        m.clear();
        int maxx = getmax(k);

        printf("%d %d\n", minn, maxx);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1003361
推荐阅读
相关标签
  

闽ICP备14008679号