Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
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.
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.
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.
3 2
2
1 2
2 3
5 3
3
1 2
2 3
3 4
3 5
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.

直接贪心,目标是形成一个"菊花图",中间一个点,然后其它点周围一圈一圈地"围"着它。
比如当n = 7, k = 3时,形成下面的一棵树

计算的时候稍微判断一下n模k的余数(当被整除、余1和余数大于2的结果是不同的)然后特判一下就好了(详情可以看代码)。
Code
/**
* Codeforces
* Problem#828D
* Accepted
* Time:78ms
* Memory:2052k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} int n, k; inline void init() {
readInteger(n);
readInteger(k);
} inline void solve() {
int temp = (n - );
int res = temp / k;
if(temp % k == ) res = res * + ;
else if(temp % k >= ) res = res * + ;
else res = res * ;
printf("%d\n", res);
for(int i = ; i <= k + ; i++)
printf("1 %d\n", i);
for(int i = k + ; i <= n; i++) {
printf("%d %d\n", i - k, i);
}
} int main() {
init();
solve();
return ;
}
Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心的更多相关文章
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- 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 i ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)
Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
随机推荐
- eclipse签名使用的key文件(android生成keystore)
命令行(或终端)生成keystore文件 在命令行(或终端)输入命令: keytool -genkey -alias Gallery.keystore -keyalg RSA -validit ...
- js执行上下文
js在执行是会有一个“准备工作”: 主要内容有 1.变量.函数表达式——>变量声明,默认赋值为undefined: 2.this——>赋值: 3.函数声明——>赋值: 这三种数据的准 ...
- linux网卡eth1如何修改为eth0
ifconfig看到的ip不是我想要的ip,而且显示的第一块网卡也是eth1 ,这明显是有问题的, vim /etc/sysconfig/network-script/ifcfg-eth0 看到的ip ...
- Unity shader学习之阴影
Unity阴影采用的是 shadow map 的技术,即把摄像机放到光源位置上,看不到的地方就有阴影. 前向渲染中,若一光源开启了阴影,Unity会计算它的阴影映射纹理(shadow map),它其实 ...
- 三 js语句
/** * Created by Administrator on 2017/12/24. * 1.顺序语句 * 2.选择语句 if else swtich case * 3.循环语句 for whi ...
- 如何让多个dz论坛共用一个用户数据库
用户数据库在论坛中是可以独立备份的,备份方法:论坛后台——站长——数据库,备份所有ucenter数据表,也就是用户数据.其他DZ论坛搭建完成以后,可以上传用户数据库,将备份文件使用上传至网站所使用的主 ...
- IIS8无法通过IP访问解决办法
今天配置在Windows server 2012 R2 上配置IIS8时,出现局域网内无法使用IP访问站点的问题,查找资料依然无法解决.最后发现IIS8配置好主机名后无法使用主机IP访问站点,只能使用 ...
- 【CDH学习之三】CDH安装
登录CM 1.版本选择 免费版本的CM5已经去除50个节点数量的限制. 各个Agent节点正常启动后,可以在当前管理的主机列表中看到对应的节点. 选择要安装的节点,点继续. 接下来,出现以下包名,说明 ...
- mysql 问题:连不上
问题描述: 客户端报错: MySQL Authentication plugin ‘caching_sha2_password’ cannot be loaded 解决方式: ALTER USER ' ...
- python 简单了解namedtuple
namedtuple类位于collections模块,有了namedtuple后通过属性访问数据能够让我们的代码更加的直观更好维护 namedtuple能够用来创建类似于元祖的数据类型,除了能够用索引 ...