zoukankan      html  css  js  c++  java
  • Codeforces 828D High Load

    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.

     

      直接贪心,目标是形成一个"菊花图",中间一个点,然后其它点周围一圈一圈地"围"着它。

      比如当n = 7, k = 3时,形成下面的一棵树

      计算的时候稍微判断一下n模k的余数(当被整除、余1和余数大于2的结果是不同的)然后特判一下就好了(详情可以看代码)。

    Code

     1 /**
     2  * Codeforces
     3  * Problem#828D
     4  * Accepted
     5  * Time:78ms
     6  * Memory:2052k
     7  */
     8 #include <iostream>
     9 #include <cstdio>
    10 #include <ctime>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <cstring>
    14 #include <cstdlib>
    15 #include <fstream>
    16 #include <sstream>
    17 #include <algorithm>
    18 #include <map>
    19 #include <set>
    20 #include <stack>
    21 #include <queue>
    22 #include <vector>
    23 #include <stack>
    24 #ifndef WIN32
    25 #define Auto "%lld"
    26 #else
    27 #define Auto "%I64d"
    28 #endif
    29 using namespace std;
    30 typedef bool boolean;
    31 const signed int inf = (signed)((1u << 31) - 1);
    32 const signed long long llf = (signed long long)((1ull << 61) - 1);
    33 const double eps = 1e-6;
    34 const int binary_limit = 128;
    35 #define smin(a, b) a = min(a, b)
    36 #define smax(a, b) a = max(a, b)
    37 #define max3(a, b, c) max(a, max(b, c))
    38 #define min3(a, b, c) min(a, min(b, c))
    39 template<typename T>
    40 inline boolean readInteger(T& u){
    41     char x;
    42     int aFlag = 1;
    43     while(!isdigit((x = getchar())) && x != '-' && x != -1);
    44     if(x == -1) {
    45         ungetc(x, stdin);    
    46         return false;
    47     }
    48     if(x == '-'){
    49         x = getchar();
    50         aFlag = -1;
    51     }
    52     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
    53     ungetc(x, stdin);
    54     u *= aFlag;
    55     return true;
    56 }
    57 
    58 int n, k;
    59 
    60 inline void init() {
    61     readInteger(n);
    62     readInteger(k);
    63 }
    64 
    65 inline void solve() {
    66     int temp = (n - 1);
    67     int res = temp / k;
    68     if(temp % k == 1)    res = res * 2 + 1;
    69     else if(temp % k >= 2)    res = res * 2 + 2;
    70     else res = res * 2;
    71     printf("%d
    ", res);
    72     for(int i = 2; i <= k + 1; i++)
    73         printf("1 %d
    ", i);
    74     for(int i = k + 2; i <= n; i++) {
    75         printf("%d %d
    ", i - k, i);
    76     }
    77 }
    78 
    79 int main() {
    80     init();
    81     solve();
    82     return 0;
    83 }
  • 相关阅读:
    每天一个linux命令(31): /etc/group文件详解
    每天一个linux命令(15):tail 命令
    每天一个linux命令(30): chown命令
    每天一个linux命令(22):find 命令的参数详解
    每天一个linux命令(23):Linux 目录结构
    每天一个linux命令(17):whereis 命令
    每天一个linux命令(18):locate 命令
    每天一个linux命令(34):du 命令
    每天一个linux命令(25):linux文件属性详解
    ThinkPad E420功能键和快捷键 以及 Fn和Ctrl键功能切换的设置,插入鼠标后, 关闭触摸板
  • 原文地址:https://www.cnblogs.com/yyf0309/p/7156553.html
Copyright © 2011-2022 走看看