标签:des style blog http io ar color os sp
优先队列构造前两列....
5 3
2 4 3 1 5 1 5 4 2 3 3 5 2 4 1HintIn 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.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int n,m;
int li[111][111];
bool have[111];
struct DUI
{
int x,s;
bool operator < (const DUI xx) const
{
return s>xx.s;
}
};
int sum[111];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
sum[i]=m/n+((i<=m%n)?1:0);
}
for(int u=1;u<=n;u++)
{
priority_queue<DUI> q;
for(int i=1;i<=n;i++)
{
if(i==u) continue;
q.push((DUI){i,sum[i]});
}
for(int i=0;i<sum[u];i++)
{
int pos=u+i*n;
DUI D=q.top(); q.pop();
li[pos][1]=D.x;
D.s++;
q.push(D);
}
}
for(int i=1;i<=m;i++)
{
int c=i;
while(c>n) c-=n;
li[i][0]=c;
}
for(int i=1;i<=m;i++)
{
memset(have,false,sizeof(have));
have[li[i][0]]=true; have[li[i][1]]=true;
int p=1;
for(int j=2;j<n;j++)
{
while(have[p]==true) p++;
li[i][j]=p;
have[p]=true;
}
}
for(int i=1;i<=m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d%c",li[i][j],(j==n-1)?'\n':' ');
}
}
}
return 0;
}
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/ck_boss/article/details/41393889