HDOJ 4671 Backup Plan 构造优先队列

优先队列构造前两列....

Backup Plan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 775    Accepted Submission(s): 365

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 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

2013 Multi-University Training Contest 7

#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;
}
时间: 2024-10-13 04:12:02

HDOJ 4671 Backup Plan 构造优先队列的相关文章

【BZOJ 1150】 1150: [CTSC2007]数据备份Backup (贪心+优先队列)

1150: [CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游戏的乐趣.已知办公 楼都位于同一条街上.你决定给这些办公楼配对(两个一组).每一对办公楼可以通过在这两个建筑物之间铺设网 络电缆使得它们可以互相备份.然而,网络电缆的费用很高.当地电信公司仅能为你提供 K 条网络电缆,这意味 着你仅

BZOJ 1150 [CTSC2007]数据备份Backup(贪心+优先队列)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1150 [题目大意] 给出n个数,请你挑出k对(每个数不可重复选取),使得他们差的绝对值之和最小(k小于等于n/2) [题解] 因为数列给出有序,省去排序步骤,我们发现最优答案一定是选择k对相邻的数, 因此我们将相邻的数两两之间求差,得到差分数列, 现在问题转化为在这个新的数列中,选取k个不相邻的数,使得和最小. 我们发现在选取一个数之后,只对左右两边的数有影响, 所以,每当我们选取一

hdoj 1873 看病要排队(优先队列的运用)

看病要排队 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5771 Accepted Submission(s): 2371 Problem Description 看病要排队这个是地球人都知道的常识. 不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看病.而看病的人病情有轻重,所

HDOJ 5534 Virtual Participation 构造

构造 小于10^5的直接输出1 大于10^5的构造如下的串 1111...12222...233....3.....t,t+1,t+2.. 设长度为n,每种字符出现了L[i]次则不同的串有  n*n+n-sigma(L[i])=2*K 大约估计一个n,用贪心求出L Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi

hdoj 3785 寻找大富翁【优先队列+sort排序】

寻找大富翁 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4504    Accepted Submission(s): 1838 Problem Description 浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. Input 输入包含多组测试用例.每个用例首先包含2个整数n(0<n<=100000)和m(0<m<

hdoj 1509 Windows Message Queue(优先队列)

Windows Message Queue 点击打开链接 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4309 Accepted Submission(s): 1701 Problem Description Message queue is the basic fundamental of windows system. For eac

HDOJ 5385 The path 构造

The path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 521    Accepted Submission(s): 190 Special Judge Problem Description You have a connected directed graph.Let d(x) be the length of the s

TFS Express backup and restore

 When we setup source control server, we should always make a backup and restore plan for it. This article is to describe how to backup and restore a TFS Express instance from one server to another server. This blog is an English version, for Chine

完美网络(优先队列)

完美网络 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 完美网络是连通网络的基础上要求去掉网络上任意一条线路,网络仍然是连通网络.求一个连通网络要至少增加多少条边可以成为完美网络. 输入 第一行输入一个数T代表测试数据个数(T<=20).每个测试数据第一行2个数n,m 分别代表网络基站数和基站间线路数.基站的序号为从1到n.接下来m行两个数代表x,y 代表基站x,y间有一条线路. (0 < n < m < 10