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

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,k;
  4. vector<pair<int,int> >ans;
  5. int main(){
  6. scanf("%d%d",&n,&k);
  7. n=n-1;
  8. int Ans=n/k*2+(n%k>0)+(n%k>1);
  9. int p=0;
  10. for(int i=0;i<n-n%k;i++){
  11. if(i%(n/k)==0)
  12. ans.push_back(make_pair(i+1,n+1));
  13. else
  14. ans.push_back(make_pair(i,i+1));
  15. if(i%(n/k)==0)p=p+1;
  16. }
  17. int step =n/k-1;
  18. for(int i=n-n%k;i<n;i++){
  19. // cout<<p<<endl;
  20. if(p<k){
  21. p=p+1;
  22. ans.push_back(make_pair(step,i+1));
  23. }else{
  24. ans.push_back(make_pair(step+1,i+1));
  25. }
  26. step+=n/k;
  27. }
  28. cout<<Ans<<endl;
  29. sort(ans.begin(),ans.end());
  30. for(int i=0;i<ans.size();i++){
  31. cout<<ans[i].first<<" "<<ans[i].second<<endl;
  32. }
  33. }

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

    题解: 首先有一个比较显然的事情是如果我们确定了买的次数这道题就可以简单的贪心了 但是答案和买的次数是什么关系呢.. 好像是可以三分的 所以应该是单峰的 这里用了模拟退火,而且是没有处理失败情况的模拟 ...

  3. pycharm创建python模板文件

    1.新建一个项目: 2.右键单击项目名称-->选择新建-->编辑模板文件 3.编辑模板文件保存 4.新建文件测试 至此不再重复添加头部信息了

  4. Docker镜像、容器剖析

    我们通常所说的docker是什么? 在这里英文本意为“搬运工”这里指的的docker搬运点的是集装箱,集装箱装的是够任意类型的APP,开发者通过Docker可以将app变成一种标准化,可移植的.自管理 ...

  5. 初识Python.day2

    一. python运算符 算数运算符  比较运算符 赋值运算符 逻辑运算符 成员运算符 二. Python基础数据类型 1. python字符串总结 # 首字母变大写 # a1 = "jiu ...

  6. pycharm中出现的错误

    错误1 在pycharm上安装TensorFlow运行import tensorflow时报错: File , in _find_and_load SystemError: PyEval_EvalFr ...

  7. html ie

    <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content=" ...

  8. 配置windows批处理脚本(.bat)文件路径 | window批处理文件配置执行路径

    以PHP的composer为例 1 . 路径D:\phpStudy\php\php-5.5.38加入环境变量 2.在路径D:\phpStudy\php\php-5.5.38新建composer.bat ...

  9. BZOJ4566 [Haoi2016]找相同字符 字符串 SAM

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4566.html 题目传送门 - BZOJ4566 题意 给定两个字符串 $s1$ 和 $s2$ ,问有 ...

  10. 数学模型:3.非监督学习--聚类分析 和K-means聚类

    1. 聚类分析 聚类分析(cluster analysis)是一组将研究对象分为相对同质的群组(clusters)的统计分析技术 ---->> 将观测对象的群体按照相似性和相异性进行不同群 ...