Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9564 | Accepted: 4223 |
Description
Input
Output
Sample Input
3 3 1 2 3 1 3 1 3 2 1 10 2 1 2 3 4 5 6 7 8 9 10
Sample Output
3 1 2 1 2 3 1 2 3 4 5 6 7 9 8 10
还是排列题,递归500+MS AC。。。(有人用STL 400MS+,比较猥琐)
#include<iostream> using namespace std; int n,k,step[1100]; char lock[1100]; void f(int d) { if(d>n) { k--; return; } for(;step[d]<=n;step[d]++) if(!lock[step[d]]) { lock[step[d]]=1; f(d+1); lock[step[d]]=0; if(!k) return; } step[d]=1; } int main() { int i,t; cin>>t; while(t--) { cin>>n>>k; memset(lock,0,sizeof(lock)); for(i=1;i<=n;i++) scanf("%d",&step[i]); k++; while(k) f(1); for(i=1;i<=n;i++) printf("%d ",step[i]); cout<<endl; } return 0; }