HDU 6351 Beautiful Now (暴力搜索)

Beautiful Now

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


 

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

 

 

Sample Output

 

12 21 123 321 298944353 998544323 238944359 998544332 233944859 998544332

 

 

Source

2018 Multi-University Training Contest 5

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6361 6360 6359 6358 6357 

 

 

#include<iostream>
#include<algorithm>
#include<fstream>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 200
using namespace std;

int n,m;
int i,j;
int num[maxn],cnt;
int tmp[maxn];

/*
题目大意:给定一个数串,要求交换k次后的最大最小值。
(可以交换自己)

注意到数据量,k大于九次时就可以得到其最小值和最大值了。
那么当k小于九次时不妨直接暴力搜索。

这题代码暂且过不了,思路是对的,望细心的人士指点,
本人日后也做修改。

*/

int minv[maxn],maxv[maxn];

void swp(int &x,int &y)
{
    if(x==y) return ;
    int tp=x;
    x=y;
    y=tp;
}

bool judge1()
{
    for(int i=0;i<cnt;i++)
    {
        if(minv[i]==num[i]) continue;
        return minv[i]>num[i];
    }
    return true;
}

bool judge2()
{
    for(int i=0;i<cnt;i++)
    {
        if(maxv[i]==num[i]) continue;
        return maxv[i]<num[i];
    }
    return true;
}

void dfs1(int u,int v)
{
    if(judge2())  memcpy(maxv,num,sizeof(num));
    if(v==0||u>=cnt-1) return ;
    int mv=num[u];
    for(int i=u;i<cnt;i++) mv=max(mv,num[i]);
    dfs1(u+1,v);
    for(int i=u;i<cnt;i++)
    {
        if(num[i]==mv)
        {
            swp(num[i],num[u]);
            dfs1(u+1,v-1);
            swp(num[i],num[u]);
        }
    }
}

void dfs2(int u,int v)
{
     if(judge1())  memcpy(minv,num,sizeof(num));
    if(v==0||u>=cnt-1) return ;
    int mv=10;
    for(int i=u;i<cnt;i++)
        mv=min(mv,num[i]);
    dfs2(u+1,v);
    for(int i=u;i<cnt;i++)
    {
        if(u==0&&mv==0) continue;
        if(num[i]==mv)
        {
            swp(num[i],num[u]);
            dfs2(u+1,v-1);
            swp(num[i],num[u]);
        }
    }
}

int main()
{
    ofstream write;
    write.open("out.txt");
    int t;scanf("%d",&t);
    while(t--)
    {
        memset(num,0,sizeof(num));
        scanf("%d%d",&n,&m);cnt=0;

        while(n) {num[cnt++]=n%10;n/=10;}
        i=0,j=cnt-1;while(i<j) { swp(num[i],num[j]) ; i++,j--; }

        memcpy(maxv,num,sizeof(num));
        memcpy(minv,num,sizeof(num));

        sort(maxv,maxv+cnt);sort(minv,minv+cnt);
        i=0,j=cnt-1;   while(i<j) { swp(minv[i],minv[j]) ; i++,j--; }

        if(m>=9)
        {
            ///for(int i=0;i<cnt;i++)  write<<maxv[i];write<<" ";
            ///for(int i=0;i<cnt;i++)  write<<minv[i];write<<"\n";
            for(int i=0;i<cnt;i++) printf("%d",maxv[i]);printf(" ");
            for(int i=0;i<cnt;i++) printf("%d",minv[i]);printf("\n");
        }
        else
        {
            memcpy(tmp,num,sizeof(num));
            dfs1(0,m);
            memcpy(num,tmp,sizeof(num));
            dfs2(0,m);
            for(int i=0;i<cnt;i++) printf("%d",minv[i]);printf(" ");
            for(int i=0;i<cnt;i++) printf("%d",maxv[i]);printf("\n");
            ///for(int i=0;i<cnt;i++)  write<<minv[i];write<<" ";
           /// for(int i=0;i<cnt;i++)  write<<maxv[i];write<<"\n";
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值