ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1. Nuclear Reactor(二分)

2021-11-24 20:36:10  阅读:208  来源: 互联网

标签:二分 plants Reactor int Nuclear ll ++ output input


Problem - 1 - Codeforces

1. Nuclear Reactor time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Dream Land has nn nuclear reactor plants. Each of the plants is positioned on a straight line pipi (1≤pi≤10181≤pi≤1018) At the beginning, all plants are inactive. The president of Dream Land wants to activate exactly kk (1≤k≤n1≤k≤n) plants in such away the distance between any two active plants is as large as possible. The president assigned this task to you.

Input

The first line of input consist of two integers nn (1≤n≤1051≤n≤105) and kk (1≤k≤n1≤k≤n), the number of nuclear plants and the number of activated plants respectively. The second line consist of nn integers which are the positions of the nuclear reactor plants pipi (1≤pi≤10181≤pi≤1018).

Output

The output consist of exactly kk integers the positions of activated plants such as the distance between any two activated plants is as large as possible. If there are multiple answers, print any of them.

Example input
5 3
4 5 11 8 10
output
5 8 11

 

 题意:一条直线上排列了n个核电站,现在要启动其中的k个。要求k个核电站中任意两个距离最大(注意这里的任意,可把我坑死了),如果有多种可能输出其中一种

一开始被这个任意(原文为any)迷惑了,看着样例不知所措。

 

 也就是说只要最小间隔最大,则满足题意。剩下的就是二分了,附代码。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define TL ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
 5 
 6 int n,k;
 7 ll a[100009];
 8 vector<ll>ans;
 9 
10 bool check(ll x)
11 {
12     ll end = -1e18-1;
13     int cnt = 0;
14     ans.clear();
15     for(int i = 0;i < n;i++)
16     {
17         if(a[i] - end >= x)
18         {
19             cnt++;
20             ans.push_back(a[i]);
21             end = a[i];
22         }
23     }
24     return cnt >= k;
25 }
26 int main()
27 {
28     TL;
29     cin >> n >> k;
30     for(int i = 0;i < n;i++) cin >> a[i];
31     sort(a,a+n);
32     ll r = 0,l = 1e18,mid;
33     while(r<l)
34     {
35         mid = (r + l + 1) >> 1;
36         if(check(mid)) r = mid;
37         else l = mid - 1;
38     }
39     check(r);
40     for(int i = 0;i < k;i++)
41     {
42         cout << ans[i] << " ";
43     }
44     return 0;
45 }

 

标签:二分,plants,Reactor,int,Nuclear,ll,++,output,input
来源: https://www.cnblogs.com/wbw1537/p/15599914.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有