HDU 4671 Backup Plan (2013多校7 1006题 构造)

Backup Plan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 93    Accepted Submission(s): 36
Special Judge

Problem Description Makomuno has N servers and M databases. All databases are synchronized among all servers and each database has a ordered list denotes the priority of servers to access. This list is guaranteed to be a valid permutation of all servers.

Every time someone wants to execute queries on a certain database, he will send a request to the first server in the list. If it’s dead, he will simply turn to the next one. Otherwise a working copy of the database is found, and this copy is called active.

Now, given N and M, Makomuno wants to find a permutation for each database which could assure that all servers are load-balanced. Moreover, Makomuno hopes the system will be load-balanced even if 
exactly one server is broken.

Note that if we call the number of active copies on i-th server A
i, then load-balanced means max∣A
i – A
j∣≤1 for any i and j in non broken servers set. We won’t consider broken servers in this case.  

 

Input The input contains several test cases, terminated by EOF.

Each test case has one line containing two integer N ( 2≤N≤100) and M ( 1≤M≤100).  

 

Output For each case output M lines, the i-th line contains a permutation of all servers, indicating the expected order. Servers are numbered from 1 to n.  

 

Sample Input 5 3  

 

Sample Output 2 4 3 1 5 1 5 4 2 3 3 5 2 4 1
Hint In the sample test case, the active copies of these databases are on server 2,1 and 3 in normal state. A = {1,1,1,0,0} If server 1 or 3 has broken, server 5 will take its work. In case we lost server 2, the second database will use server 4 instead. A = {1,BROKEN,1,1,0} It’s clear that in any case this system is load-balanced according to the plan in sample output.  

 

Source
2013 Multi-University Training Contest 7  

 

Recommend zhuyuanchen520  

 

 

 

题目意思很难懂,自己去理解吧、

 

 

就是要构造出一个m*n的矩阵

 

我的做法就是

1) n>=m

那么第一列就放1,2,….m

第二列放n n n n n n n n (如果n==m的情况,那么在第一列是n的时候放n-1).

其余列放没有在第一列和第二列出现的既可。

 

2) n < m

 

这种情况:

第一列的方法是1 2 3 ….n  1  2  3 ….n  1  2  3…这样循环

第二列。

对于第1列放1的行,就是n*k+1(k=0,1,2…)行,从n,n-1,n-2,…这样循环放,遇到1就跳过。

对于放2的行一样处理,遇到2跳过

对于其它数也一样。

 

然后其余列只有和前两列不同既可

 1 /* **********************************************
 2 Author      : kuangbin
 3 Created Time: 2013/8/13 13:34:52
 4 File Name   : F:\2013ACM练习\2013多校7\1006.cpp
 5 *********************************************** */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 using namespace std;
19 int a[110][110];
20 bool used[110];
21 int main()
22 {
23     //freopen("in.txt","r",stdin);
24     //freopen("out.txt","w",stdout);
25     int n,m;
26     while(scanf("%d%d",&n,&m) == 2)
27     {
28         if(n >= m)
29         {
30             for(int i = 1;i <= m;i++)
31             {
32                 a[i][1] = i;
33                 a[i][2] = n;
34                 if(a[i][1]==n)a[i][2] = n-1;
35                 memset(used,false,sizeof(used));
36                 used[a[i][1]] = used[a[i][2]] = true;
37                 int t = 1;
38                 for(int j = 3;j <= n;j++)
39                 {
40                     while(used[t])t++;
41                     a[i][j] = t++;
42                 }
43             }
44         }
45         else
46         {
47             int t = 1;
48             for(int i = 1;i <= m;i++)
49             {
50                 a[i][1] = t++;
51                 if(t > n)t = 1;
52             }
53             for(int i = 1;i <= n;i++)
54             {
55                 if(i < n)t = n;
56                 else t = n-1;
57                 for(int j = 0;j*n+i <= m;j++)
58                 {
59                     a[j*n+i][2] = t;
60                     t--;
61                 //    printf("%d %d %d\n",i,j,t);
62                     if(t == 0)t = n;
63                     if(t == i)t--;
64                     if(t==0)t=n;
65                 }
66 
67             }
68             for(int i = 1;i <= m;i++)
69             {
70                 memset(used,false,sizeof(used));
71                 used[a[i][1]] = used[a[i][2]] = true;
72                 int t = 1;
73                 for(int j = 3;j <= n;j++)
74                 {
75                     while(used[t])t++;
76                     a[i][j] = t++;
77                 }
78             }
79         }
80         for(int i = 1;i <= m;i++)
81         {
82             for(int j = 1;j <= n;j++)
83             {
84                 printf("%d",a[i][j]);
85                 if(j < n)printf(" ");
86                 else printf("\n");
87             }
88         }
89     }
90     return 0;
91 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3255891.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞