Codeforces 827B. High Load 【构造】

B. High Load
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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·1052 ≤ 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.

Examples
input
3 2
output
2
1 2
2 3
input
5 3
output
3
1 2
2 3
3 4
3 5
Note

In the first example the only network is shown on the left picture.

In the second example one of optimal networks is shown on the right picture.

Exit-nodes are highlighted.


---

显然 构图后,Exit-nodes必须连接到度为1的点上 使得那n-k个非Exit-nodes的点度>=2 

不难发现 度为1的点尽可能多,最远的2个Exit-nodes距离就越短

而Exit-nodes只有k,即度=1的点<=k个

选出一个中心点,给它k条边连接到其他点,形成一个周期为k的圆,不停绕着圆连点即可

#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<list>
#define ll long long
#define pii pair<int,int>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf=0x3f3f3f3f;
const int N = 2e5+5;
int num[N];//圆上i位置的链上的元素个数
int now[N];//现在圆上i位置上最外面的点是now[i]
int outD[N];//第i个Exit-nodes的深度
void slove(int n,int k){
    vector<pii>ans;
    MEM(num,0);
    int idx=0;
    int a=n-k;
    int center=a-1;
    for(int i=0;i<k;++i){
        now[i]=center;
    }
    int i;
    for(i=0;i<center;++i){
        ans.push_back({now[i%k],i});
        now[i%k]=i;
        ++num[i%k];
    }
    for(int j=0;j<k;++j){
        ans.push_back({now[(i+j)%k],i+1+j});
        now[(i+j)%k]=i+1+j;
        ++num[(i+j)%k];
        outD[j]=num[(i+j)%k];
    }
    sort(outD,outD+k,greater<int>());
    printf("%d\n",outD[0]+outD[1]);
    for(int i=0;i<ans.size();++i){
        printf("%d %d\n",ans[i].first+1,ans[i].second+1);
    }
}

int main()
{
    //freopen("/home/lu/code/r.txt","r",stdin);
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        slove(n,k);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值