Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造
D. High Load
题目连接:
http://codeforces.com/contest/828/problem/D
Description
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.
Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.
Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.
Input
The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.
Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.
Output
In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.
If there are multiple answers, print any of them.
Sample Input
3 2
Sample Output
2
1 2
2 3
Hint
题意
你需要构造n个点一棵树,满足这个树里面只有k个点的度数为1,且树的直径最小。
题解:
纸上随便画一画就可以知道,我们肯定是按照那k个平均去画就好了,也就是首先让一个节点当中央节点,然后剩下的每一个点都分(n-1)/k节点,如果n-k%k!=0的话,我们让一个节点后面增加一个就好了。 >1同理
代码
#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<pair<int,int> >ans;
int main(){
scanf("%d%d",&n,&k);
n=n-1;
int Ans=n/k*2+(n%k>0)+(n%k>1);
int p=0;
for(int i=0;i<n-n%k;i++){
if(i%(n/k)==0)
ans.push_back(make_pair(i+1,n+1));
else
ans.push_back(make_pair(i,i+1));
if(i%(n/k)==0)p=p+1;
}
int step =n/k-1;
for(int i=n-n%k;i<n;i++){
// cout<<p<<endl;
if(p<k){
p=p+1;
ans.push_back(make_pair(step,i+1));
}else{
ans.push_back(make_pair(step+1,i+1));
}
step+=n/k;
}
cout<<Ans<<endl;
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++){
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
}
Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造的更多相关文章
- 【构造】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) B. High Load
让你构造一棵树(给定了总结点数和总的叶子数),使得直径最小. 就先弄个菊花图(周围一圈叶子,中间一个点),然后平均地往那一圈放其他的点即可. #include<cstdio> using ...
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)
Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C
A.题目链接:http://codeforces.com/contest/828/problem/A 解题思路: 直接暴力模拟 #include<bits/stdc++.h> using ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B
Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It i ...
随机推荐
- linux dig 命令使用方法
ref:https://www.imooc.com/article/26971?block_id=tuijian_wz dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 D ...
- 一脸懵逼学习Hadoop分布式集群HA模式部署(七台机器跑集群)
1)集群规划:主机名 IP 安装的软件 运行的进程master 192.168.199.130 jdk.hadoop ...
- CSS3 伸缩布局盒模型
CSS3引入的布局模式Flexbox布局,主要思想是让容器有能力让其子项目能够改变其宽度,高度,以最佳方式填充可用空间.Flex容器使用Flex项目可以自动放大与收缩,用来填补可用的空闲空间.更重要的 ...
- 【译】理解JavaScript闭包——新手指南
闭包是JavaScript中一个基本的概念,每个JavaScript开发者都应该知道和理解的.然而,很多新手JavaScript开发者对这个概念还是很困惑的. 正确理解闭包可以帮助你写出更好.更高效. ...
- 【技巧汇总】eclipse中如何跳转到指定行
技巧汇总 持续更新ing eclipse中如何跳转到指定行 ctrl+L
- web实现下拉列表多选加搜索
实现如图所示的下拉多选还能带有搜索功能. <!DOCTYPE html> <html> <head> <title></title> < ...
- BZOJ3286 Fibonacci矩阵 矩阵 快速幂 卡常
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3286 题意概括 n,m,a,b,c,d,e,f<=10^1000000 题解 神奇的卡常题目 ...
- F. Shovels Shop 背包DP
题意: 商店里有n把铲子 每个铲子有其标价 一个人要买k吧 有m个优惠政策 每个优惠政策有两个元素x,y 表示 正好买x个铲子的时候 这x个铲子中最便宜的y个铲子免单 求用最少的前买到k个铲子 ...
- day9文件操作---从即日起时景丽阳老师给我们讲课
今天的主要内容是文件的操作,读,写,只读只写,或者可读同时可写,追加写,以二进制的方式读,以二进制的方式写,以二进制的方式追加. 思维导图: 给你一个文件路径,从中找出所有的文件,方法如下: # 方法 ...
- 2501 矩阵距离 (bfs)
描述 给定一个N行M列的01矩阵 A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i-k|+|j-l| 输出一个N行M列的整数矩阵B,其 ...