nowcoder SG3 圆周上两点间的距离
严重怀疑这题是不是有问题。
第一组数据 0.00000038 和 179.00032609 差最大为179.00032571
但答案给出为179.00021342??
好!反正我早不搞比赛了(躺~
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#define DEBUG
using namespace std;
#ifdef DEBUG
double minone = -1;
double maxone = -1;
#endif
int main() {
int n;
scanf("%d", &n);
vector<double> nums;
set<double> sts;
for(int i = 0; i < n; ++i) {
double num;
scanf("%lf", &num);
sts.insert(num);
}
n = sts.size();
nums.reserve(n);
for (auto i = sts.begin(); i != sts.end(); ++i)
nums.push_back(*i);
// sts.clear();
double maxDis = 0.0;
sort(nums.begin(), nums.end());
//220315_DXM_如果nums[i]大于180,满足条件的必然是0~180部分的值,即已经重复了
for (int i = 0; nums[i] < 180 && i < n - 1; ++i) {
double target = nums[i] + 180;
//220315_DXM_greater寻找第一个大于target的数,smaller寻找第一个小于等于terget的数
// auto gt_index = upper_bound(nums.begin(), nums.end(), target);
auto index = lower_bound(nums.begin(), nums.end(), target);
if (index == nums.end()) continue;
double gt_value = *index - nums[i] > 180 ?
360 + nums[i] - *index :
*index - nums[i];
#ifdef DEBUG
if (gt_value > maxDis) {
maxDis = gt_value;
minone = nums[i];
maxone = *index;
}
#else
maxDis = max(gt_value, maxDis);
#endif
}
reverse(nums.begin(), nums.end());
for (int i = n - 1; nums[i] < 180 && i > 0;--i) {
double target = nums[i] + 180;
auto index = lower_bound(nums.begin(), nums.end(), target, greater<double>());
if (index == nums.end()) continue;
double le_value = *index - nums[i] > 180 ?
360 + nums[i] - *index :
*index - nums[i];
#ifdef DEBUG
if (le_value > maxDis) {
maxDis = le_value;
minone = nums[i];
maxone = *index;
}
#else
maxDis = max(le_value, maxDis);
#endif
}
printf("%.8lf\n", maxDis);
#ifdef DEBUG
printf("MinOne:%.8lf\nMaxOne:%.8lf\n", minone, maxone);
#endif
// printf("%ld", sts.size());
return 0;
}