hdu4671 思维构造
pid=4671">http://acm.hdu.edu.cn/showproblem.php? pid=4671
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.
Each test case has one line containing two integer N ( 2≤N≤100) and M ( 1≤M≤100).
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.
/**
hdu4671 思维构造
题目大意:有n台server和m个数据库,我们要用server执行数据库。对于每一个数据库被执行server的优先级为1~n的一个排列,每一个数据库仅仅执行一次,
问如何定义m个数据库的优先级,如果有一台server坏了的情况下仍然满足每台server的执行数据库的数量差不能大于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
*/
hdu4671 思维构造的更多相关文章
- 思维/构造 HDOJ 5353 Average
题目传送门 /* 思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的.一看到这题就想到了UVA_11300(求最小交换数) 这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举 ...
- Pythagorean Triples毕达哥斯拉三角(数学思维+构造)
Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...
- A Mist of Florescence CodeForces - 989C(思维构造)
题意: 让你构造一个图,使得A,B,C,D的个数为给定的个数,上下左右连通的算一个. 哎呀 看看代码就懂了..emm..很好懂的 #include <bits/stdc++.h> usin ...
- Educational Codeforces Round 53C(二分,思维|构造)
#include<bits/stdc++.h>using namespace std;const int N=1e6+6;int x[N],y[N];int sx,sy,n;char s[ ...
- 牛客多校训练第八场C.CDMA(思维+构造)
题目传送门 题意: 输入整数m( m∈2k ∣ k=1,2,⋯,10),构造一个由1和-1组成的m×m矩阵,要求对于任意两个不同的行的内积为0. 题解: Code: #include<bits/ ...
- Atcoder C - +/- Rectangle(思维+构造)
题目链接:http://agc016.contest.atcoder.jp/tasks/agc016_c 题解:挺简单的构造,很容易想到的构造方法就是(h*w)的小矩阵里其他值赋值为1,最后一个赋值为 ...
- AtCoder - 2282 (思维+构造)
题意 https://vjudge.net/problem/AtCoder-2282 告诉你sx,sy,tx,ty,问从s走到t,再从t走到s,再从s走到t,再从t回到s的最短路,每次只能上下左右选一 ...
- ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication && ZOJ 4061 (思维+构造)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an ...
- Vasya And The Matrix CodeForces - 1016D (思维+构造)
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the ma ...
随机推荐
- code forces 979C
C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 【TEST】NOI-Linux可用 gedit c++精简配置 附Emacs日常配置
这里是backup的测试随笔,用于测试 CSS / Markdown 效果. 同时也是是本菜鸡考场上一般使用的Gedit配置. 只有6行,挺短的.应该算好记吧. 使用之前记得勾选首选项里的外部工具. ...
- ABC103
Wow今天听同学说了这个网站,做了一次比赛的题目,只有四道题. A.三个数a,b,c,找两个最小的差相加,显然是中间数与另外两个数的差,也就是最大值减最小值了 B.两个字符串,判断能否通过对一个进行每 ...
- "贪心"的考试
先来落实比较简单的T3,T4 T3 团结的队伍 team 题目描述 众所周知,长郡信息组是一个团结友爱的优秀团队.为了弘扬团队精神,践行社会主义核心价值观,秉承朴实沉毅的校训,信息组决定外出游玩(. ...
- YYH的球盒游戏(NOIP模拟赛Round 6)
题目描述 YYH有一些总共有种颜色的球,他有颜色的球个.他同样有个盒子,第个盒子能放个球. 他的目标是把这个球按规则放进个盒子里: 对于一个盒子,对于每种颜色的球至多只能放个. 把颜色为的球放进盒子, ...
- (二十五)epoll深入理解续
转自:http://blog.csdn.net/yusiguyuan/article/details/15027821 在Linux的网络编程中,很长的时间都在使用select来做事件触发.在linu ...
- jsp/el和jstl动态页面
一.JSP技术 1.jsp脚本和注释 jsp脚本: 1)<%java代码%> ----- 内部的java代码翻译到service方法的内部 2)<%=java变量或表达式> - ...
- 服务器IIS禁止通过IP访问
之前发布的网站,只是配置端口信息,IP都选择的是*,所以一直没有考虑IP能访问的事情. 现在有个需求,就是只能通过域名访问,不能通过IP访问,那么做法就是给这个主机名添加上解析的域名,IP自然就失效了
- [thinkphp]查看thinkphp系统定义的常量值
echo 'MEMORY_LIMIT_ON: ' . MEMORY_LIMIT_ON . '<br>'; echo 'THINK_PATH: ' . THINK_PATH . '<b ...
- CF986A Fair【图论/BFS】
[题意]: 有些公司将在Byteland举办公平的会议.Byteland的n个城镇,m条两镇之间的双向道路.当然,你可以使用道路从任一个城镇到达任何城镇. 有k种商品产自Byteland,并且每个城镇 ...