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

本文提供了一种解决CodeForces 828/D问题的方法,通过选择任意节点作为根节点,并采用贪心策略将剩余节点均匀分配到各分支中,以达到最优解。文章附带了详细的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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;
}

转载于:https://www.cnblogs.com/TnT2333333/p/7157848.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值