ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C - Beautiful Now

2020-01-29 11:55:44  阅读:293  来源: 互联网

标签:Beautiful include 20 int sum2 sum1 integer Now


Anton has a positive integer nn, however, it quite looks like a mess, so he wants to make it beautiful after kk swaps of digits. 
Let the decimal representation of nn as (x1x2⋯xm)10(x1x2⋯xm)10 satisfying that 1≤x1≤91≤x1≤9, 0≤xi≤90≤xi≤9 (2≤i≤m)(2≤i≤m), which means n=∑mi=1xi10m−in=∑i=1mxi10m−i. In each swap, Anton can select two digits xixi and xjxj (1≤i≤j≤m)(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 kk swaps?

InputThe first line contains one integer TT, indicating the number of test cases. 
Each of the following TT lines describes a test case and contains two space-separated integers nn and kk. 
1≤T≤1001≤T≤100, 1≤n,k≤1091≤n,k≤109. 
OutputFor 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

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 
  8 int maxx,minn,k,len;
  9 int c[20],sum1[20],sum2[20],p[20];
 10 char ss[20];
 11 
 12 void update()
 13 {
 14     if(c[ p[1] ]==0)
 15     {
 16         return;
 17     }
 18     // 用sum1记录当前排列
 19     for(int i=1;i<=len;++i)
 20     {
 21         sum1[i]=p[i];
 22     }
 23 
 24     int kk=0,s=0;
 25     for(int i=1;i<=len;++i)
 26     {
 27         s=s*10 + c[ p[i] ];
 28         if(sum1[i] != i)
 29         {
 30             for(int j=i+1;j<=len;++j)
 31             {
 32                 if(sum1[j]==i)
 33                 {
 34                     swap(sum1[i],sum1[j]);
 35                     ++ kk;
 36                     // 这一序列不能在k步内实现
 37                     if(kk>k)
 38                     {
 39                         return;
 40                     }
 41                     break;
 42                 }
 43             }
 44         }
 45     }
 46 
 47     // 当前队列满足条件
 48     // 更新最大最小值
 49     maxx=max(maxx,s);
 50     minn=min(minn,s);
 51 }
 52 
 53 int main()
 54 {
 55     int T;
 56     scanf("%d",&T);
 57     while(T--)
 58     {
 59         memset(sum1,0,sizeof(sum1));
 60         memset(sum2,0,sizeof(sum2));
 61         scanf("%s %d",ss,&k);
 62 
 63         len=strlen(ss);
 64 
 65         for(int i=0;i<len;++i)
 66         {
 67             // 字符转数字
 68             c[i+1]=ss[i]-'0';
 69             ++ sum1[ c[i+1] ];
 70             ++ sum2[ c[i+1] ];
 71         }
 72 
 73         // 剪枝
 74         if(k>=len-1)
 75         {
 76             // 输出最小数
 77             // 输出第一位(特判非零)
 78             for(int i=1;i<=9;++i)
 79             {
 80                 if(sum1[i])
 81                 {
 82                     printf("%d",i);
 83                     -- sum1[i];
 84                     break;
 85                 }
 86             }
 87 
 88             for(int i=0;i<=9;++i)
 89             {
 90                 while(sum1[i])
 91                 {
 92                     printf("%d",i);
 93                     --sum1[i];
 94                 }
 95             }
 96 
 97             printf(" ");
 98 
 99             // 输出最大数
100             for(int i=9;i>=0;--i)
101             {
102                 while(sum2[i])
103                 {
104                     printf("%d",i);
105                     -- sum2[i];
106                 }
107             }
108             printf("\n");
109             continue;
110         }
111 
112         // 未能剪枝
113 
114         // 找一个互不相等的排列(从小到大有序)
115         // 以初始排列为当前数字的编号
116         for(int i=1;i<=len;++i)
117         {
118             p[i]=i;
119         }
120         // 初始化最大最小值
121         minn=2e9;
122         maxx=-1;
123         // 自当前排列开始更新满足条件时的最大最小值
124         do
125         {
126             update();
127         }while(next_permutation(p+1,p+len+1));  // 需要头文件algor
128         // 九位数的全排列,有362880种
129         // 最多尝试次数,为九次
130         // 复杂度约为 10^7
131         printf("%d %d\n",minn,maxx);
132     }
133     return 0;
134 }

 



标签:Beautiful,include,20,int,sum2,sum1,integer,Now
来源: https://www.cnblogs.com/jishuren/p/12239916.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有