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 构造的更多相关文章

  1. 【构造】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) B. High Load

    让你构造一棵树(给定了总结点数和总的叶子数),使得直径最小. 就先弄个菊花图(周围一圈叶子,中间一个点),然后平均地往那一圈放其他的点即可. #include<cstdio> using ...

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

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

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

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

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

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

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

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

随机推荐

  1. Git基础(二) 文件的生命周期

    使用Git时,文件的生命周期如下:

  2. 用vi编辑文件

    原文:https://www.ibm.com/developerworks/library/l-lpic1-103-8/index.html Overview In this article, lea ...

  3. C#学习-类和结构

    类和结构体,对两者进行比较 语法上的区别在于,定义类要使用关键字class,而定义结构体则使用关键字struct; 结构体中不可对声明字段进行初始化,但类可以: 如果没有为类显式地定义构造函数,C#编 ...

  4. 去除ArrayList集合中的重复自定义对象元素

    要求去除ArrayList集合中重复的Student的对象(什么叫重复,所有属性值都相同叫做重复). 思路: 1.创建一个新集合 2.遍历旧集合中的每一个元素,去新集合中找这个元素,如果这个元素不存在 ...

  5. Adjoint operators $T_K$ and $T_{K^{*}}$ in BEM

    In our last article, we introduced four integral operators in the boundary integral equations in BEM ...

  6. 【Android】setHapticFeedbackEnabled 设置

    使其在触摸的时候没有触感反馈.接着设置长按事件的监听. 代码在:launcher launcher->setupViews方法 // Setup the workspacemWorkspace. ...

  7. Python_生成器generator

    生成器:调用时返回一个迭代器 如果一个函数中包含yield语法,那这个函数就会变成一个生成器 例1: def draw_money(draw): #这个函数称为生成器 while draw >0 ...

  8. 强大的图片展示插件,JQuery图片预览展示插件

    只需要引入JQuery.js , viewer.css 和 viewer.js <!DOCTYPE html> <html lang="en"> <h ...

  9. Machine Learning 算法可视化实现2 - Apriori算法实现

    目录 关联分析 Apriori原理 Apriori算法实现 - 频繁项集 Apriori算法实现 - 从频繁项集挖掘关联规则 一.关联分析 关联分析是一种在大规模数据集中寻找有趣关系的任务. 这些关系 ...

  10. 有了这些,java IO就不愁了

    IO的总结: java中相对路径和绝对路径的问题: 在web项目中,如果生成的文件前面没有 / 开头的话,表示的是生成的文件在当前项目的根目录下如student.txt在项目中刷新就能看到. 如果是以 ...