HDU 4671 Backup Plan (2013多校联合7 1006)

来源:互联网 发布:微信查删除好友软件 编辑:程序博客网 时间:2024/06/12 20:33

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


Backup Plan

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


这段时间太懒,几场多校也没写总结,这几天补上的说。。。

关于这道题,我们容易发现只有顺位为1和2的服务器有意义(也就是第一列和第二列),其他的就随便放就是了。

对于对于第一顺位的,我们直接按顺序1234.。。1234.。。这么排就行了,因为如果没有服务器坏掉的话,第一顺位的服务器出现数量要尽量平均,对于第二顺位的,因为只有当有服务器坏掉的时候才有可能使用它们,考虑坏掉一台的情况,我们把这台服务在不坏的情况下承载的所有数据库分到其他服务器上即可,优先放到承载数据库数量较少的服务器上。容易发现,由于每台服务器坏掉的情况是独立的,对每台服务器做这样的操作后就得到了一个合法的答案。

详细方法可以参考我的代码。如下:


#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#define maxn 110using namespace std;int vis[maxn][maxn];int ans[maxn][maxn];int main(){   //freopen("dd.txt","r",stdin);   int n,m;   while(scanf("%d%d",&n,&m)!=EOF)   {       memset(ans,0,sizeof(ans));       memset(vis,0,sizeof(vis));       int i,j;           int tmp=m%n;           for(i=1;i<=m;i++)           {               int t=i%n;               if(t==0)               t=n;               ans[i][1]=t;               vis[i][t]=1;           }           for(i=1;i<=n;i++)           {               int tt=tmp+1;               if(tt>n)               tt-=n;               int po=i;               for(j=po;j<=m;j+=n)               {                   if(tt>n)                   tt-=n;                   if(tt==i)                   tt++;                   if(tt>n)                   tt-=n;                   ans[j][2]=tt;                   vis[j][tt]=1;                   tt++;               }           }           for(i=1;i<=m;i++)          {              int num=3;              for(j=1;j<=n;j++)              {                  if(vis[i][j])                  continue;                  ans[i][num++]=j;              }          }       for(i=1;i<=m;i++)       {           for(j=1;j<=n;j++)           {               printf("%d ",ans[i][j]);           }           printf("\n");       }   }    return 0;}


原创粉丝点击