CodeForces - 827B High Load(贪心+思维)
2017-09-12 22:57
429 查看
题目大意:
给你n个结点,其中有k个结点是出入结点,让你把这n个结点连出一个树,使得最远的两个出入结点的距离最近,并保证所有度为1的结点为出入结点。分析:
我只要贪心地从根节点引出k条边,每条边再往下延伸,并使得每个叶子节点的深度差不超过1,就可以了。高中数学老师说过,多想一点,就能少写一点!代码:
#include<bits/stdc++.h> using namespace std; int n,k; int main() { scanf("%d%d",&n,&k); n--; int len1=n/k; int len2=(n%k)?(len1+1):len1; int ans=(n%k>=2)?(len2*2):(len1+len2); printf("%d\n",ans); n++; for(int i=2;i<=k;i++) { printf("%d %d\n",1,i); } for(int i=k+1;i<=n;i++) { printf("%d %d\n",i-k,i); } }
相关文章推荐
- codeforces-500【B思维】【C贪心】
- Codeforces 349B Color the Fence【贪心+思维】
- Codeforces 798C Mike and gcd problem【思维+贪心】好题!
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- Codeforces 521A DNA Alignment【贪心+思维】
- codeforces-486【C-贪心-思维】【D-树状DP】
- Codeforces-540B-School Marks(思维贪心)
- CodeForces 707B Bakery(思维+贪心)
- Codeforces 918C - The Monster (贪心 + 思维 + 栈模拟)
- 【CodeForces】337C - Quiz(贪心,思维)
- 【CodeForces - 798C Mike and gcd problem】 思维+贪心
- 【CodeForces】---Party Lemonade(贪心&&思维&&递归)
- Codeforces 534B Covered Path【有点奇怪的思维+贪心】
- CodeForces - 808C-贪心-思维
- codeforces-337【C思维,贪心】
- Codeforces 478C Table Decorations 贪心(思维!!!)
- Codeforces 500C:New Year Book Reading(思维+贪心?)
- Codeforces 349 B Color the Fence (思维+贪心)
- codeforces 913 C. Party Lemonade【思维贪心】
- CodeForces 798D Mike and distribution(二维贪心 + 思维)