Topcoder SRM 283 Div1 300

题意:二维空间中给n个点,求一条直线(直线只可平行于x轴或y轴或两条对角线),使得最多的点到该直线距离不超过D,返回最大数量值。n不超过50

解法:设直线为ax+by+c=0,将每个点和两两点的中点分别作为关键点,枚举每个关键点,再枚举四条过关键点的直线,求出到该直线距离不超过D的点数量。维护数量最大值即可。复杂是 O(n3)

Code

#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;

double a[] = {0,1,1,1};
double b[] = {1,0,1,-1};
double sq(double x){return x * x;}

class PowerSupply{
public:
    int maxProfit(vector <int> x, vector <int> y, int D){
        double d = D;
        int ans = 0;
        for(int i = 0; i < x.size(); i++)
        {
            for(int j = 0; j < 4; j++)
            {
                double c = -(a[j] * x[i] + b[j] * y[i]);
                int ans2 = 0;
                for(int k = 0; k < x.size(); k++)
                {
                    if(sq(a[j]*x[k] + b[j]*y[k] + c) <= sq(d) * (sq(a[j]) + sq(b[j]))) ans2++;
                }
                ans = max(ans, ans2);
            }
        }
        for(int i = 0; i < x.size(); i++)
        {
            for(int j = i + 1; j < x.size(); j++)
            {
                double x1 = (x[i] + x[j]) * 0.5, y1 = (y[i] + y[j]) * 0.5;
                for(int k = 0; k < 4; k++)
                {
                    double c = -(a[k] * x1 + b[k] * y1);
                    int ans2 = 0;
                    for(int l = 0; l < x.size(); l++)
                    {
                        if(sq(a[k]*x[l] + b[k]*y[l] + c) <= sq(d) * (sq(a[k]) + sq(b[k]))) ans2++;
                    }
                    ans = max(ans, ans2);
                }
            }
        }
        return ans;
    }
}tt;

你可能感兴趣的:(Topcoder SRM 283 Div1 300)