Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

A bit Greedy can achieve O(m) - the mid station between 2 adjacent cities has the longest distance within that range.

#include <vector>
#include <iostream>
using namespace std;


int main(){
    int n;
    int m;
    cin >> n >> m;
    vector<int> c(m);    
    for(int c_i = 0;c_i < m;c_i++){
       cin >> c[c_i];
    }
    sort(c.begin(), c.end());
    int r = 0;
    for(int i =0; i < m - 1; i ++)
    {
        r = max(r, (c[i + 1] - c[i])/2);
    }
    r = max(r, c[0]);
    r = max(r, n - c.back() - 1);
    cout << r << endl;
    
    return 0;
}
posted on 2016-08-07 13:53  Tonix  阅读(267)  评论(0编辑  收藏  举报