#2343732第79个测试结果错误

Vagente edited 1 年,2 月前

答案是  499998143
我的输出是499998126
如果我的输出比答案大可能是我逻辑的问题,没有找到最优解。但我找了半天也没找出来为什么我的输出会比答案小。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int *houses;

int main(){
    int32_t n;
    cin >> n;
    houses = new int32_t[n];
    for(int i = 0; i < n; i++){
        cin >> houses[i];
    }

    sort(houses, houses + n);

    float midPoint = (houses[0] + houses[n - 1]) / (float) 2;
    int32_t closest;
    int32_t min = houses[n - 1];
    for(int i = 0; i < n; i++){
        float temp = abs(houses[i] - midPoint);
        if(temp < min){
            min = temp;
            closest = houses[i];
        }
    }

    if(closest >= midPoint){
        cout<<houses[n - 1] - closest;
    }else
        cout<<closest - houses[0];
    delete[] houses;
}

Comments