Beautiful NowTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2815 Accepted Submission(s): 1026 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. Input The first line contains one integer T , indicating the number of test cases. 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 |
超时了无数次,竟然是因为标记数组应该用布尔型,CTRYB!
#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
#define ll long long
#define maxn 100010
#define mod 7
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char s[20];
int k,len;
int a[20];
bool vis[20];
bool check(){
memset(vis,false,sizeof(vis));
int tmp=0;
for(int i=0;i<len;i++){
if(vis[i])continue;
int x=0;
while(!vis[i]){
x++;vis[i]=true;
i=a[i];
}
tmp+=(x-1);
if(tmp>k)return false;
}
return tmp<=k;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s%d",s,&k);
len=strlen(s);
ll minn=inf,maxx=-1;
int x=0;
for(int i=0;i<len;i++){
a[i]=i;
}
do{
if(s[a[0]]!='0'&&check()){
x=0;
for(int i=0;i<len;i++)
x=x*10+(s[a[i]]-'0');
minn=minn>x?x:minn;
maxx=maxx>x?maxx:x;
}
}while(next_permutation(a,a+len));
printf("%lld %lld\n",minn,maxx);
}
}