HDU 4671 Backup Plan

来源:互联网 发布:杨振宁 国籍 知乎 编辑:程序博客网 时间:2024/05/16 23:49

Backup Plan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 136    Accepted Submission(s): 54
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 Ai, then load-balanced means max∣Ai - Aj∣≤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 51 5 4 2 33 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
 

题意: 略:
思路: YY
只要弄好前两列就可以。 因为整个计划只会坏一台服务器。
尽量让每个服务器出现次数相同
第一列按顺序填就好。
第二列, 在相同服务器的,  更换的服务器接着第一列的顺序。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int V = 105;int n, m, ans[V][V];bool vis[V][V];int main() {    int i, j, k;    while(~scanf("%d%d", &n, &m)) {        memset(ans, 0, sizeof(ans));        memset(vis, false, sizeof(vis));        for(i = 0, j = 1; i < m; ++i) {            ans[i][0] = j;            vis[i][j] = true;            j = j == n ? 1 : j + 1;        }        for(i = 0; i < m; ++i) {            if(ans[i][1] != 0)                continue;            int now = m % n + 1;            for(j = i; j < m; j += n) {                if(now == ans[j][0])                    now = now == n ? 1 : now + 1;                ans[j][1] = now;                vis[j][now] = true;                now = now == n ? 1 : now + 1;            }        }        for(i = 0; i < m; ++i)            for(j = 2, k = 1; j < n; ++j) {                while(vis[i][k])                    k++;                ans[i][j] = k++;            }        for(i = 0; i < m; ++i) {            for(j = 0; j < n - 1; ++j)                printf("%d ", ans[i][j]);            printf("%d\n", ans[i][n - 1]);        }    }}




原创粉丝点击