hdu4671 思维构造

http://acm.hdu.edu.cn/showproblem.php?pid=4671

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.

/**
hdu4671  思维构造
题目大意:有n台服务器和m个数据库,我们要用服务器运行数据库,对于每一个数据库被运行服务器的优先级为1~n的一个排列,每个数据库只运行一次,
           问怎样定义m个数据库的优先级,假设有一台服务器坏了的情况下仍然满足每台服务器的运行数据库的数量差不能大于1
解题思路:这个题是一个考验思维的题,当然答案有很多。我只要确定每个数据库优先级最高和次高的就可以了,我们分两种情况来讨论:
           1.n>=m 在这种情况下1~m第一优先级的为1~m,第二优先级的为余下的任意(若n==m,则所有任意,要保证第一第二不能是一个数)
           2.n<m  在这种情况下1~m为1~n,再1~n,知道循环够m。第二优先级,我们对于第一优先级一样的放在一块考虑,从n~1循环(和第一重复就跳过)。
           为什么这样呢?因为假设i坏了,那么第二优先级增加的还是各一个,仍保证是对的。注意要n~1循环,因为第m不一定是n的倍数,所以有i~n
           可能会在第一优先级里少排一个,我们在第二优先级里要优先考虑,否则会出现有一个坏了的话差大于1的情况
*/
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,a[105][2],flag[105];
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n>=m)
        {
            for(int i=1;i<=m;i++)
            {
                a[i][0]=i;
                if(a[i][0]==n)
                   a[i][1]=1;
                else
                   a[i][1]=n;
            }
        }
        else
        {
            for(int i=1;i<=m;i++)
            {
                a[i][0]=(i%n==0)?n:i%n;
            }
            for(int i=1;i<=n;i++)
            {
                int k=n;
                for(int j=1;j<=m;j++)
                {
                    if(a[j][0]==i)
                    {
                        k=(k%n==0)?n:k%n;
                        if(k==i)k--;
                        k=(k%n==0)?n:k%n;
                        a[j][1]=k--;
                       // printf("??%d:%d\n",a[j][0],a[j][1]);
                    }
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            //printf(">>%d %d\n",a[i][0],a[i][1]);
        }
        for(int i=1;i<=m;i++)
        {
            memset(flag,0,sizeof(flag));
            printf("%d %d",a[i][0],a[i][1]);
            flag[a[i][0]]=flag[a[i][1]]=1;
            for(int j=1;j<=n;j++)
            {
                while(flag[j])j++;
                if(j>n)break;
                printf(" %d",j);
                flag[j]=1;
            }
            printf("\n");
        }
    }
    return 0;
}
/**
3 14
answer:
1 3 2
2 3 1
3 2 1
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值