HDU 4671 Backup Plan

Backup Plan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 486    Accepted Submission(s): 224
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
 

Recommend
zhuyuanchen520
   找了好久的规律,注意处理好细节
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define N 110
using namespace std;
int main()
{
    //freopen("data.in","r",stdin);
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        if(n==m)
        {
            for(int i=1;i<=m;i++)
            {
                printf("%d",i);
                for(int j=1;j<=n;j++)
                {
                    if(j!=i)
                    {
                        printf(" %d",j);
                    }
                }
                printf("\n");
            }
            continue;
        }
        if(n>m)
        {
            for(int i=1;i<=m;i++)
            {
                printf("%d %d",i,n);
                for(int j=1;j<=n-1;j++)
                {
                    if(j!=i)
                    {
                        printf(" %d",j);
                    }
                }
                printf("\n");
            }
            continue;
        }
        if(n<m)
        {
            int T = m/n;
            int sta=1,end=n;
            if(m%n==0)
            {
                for(int i=1;i<=n;i++)
                {
                    int x=i+1;
                    if(i==n)
                    {
                        x=1;
                    }
                    for(int j=1;j<=T;j++)
                    {
                        printf("%d",i);
                        printf(" %d",x);
                        for(int y=1;y<=n;y++)
                        {
                            if(y==i||y==x)
                            {
                                continue;
                            }
                            printf(" %d",y);
                        }
                        printf("\n");
                        x+=1;
                        if(x==n+1)
                        {
                            x=1;
                        }
                        if(x==i)
                        {
                            x+=1;
                        }
                        if(x==n+1)
                        {
                            x=1;
                        }
                    }
                }
                continue;
            }
            end = m%n;
            T = T+1;
            int u=end+1,v=n;
            for(int i=1;i<=end;i++)
            {
                int x=u;
                for(int j=1;j<=T;j++)
                {
                    printf("%d",i);
                    printf(" %d",x);
                    for(int y=1;y<=n;y++)
                    {
                        if(y==i||y==x)
                        {
                            continue;
                        }
                        printf(" %d",y);
                    }
                    printf("\n");
                    if(x<v)
                    {
                        x+=1;
                    }else
                    {
                        x=1;
                    }
                    if(x==i)
                    {
                        x+=1;
                    }
                }
            }
            for(int i=u;i<=n;i++)
            {
                int x = u;
                if(x==i)
                {
                    x+=1;
                }
                if(n-u+1==1)
                {
                    x=1;
                }
                for(int j=1;j<=T-1;j++)
                {
                    printf("%d",i);
                    printf(" %d",x);
                    for(int y=1;y<=n;y++)
                    {
                        if(y==i||y==x)
                        {
                            continue;
                        }
                        printf(" %d",y);
                    }
                    printf("\n");
                    x+=1;
                    if(x==i)
                    {
                        x+=1;
                    }
                    if(x==n+1)
                    {
                        x = 1;
                    }
                    if(x==i)
                    {
                        x+=1;
                    }
                }
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值