CF181c
题意:
怎样改动得到一个beautiful数。。。
枚举!!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 sort+bruce 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 //#include<map> 11 #include<math.h> 12 using namespace std; 13 typedef long long ll; 14 //typedef __int64 int64; 15 const int maxn = 10005; 16 const int inf = 0x7fffffff; 17 const double pi=acos(-1.0); 18 19 int vis[ maxn ]; 20 char last[ maxn ],tmp[ maxn ],a[ maxn ]; 21 int ans; 22 23 int main(){ 24 int n,k; 25 while( scanf("%d%d",&n,&k)!=EOF ){ 26 scanf("%s",a); 27 memset( vis,0,sizeof( vis )); 28 for( int i=0;i<n;i++ ) 29 vis[ a[ i ]-'0' ]++; 30 ans=inf; 31 32 for( int i=0;i<=9;i++ ){//枚举1 to 9 ,即k位相同的是多少 33 34 if( vis[ i ]>=k ){ 35 ans=0; 36 strcpy( last,a ); 37 } 38 39 int now_sum=vis[ i ]; 40 int now_cost=0; 41 strcpy( tmp,a ); 42 for( int j=1;j<=9;j++ ){//对于每一位上 枚举是增是减 且 改变量是多少 43 if( i+j<=9 ){ 44 for( int jj=0;jj<n;jj++ ){ 45 if( now_sum==k ) break; 46 if( tmp[ jj ]==i+j+'0' ) now_sum++,now_cost+=j,tmp[ jj ]=i+'0'; 47 } 48 }//这里从0 到 n-1 是因为原数是i+j,之后变为i。。这样就能得到字典序最小 49 if( i-j>=0 ){ 50 for( int jj=n-1;jj>=0;jj-- ){ 51 if( now_sum==k ) break; 52 if( tmp[ jj ]==i-j+'0' ) now_sum++,now_cost+=j,tmp[ jj ]=i+'0'; 53 } 54 } 55 if( now_sum==k ) break; 56 } 57 tmp[ n ]='\0'; 58 if( now_sum==k ){ 59 if( now_cost<ans||(now_cost==ans&&strcmp( last,tmp )>0) ){ 60 ans=now_cost; 61 strcpy( last,tmp ); 62 /* 63 printf("test\n"); 64 printf("ans;%d ",ans); 65 printf("last: %s \n",last); 66 */ 67 } 68 } 69 } 70 printf("%d\n",ans); 71 printf("%s\n",last); 72 } 73 return 0; 74 }
keep moving...