1. 程式人生 > >不知道哪裡來的排序題

不知道哪裡來的排序題

不太清楚哪裡來的題……反正我管它叫變態排序題 在這裡插入圖片描述

題解是別人給的

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 1E7+10;
struct node{
	double left,right;
	double minn,maxn;
}seg[N];
double num[N];
const double inf = 0x3f3f3f3f3f3f3f;
int main(){
	int n;
	cin>>n;
	double
minn = inf; double maxn = -inf; int minid; int maxid; for(int i=0;i<n;i++){ scanf("%lf",&num[i]); if(maxn < num[i]){ maxn = num[i]; maxid = i; } if(minn > num[i]){ minn = num[i]; minid = i; } } // printf("%lf %lf\n",minn,maxn); double evl = (maxn - minn)/(double
)(n-1); // printf("%lf\n",evl); for(int i=1;i<=n-1;i++){ seg[i].left = minn; seg[i].right = minn + evl; minn += evl; seg[i].maxn = -inf; seg[i].minn = inf; } seg[1].left = minn; seg[n-1].right = maxn; maxn = evl; for(int i=0;i<n;i++){ int id = (num[i] - num[minid])/evl + 1; seg[
id].maxn = max(seg[id].maxn,num[i]); seg[id].minn = min(seg[id].minn,num[i]); } /* for(int i=1;i<=n-1;i++){ printf("%lf %lf\n",seg[i].minn,seg[i].maxn); } */ for(int i=2;i<n-1;i++){ if(seg[i].minn == inf){ seg[i].minn = seg[i].maxn = seg[i-1].maxn; } } /* for(int i=1;i<=n-1;i++){ printf("%lf %lf\n",seg[i].minn,seg[i].maxn); } */ for(int i=2;i<=n-1;i++){ maxn = max(maxn,seg[i].minn - seg[i-1].maxn); } printf("%lf\n",maxn); return 0; } /* 5 2.1 3.1 7.5 1.5 6.3 */