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. react-native-video

    <Video ref='videoPlayer' source={{uri:'http://www.thinktown.com/video/th.mp4'}} //网络视频 //source={ ...

  2. [转]git 删除远程仓库文件

    来源:https://www.jianshu.com/p/de75a9e3d1e1 git删除远程文件夹或文件的方法 项目开发初期由于.gitignore 文件配置不正确很有可能导致某些不需要的目录上 ...

  3. [转]PyCharm安装及使用

    https://www.jianshu.com/p/042324342bf4 PyCharm 搭建环境 1.win10_X64,其他Win版本也可以. 2.PyCharm版本:Professional ...

  4. python函数式编程——返回函数

    1.函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 2.闭包 注意到返回的函数在其定义内部引用了局部变量args,所以,当一个函数返回了一个函数后,其内部的局部变量还 ...

  5. MySQL主从数据同步延时分析

    一.MySQL数据库主从同步延迟                                                              要了解MySQL数据库主从同步延迟原理,我们 ...

  6. mysql特殊使用

    1.按照 job 和薪水倒序排序: select ename,job,sal from emp order by job desc,sal desc; 2.substr()截取子串 该函数接收3个参数 ...

  7. Codeforces 639D Bear and Contribution

    Bear and Contribution 对于对于5余数为, 0, 1, 2, 3, 4的分别处理一次, 用优先队列贪心. #include<bits/stdc++.h> #define ...

  8. 普林斯顿微积分读本 大纲与重点 (by zzd)

    普林斯顿微积分读本 大纲重点 由于博客园太菜,所以我用图片上传. 当前更新状态:未完待续,挖坑暂时不填了. UPD(2018-07-08): 稍微更一下,加一个本书的某一版本下载链接:https:// ...

  9. 20165235 实验二Java面向对象程序设计

    20165235 Java面向对象程序设计 姓名:祁瑛 学号:20165235 班级:1652 实验课程:JAVA程序设计 实验名称:Java面向对象程序设计 实验时间:2018.4.14 指导老师: ...

  10. Fire! -两次dfs

    题目描述: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...