codeforces 828 D. High Load(思维,水题)

题目链接:http://codeforces.com/contest/828/problem/D

 

题解:任意去一个点为根然后有几个k就是几个子叶也就是根结点有几个分支然后最好的解法就是贪心,将剩下的点均匀的分到各个分支上就行具体看一下代码。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e5 + 10;
int main() {
    int n , k;
    scanf("%d%d" , &n , &k);
    int every = (n - k - 1) / k;
    int more = (n - k - 1) % k;
    int ad = 0;
    if(more == 0) ad = 0;
    else if(more == 1) ad = 1;
    else ad = 2;
    printf("%d\n" , every * 2 + 2 + ad);
    int cnt = 2 , pre = 1;
    for(int i = 1 ; i <= k ; i++) {
        pre = 1;
        for(int j = 1 ; j <= every + 1 ; j++) {
            cout << pre << ' ' << cnt << endl;
            pre = cnt++;
        }
        if(more) {
            more--;
            cout << pre << ' ' << cnt << endl;
            cnt++;
        }
    }
    return 0;
}
posted @ 2017-07-12 21:45  Gealo  阅读(176)  评论(0编辑  收藏  举报