Codeforces Round #423 (Div. 2) D. High Load(构造题)

题目链接:Codeforces Round #423 (Div. 2) D. High Load

题意:

给你一个数n和k,让你构造出一颗树,有k个叶子节点,使得这棵树的任意两个点的距离的最大值最小。

题解:

显然要使得这棵树的任意两个点的距离的最大值最小,每个点离树根越近越好。

然后要求有k个叶子节点,所以我就任意选一个点为根,然后构成一棵m叉的树就行了。

最大距离的最小值就是离根最远的和最近的加一加就行了。

 1 #include<cstdio>
 2 #define F(i,a,b) for(int i=a;i<=b;++i)
 3
 4 const int N=2e5+7;
 5 int n,m,a[N];
 6
 7 int main()
 8 {
 9     scanf("%d%d",&n,&m);
10     int now=1;
11     F(i,2,n)
12     {
13         if(i<=m+1)a[i]=1;
14         else a[i]=i-m;
15     }
16     int ans;
17     if((n-1)%m==0)ans=(n-1)/m*2;
18     else if((n-1)%m==1)ans=((n-1)/m*2)+1;
19     else ans=((n-1)/m*2)+2;
20     printf("%d\n",ans);
21     F(i,2,n)printf("%d %d\n",i,a[i]);
22     return 0;
23 }

时间: 2024-10-11 06:22:37

Codeforces Round #423 (Div. 2) D. High Load(构造题)的相关文章

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #260 (Div. 2) A. Laptops(简单题)

题目链接:http://codeforces.com/problemset/problem/456/A A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day Dima and Alex had an argument about the price and quality of laptops.

Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/482/problem/A Description Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct posi

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)D. High Load

题意:出n个点,其中k个叶子节点,问构造出的树最远的两个点最近是多少 思路:以一个点为中心,然后m个伸出,一层层扩散,(n-1)%m==k,如果k==0,即可以平分,长度就是2*(n-1)/m,如果取模为k==1,说明多出一个,+1,其他的话,就是最后一层补k个,但是最长的还是+2 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 int n,m; 6 cin>>n>>m; 7 int

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 it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seate

Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)

A.String Reconstruction B. High Load C. DNA Evolution 题意:给定一个只包含A,T,C,G的字符串S,有如下两种操作 1)修改一个点的字母. 2)给定一个字符串e ($\left | e \right |\leq 10$),生成一个由e重复组成的新串,eee...,问$S_{l..r}$中有几个字母跟这个新的字符串一一对应. SOL:对于每个字母,用BIT[x][y][L]表示$S_{1..L}$中,所有$\equiv x\left (mod

Codeforces Round #423 (Div. 2) A-C

A. Restaurant Tables 这里看错题意还wa了两发.... 按题意模拟就行了 水题 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #includ

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) B. Black Square

题意:问是否可以形成一个全黑正方形 思路:可以找出正方形的边,然后判断下这个矩阵是否容得下,n,m都比边短,比赛的时候写麻烦了,还去找了这个正方形究竟在哪个位置,这样的话得考虑很多情况,不如就边*边-黑子的总数 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=2e5+10; 5 char a[102][102]; 6 int main(){ 7 int n,m; 8 s