标签:style blog http color io os ar java for
int f[][]; int p[]; int n; void insert(int r, int x) { for (int i=1; 1; i++) if (f[r][i]==0) { f[r][i]=x; return ; } else if (f[r][i]>x) { int tmp=f[r][i]; f[r][i]=x; insert(r+1, tmp); return ; } } void Permutation2Matrix() { for (int i=1; i<=n; i++) insert(1, p[i]); }
2 3 2 2 1 2 1 3 2 1 2 2 1
Case #1: 1 3 2 Case #2: No solution
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<vector> 6 using namespace std; 7 8 vector<int>Q[100005]; 9 vector<int>Hash[100005]; 10 int hxl[100005],len; 11 12 13 void dfs(int x,int y,int dep) 14 { 15 if(x>dep)return; 16 if(Hash[x][y]==-1) 17 { 18 Hash[x][y]=-2; 19 hxl[++len]=Q[x][y]; 20 return; 21 } 22 dfs(x+1,Hash[x][y],dep); 23 hxl[++len]=Q[x][y]; 24 Hash[x][y]=-2; 25 } 26 void solve(int n,int m) 27 { 28 for(int i=m;i>=2;i--) 29 { 30 int k=Hash[i][0]; 31 int s=Hash[i-1][0]; 32 for(int j=k;j>=1;j--) 33 { 34 for(;s>=1;s--) 35 if(Q[i][j]>Q[i-1][s]){ 36 Hash[i-1][s]=j; 37 s--; 38 break; 39 } 40 } 41 } 42 for(int i=1;i<m;i++) 43 { 44 int num=0; 45 for(int j=1;j<=Hash[i][0];j++) 46 if(Hash[i][j]!=-1)num++; 47 if(num!=Hash[i+1][0]){ 48 printf(" No solution\n"); 49 return; 50 } 51 } 52 len = 0; 53 for(int i=1;i<=m;i++) 54 { 55 for(int j=1;j<=Q[i][0];j++) 56 { 57 if(Hash[i][j]==-1)hxl[++len]=Q[i][j]; 58 else if(Hash[i][j]==-2)continue; 59 else dfs(i,j,m); 60 } 61 } 62 for(int i=1;i<=len;i++) 63 printf(" %d",hxl[i]); 64 printf("\n"); 65 } 66 int main() 67 { 68 int T,n,m,x,y; 69 scanf("%d",&T); 70 for(int t=1;t<=T;t++) 71 { 72 scanf("%d%d",&n,&m); 73 for(int i=1;i<=n;i++){ 74 Q[i].clear(); 75 Hash[i].clear(); 76 } 77 bool flag=false; 78 for(int i=1;i<=m;i++) 79 { 80 scanf("%d",&x); 81 Q[i].push_back(x); 82 Hash[i].push_back(x); 83 for(int j=1;j<=x;j++) 84 { 85 scanf("%d",&y); 86 Q[i].push_back(y); 87 Hash[i].push_back(-1); 88 if(j>1&&Q[i][j]<=Q[i][j-1]) flag=true; 89 } 90 if(i>1&&Q[i][0]>Q[i-1][0])flag=true; 91 } 92 printf("Case #%d:",t); 93 if(flag==true)printf(" No solution\n"); 94 else{ 95 solve(n,m); 96 } 97 } 98 return 0; 99 }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/tom987690183/p/4032215.html