首页 > 代码库 > Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)D. High Load

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 sum;
 8     if((n-1)%m==0) sum=(n-1)/m*2;
 9     else if((n-1)%m==1) sum=(n-1)/m*2+1;
10     else sum=(n-1)/m*2+2;
11     cout<<sum<<endl;
12     for(int i=2;i<=m+1;i++) {
13       printf("1 %d\n",i);
14     }
15     for(int i=m+2;i<=n;i++){
16         printf("%d %d\n",i,i-m);
17     }
18     return 0;
19 }

 

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