Ural_1207. Median on the Plane(计算几何)

  /*思路完全搞乱,开始就没想清楚就写。我晕,各种WA。

思路:

  找到所有点中最下边一层点里边最靠左的点d。然后求d到其他每个点连线与x轴的
夹角Θ(0 <= Θ <= π 因为d的纵坐标最小)。然后从小到大排序,找到存角度的数组
里n/2号点就是要找的另一个点。
*/

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define pi 3.1415926535

using namespace std;

const int N = 10010;

struct point{
double x;
double y;
}p[N];

struct angle{
double ang;
int i;
}a[N];

point tmp;

bool cmp(angle a, angle b){
return a.ang < b.ang;
}
int main(){
//freopen("data.in", "r", stdin);

int n, i, d, j;
double b, c;

scanf("%d", &n);
scanf("%lf%lf", &p[1].x, &p[1].y);
b = p[1].x; c = p[1].y; d = 1;

for(i = 2; i <= n; i++){
scanf("%lf%lf", &p[i].x, &p[i].y);
if(c > p[i].y){
b = p[i].x; c = p[i].y; d = i;
}
if(c == p[i].y && b > p[i].x){
b = p[i].x; c = p[i].y; d = i;
}
}

for(j = 1, i = 1; i <= n; i++){
if(i != d){
a[j].i = i;
b = p[i].x - p[d].x;
c = p[i].y - p[d].y;
if(b == 0) {a[j++].ang = pi/2; continue;}
if(c == 0) {a[j++].ang = 0; continue;}
if(p[i].x < p[d].x)
a[j++].ang = pi - atan(c/b);
else
a[j++].ang = atan(c/b);
}
}
sort(a+1, a+n, cmp);
/*for(i = 1; i < n; i++){
printf("%lf %d\n", a[i].ang, a[i].i);
}
*/
printf("%d %d\n", d, a[n/2].i);
return 0;
}

你可能感兴趣的:(media)