其他分享
首页 > 其他分享> > 1. Nuclear Reactor(二分)

1. Nuclear Reactor(二分)

作者:互联网

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