Codeforces Round #386 (Div. 2)G. New Roads [构造][树]
题目链接:G. New Roads
题意:给出n个结点,t层深度,每层有a[i]个结点,总共有k个叶子结点,构造一棵树。
分析:
考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点
那么答案很简单,就是对(a[i]-1)求和再加1(每一层的结点都集中在上一层的一个结点上)
同理,我们考虑最少能有多少叶子结点,就是把上一个的答案再减去min(a[i]-1, a[i-1]-1)的求和,就是每一层的结点都尽可能的分散在上一层的结点
根据这个,那么如果要求有k个叶子节点,k在最大值与最小值之间,就可以生成出这棵树了
生成的方法,和构造起来差不多,就是先求出最大值与k的差T,然后每一层处理时,如果T>0,就使这一层尽可能的分散在上一层结点上,然后T减少一点
直到T为0,之后的层都直接集中在上一层的一个结点即可
详情见代码
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; int n,t,k,a[],s1,s2; int main()
{
scanf("%d %d %d",&n,&t,&k);
for(int i=;i<=t;++i) {scanf("%d",a+i);s1+=a[i];}s1-=t-;
s2=s1;for(int i=;i<=t;++i) s2-=min(a[i]-,a[i-]-);
if(s2<=k&&k<=s1)
{
printf("%d\n",n);
for(int i=;i<=a[]+;++i) printf("1 %d\n",i);//先处理1
int tot=+a[],cnt=s1-k;
for(int i=;i<=t;++i)
{
int tt=min(a[i]-,a[i-]-),cou=;//tt是第i深度形成的边数
while(cnt&&tt)
{
if(cou) {tt--,cnt--;}
printf("%d %d\n",tot-a[i-]+cou,tot+cou);//每次都+1
cou++;
}
if(cnt==||tt==)//若还有多余的边,则加到第一个结点
{
while(cou<a[i])
printf("%d %d\n",tot-a[i-],tot+cou++);
}
tot+=a[i];//结点序增加
}
}
else puts("-1");
}
Codeforces Round #386 (Div. 2)G. New Roads [构造][树]的更多相关文章
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 ht ...
- Codeforces Round #386 (Div. 2) C D E G
一场比较简单的题 比较脑洞 C 如果坐车比较快的话 先走不如等车 所以最后的ans是min(纯走路,纯坐车) 讨论一下坐车时间 D 因为k一定是>=1的 所以当a=b的时候 GBGBGB这样间隔 ...
- Codeforces Round #547 (Div. 3) G 贪心
https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams
http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...
- Codeforces Round #346 (Div. 2) G. Fence Divercity dp
G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...
- Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost(dijkstra算法)
题目链接:https://codeforces.com/contest/1433/problem/G 题解 跑 \(n\) 遍 \(dijkstra\) 得到任意两点间的距离,然后枚举哪一条边权为 \ ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
随机推荐
- CSS属性操作二
9.float属性 基本浮动规则 先来了解一下block元素和inline元素在文档流中的排列方式. block元素通常被现实为独立的一块,独占一行,多个block元素会各自新起一行,默认block元 ...
- 组队训练2 回放(转载至cxhscst2's blog)
2017/3/4 12:00-17:00 Solve 9 / 13 Penalty 717 练习赛过程回放: 开场5分中J题签到(cst) 12分钟时qw签到A 这时qw继续开写M,WA,检查代码. ...
- Multiply Strings(字符串乘法模拟,包含了加法模拟)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Vue基础学习
使用vue-cli构建初始化vue项目 vue init webpack myfirst 项目截图:(开发工具:webStorm) 主要练习了vue的基本指令:v-bind.v-if.v-show.v ...
- 干掉H5audio音频标签的下载按钮
audio::-internal-media-controls-download-button {display:none;}audio::-webkit-media-controls {overfl ...
- eclipse提速01 - 禁用不常用的eclipse启动插件
会不断更新,需要的收藏 禁用不常用的eclipse启动插件(当然如果不需要可以卸载)
- C# DataGridView,右键单击RowHeader时显示右键菜单怎么做?
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { ...
- sklearn特征工程总结
转自: http://www.cnblogs.com/jasonfreak/p/5448385.html https://www.zhihu.com/question/28641663/answer/ ...
- Working with Validators and Messages in AngularJS
原文:http://odetocode.com/blogs/scott/archive/2014/10/16/working-with-validators-and-messages-in-angul ...
- ubuntu16.04LTS安装软件
1.安装chrome 下载源加入到系统的源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/ ...