Codeforces E. High Load(构造)
题目描述:
High Load
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.
Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.
Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.
Input
The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.
Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.
Output
In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.
If there are multiple answers, print any of them.
Examples
Input
Copy
3 2
Output
Copy
21 22 3
Input
Copy
5 3
Output
Copy
31 22 33 43 5
Note
In the first example the only network is shown on the left picture.
In the second example one of optimal networks is shown on the right picture.
Exit-nodes are highlighted.
思路:
题目是说给一个总的顶点数和特殊的顶点数,其中特殊的顶点只能连接另一个顶点,而其他顶点必须连至少两个顶点,现在要构造一种连接方式,使得特殊顶点距离的最大值最小。
刚开始看漏要求,以为可以成环,WA了后看清题目要是棵树,瞬间难度不在一个档次(虽然也不是太难),但我胡乱想也不知道要怎么构造。原来题目的思路是要把这棵树的没一个分支高度尽可能相等,也就是节点要尽可能平均,这样可以使距离最大值最小,如下:
然后就可以做了。先算出分摊下来每个分枝上至少有多少个节点,在算出余数就是要往分枝末端加上的节点。注意是当有一个节点多出来以后,两个特殊节点的最长距离加一;多两个节点后,由于这两个节点会加到不同分枝的末端,所以距离加二;而当多出节点数大于2,最长距离也是加二,因为多出的节点只加了不到一层节点,深度只增加一。
代码:
#include <iostream>
using namespace std;
int n,k;
int length;
int stem;
int main()
{
cin >> n >> k;
length = (n-1)/k*2;
stem = (n-1)/k;
int re = (n-1)%k;
if(re==0)
{
cout << length << endl;
//cout << "k " << k << endl;
for(int j = 2; j<n+1; j+=stem)
{
cout << 1 << " " << j << endl;
for(int z = j+1; z<j+stem; z++)
{
cout << z-1 << " " << z << endl;
}
}
}
if(re==1)
{
cout << length+1 << endl;
for(int j = 2; j<n; j+=stem)
{
cout << 1 << " " << j << endl;
for(int z = j+1; z<j+stem; z++)
{
cout << z-1 << " " << z << endl;
}
}
cout << n-1 << " " << n;
}
if(re==2)
{
//cout << "re " << re << " k " << k << endl;
cout << length+2 << endl;
for(int j = 2; j<n-1; j+=stem)
{
cout << 1 << " " << j << endl;
for(int z = j+1; z<j+stem; z++)
{
cout << z-1 << " " << z << endl;
}
}
cout << n-2 << " " << n-1 << endl;
cout << n-2-stem << " " << n << endl;
}
if(re>2)
{
//cout << "length " << length << endl;
cout << length+2 << endl;
for(int j = 2; j<n-re+1; j+=stem)
{
cout << 1 << " " << j << endl;
for(int z = j+1; z<j+stem; z++)
{
cout << z-1 << " " << z << endl;
}
}
for(int i = 0;i<re;i++)
{
cout << n-re-i*stem << " " << n-re+1+i << endl;
}
}
return 0;
}
Codeforces E. High Load(构造)的更多相关文章
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造
D. High Load 题目连接: http://codeforces.com/contest/828/problem/D Description Arkady needs your help ag ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
- Codeforces 549B. Looksery Party[构造]
B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
- codeforces 22C System Administrator(构造水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud System Administrator Bob got a job as a s ...
- Codeforces 353D Queue(构造法)
[题目链接] http://codeforces.com/contest/353/problem/D [题目大意] 10^6个男女排队,每一秒,如果男生在女生前面,即pos[i]是男生,pos[i+1 ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- [ An Ac a Day ^_^ ] CodeForces 468A 24 Game 构造
题意是让你用1到n的数构造24 看完题解感觉被样例骗了…… 很明显 n<4肯定不行 然后构造出来4 5的组成24的式子 把大于4(偶数)或者5(奇数)的数构造成i-(i-1)=1 之后就是无尽的 ...
随机推荐
- 【转】TCP/IP协议——ARP详解
本文主要讲述了ARP的作用.ARP分组格式.ARP高速缓存.免费ARP和代理ARP. 1.学习ARP前要了解的内容 建立TCP连接与ARP的关系 应用接受用户提交的数据,触发TCP建立连接,TCP的第 ...
- [LeetCode] 617. Merge Two Binary Trees 合并二叉树
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- Excel输入十六进制数,以及十六进制运算
网上复制来复制去的连个靠谱答案都没有...f**k 所以无奈自己探索出来了 先放效果图 文本值 转 进制值 单元格: A1文本值 A2进制值 输入内容: 'fefe =OCT2HEX(HEX2OCT( ...
- leetcode 674. 最长连续递增序列
1. 题目 给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3, ...
- 第22课 weak_ptr弱引用智能指针
一. weak_ptr的概况 (一)weak_ptr的创建 1. 直接初始化:weak_ptr<T> wp(sp); //其中sp为shared_ptr类型 2. 赋值: wp1 = sp ...
- SqlServer 通过日志恢复数据库
前期工作 查看数据属性,确保下条件: 1.数据库属性->选项->恢复模式=完整 2.建好库以后.一个数据库完整的数据备份 3.到出事期间日志没有你间断 4.记录出事的准确时间 一.数据准备 ...
- Teradata 数据库介绍
Teradata在整体上是按Shared Nothing 架构体系进行组织的,他的定位就是大型数据仓库系统,定位比较高,他的软硬件都是NCR自己的,其他的都不识别:所以一般的企业用不起,价格很贵.由于 ...
- 基础知识---委托和 lambda
委托定义类型,类型指定特定方法签名. 可将满足此签名的方法(静态或实例)分配给该类型的变量,然后(使用适当参数)直接调用该方法,或将其作为参数本身传递给另一方法再进行调用. 以下示例演示了委托的用法. ...
- ImageView的adjustViewBounds属性
adjustViewBounds属性的定义如下: 调整ImageView的边界,使得ImageView和图片有一样的宽高比 这个属性只有在ImageView一边如宽度或高度固定,一边为wrap_con ...
- 一、hexo+github搭建个人博客的过程记录
前提: 1.新建一个github仓库 2.安装配置Node.js 3.安装配置Git 前提 步骤1.新建一个github仓库 打开github网站,(注册)登录账号,新建一个仓库; 注:==仓库名称要 ...