团体程序设计天梯赛 L1-049. 天梯赛座位分配(测试数据+不同方法)
Data:
/*
3
3 2 1
#1
1 4 7 10 13 16 19 22 25 28
31 33 35 37 39 41 43 45 47 49
51 53 55 57 59 61 63 65 67 69
#2
2 5 8 11 14 17 20 23 26 29
32 34 36 38 40 42 44 46 48 50
#3
3 6 9 12 15 18 21 24 27 30
2
1 2
#1
1 3 5 7 9 11 13 15 17 19
#2
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
1
3
#1
1 3 5 7 9 11 13 15 17 19
21 23 25 27 29 31 33 35 37 39
41 43 45 47 49 51 53 55 57 59
*/
Way1 数组:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cmath> 5 #include <stdbool.h> 6 #include <set> 7 #include <vector> 8 #include <map> 9 #include <algorithm> 10 using namespace std; 11 12 long f[105][105],g[105],a[105]; 13 14 int main() 15 { 16 long n,i,j,num,c,pos; 17 scanf("%ld",&n); 18 for (i=1;i<=n;i++) 19 { 20 scanf("%ld",&a[i]); 21 a[i]*=10; 22 g[i]=0; 23 } 24 25 c=n; 26 num=0; 27 pos=0; 28 while (c>1) 29 { 30 for (i=1;i<=n;i++) 31 if (g[i]!=a[i]) 32 { 33 num++; 34 g[i]++; 35 f[i][g[i]]=num; 36 if (g[i]==a[i]) 37 c--; 38 pos=i; 39 } 40 } 41 if (c==1) 42 { 43 for (j=1;j<=n;j++) 44 if (g[j]!=a[j]) 45 break; 46 if (pos!=j) 47 num++; 48 else 49 num+=2; 50 g[j]++; 51 f[j][g[j]]=num; 52 for (i=g[j]+1;i<=a[j];i++) 53 { 54 num+=2; 55 g[j]++; 56 f[j][g[j]]=num; 57 } 58 } 59 for (i=1;i<=n;i++) 60 { 61 printf("#%ld\n",i); 62 for (j=1;j<=a[i];j++) 63 { 64 printf("%ld",f[i][j]); 65 if (j%10==0) 66 printf("\n"); 67 else 68 printf(" "); 69 } 70 } 71 return 0; 72 }
Way2 链环:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cmath> 5 #include <stdbool.h> 6 #include <set> 7 #include <vector> 8 #include <map> 9 #include <algorithm> 10 using namespace std; 11 12 //Á´»· 13 struct node 14 { 15 long a,b; 16 struct node *next,*pre; 17 }*q,*p,*r; 18 19 long sch[105][105],g[105]; 20 21 int main() 22 { 23 long n,i,j,d,num=0; 24 bool vis; 25 q=NULL; 26 scanf("%ld",&n); 27 for (i=1;i<=n;i++) 28 { 29 scanf("%ld",&d); 30 p=(struct node *) malloc (sizeof(struct node)); 31 p->a=i; 32 p->b=d*10; 33 if (q==NULL) 34 q=p; 35 else 36 { 37 r->next=p; 38 p->pre=r; 39 } 40 r=p; 41 } 42 r->next=q; 43 q->pre=r; 44 45 vis=false; 46 p=q; 47 while (1) 48 { 49 if (p->a==p->next->a && vis) 50 num+=2; 51 else 52 num++; 53 g[p->a]++; 54 sch[p->a][g[p->a]]=num; 55 p->b--; 56 vis=true; 57 if (p->b==0) 58 { 59 if (p->a==p->next->a) 60 break; 61 else 62 { 63 p->pre->next=p->next; 64 p->next->pre=p->pre; 65 if (p->pre->a==p->next->a) 66 vis=false; 67 } 68 } 69 p=p->next; 70 } 71 for (i=1;i<=n;i++) 72 { 73 printf("#%ld\n",i); 74 for (j=1;j<=g[i];j++) 75 { 76 printf("%ld",sch[i][j]); 77 if (j%10==0) 78 printf("\n"); 79 else 80 printf(" "); 81 } 82 } 83 return 0; 84 }
Way3 vector:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cmath> 5 #include <stdbool.h> 6 #include <set> 7 #include <vector> 8 #include <map> 9 #include <algorithm> 10 using namespace std; 11 12 vector<pair<int,int> > f; 13 vector<int> sch[105]; 14 15 int main() 16 { 17 long n,a,num,pre,i; 18 vector<pair<int,int> >::iterator j; 19 vector<int>::iterator k; 20 scanf("%ld",&n); 21 for (i=1;i<=n;i++) 22 { 23 scanf("%ld",&a); 24 f.push_back(make_pair(a*10,i)); 25 } 26 num=0; 27 pre=-1; 28 while (!f.empty()) 29 { 30 31 for (j=f.begin();j!=f.end();j++) 32 { 33 if (pre==j->second) 34 num+=2; 35 else 36 num++; 37 sch[j->second].push_back(num); 38 pre=j->second; 39 j->first--; 40 if (j->first==0) 41 { 42 f.erase(j); 43 j--; 44 } 45 } 46 } 47 for (i=1;i<=n;i++) 48 { 49 printf("#%ld\n",i); 50 for (k=sch[i].begin(),a=1;k!=sch[i].end();k++,a++) 51 { 52 printf("%ld",*k); 53 if (a%10==0) 54 printf("\n"); 55 else 56 printf(" "); 57 } 58 } 59 return 0; 60 }