Today's game because of the time problem I did this one topic description
Give you an n-bit number, each operation can choose the number of any adjacent two bits to exchange, if you can operate a maximum of K, then the final can get the smallest number of what
(n-bit and cannot contain leading 0)?
Input
There are several sets of test data, the first behavior data number T (t<=10); One row for each group of data, containing a number (no more than 1000 bits) and K (0<=k<=1000), separated by a space;
Output
The smallest number that can eventually be obtained.
Sample input
2321654987 1321654987 2
Sample output
231654987132654987
This problem has no algorithm, is to use two nested for loop, and then to pay attention to the boundary conditions can be a, the game WA several times, either forget the memset, or CT forget reset to 0, or J write I, in short, very egg pain, made a lot of low-level error, not the state ah.
Let me explain my code and ideas.
I like the greedy algorithm, that is, each time the whole sequence is scanned, with Num[0~9] mark the first occurrence of the 0~9 10 number of positions, and then start from 0 to 9for Loop, if the minimum number of position and the current location to determine the distance is less than equal to K, then you can move the array back , and then put that small number in the current operation position, k-= the distance of the operation.
My Code also has a lot of needs to improve the place, can reduce complexity, but the game is more hasty, not considering so much, can a on the line. Here is the source code for my game. In fact, now there are a lot of places to prune, if the algorithm timed out, and then changed (I was thinking so).
#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include < string> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack>using namespace std;typedef long long ll;const int inf=0x7fffffff; const int Max_n=1009;int T,k,ct;char a[max_n];int num[10];int Main () { cin>>t; while (t--) { memset (A,0,sizeof (a)); scanf ("%s", A); s CANF ("%d", &k); int Len=strlen (A); memset (num,-1,sizeof (num)); ct=0; for (int i=1;i<len;i++) { if (num[a[i]-' 0 ']==-1) {//can also reduce complexity here, as long as the count is less than a[0] on the line Num[a[i] -' 0 ']=i; &NBSP ct++; } if (ct==10) break; &N Bsp } for (int i=1;i<=9;i++) {//This place can change 9 to a[0]-' 0 ' &NBSP ; if (num[i]<=k&&num[i]!=-1&&i<a[0]-' 0 ') { & nbsp Char cur=a[num[i]]; for (int j=num[i];j>=1;j--) { &NB Sp a[j]=a[j-1]; }&NBS P a[0]=cur; K-=num[i ]; break; } & nbsp } for (int i=1;i<len;i++) { if (k==0) Break;&nbs P; memset (num,-1,sizeof (num)); ct=0; for (int j=i+1;j<len;j++) { if (num[a[ j]-' 0 ']==-1) { num[a[j]-' 0 ']=j; &N Bsp ct++; } &NBS P if (ct==10) break; } for (int j=0;j<=9;j++) { if (num[j]-i<=k&& ; num[j]!=-1&&j<a[i]-' 0 ') { Char cur=a[num[ j]]; for (int p=num[j];p >i;p--) { &NBSP ; a[p]=a[p-1]; &NBSP ; } a[i]=cur; k-=num[j]-i; break ; } } } cout<<a<<endl; } return 0;}
2015 Xiangtan University Program Design Competition (Internet) problem D: the smallest number