1. 程式人生 > >凸包 Codeforces605C Freelancer's Dreams

凸包 Codeforces605C Freelancer's Dreams

題意:有n份工作,第i份工作做1個單位時間獲得ai經驗和bi錢,現在要湊齊p點經驗和q錢,至少要工作多久,工作的時間可以為小數

思路:仔細想想總覺得哪裡不對勁,是不是感覺好像只需要1份工作或者2份工作的組合就能形成最優的?

事實就是這樣的,想達到最優最多隻需要選擇2份工作就行了,那麼問題就是怎麼枚舉出這2份工作。

現在我們考慮單位時間內能完成的內容

我們假設向量V1(a1,b1),V2(a2,b2)....Vn(an,bn),現在要求這n個向量線性組合,且每個向量前面的係數之和<=1

我們把這些向量的約束畫到草稿紙上,發現能達到的地方恰好形成了一個凸包!!

另外,因為我們並不是要求向量線性組合後要恰好等於 (p,q),而是應該在(p,q)的右上方,所以我們其實可以在凸包上處理一下,把凸包向左向下擴充套件到x軸和y軸,這樣形成的一個凸多邊形,然後再列舉每一條邊相鄰的兩個向量組合而成最少需要多少時間能達到要求就行了。

然後就是,可能只做1份工作是最優的,那麼可以直接單獨考慮

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;

const int MX = 1e5 + 10;
const int INF = 1e6;
const double eps = 1e-9;

struct Point {
    double x, y;
    bool operator<(const Point&b) const {
        if(x == b.x) return y < b.y;
        return x < b.x;
    }
    Point() {}
    Point(double _x, double _y) {
        x = _x; y = _y;
    }
} P[MX], R[MX];

double cross(Point a, Point b, Point c) {
    return ((b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y));
}

int convex(int n) {
    int m = 0, k;
    sort(P, P + n);
    for(int i = 0; i < n; i++) {
        while(m > 1 && cross(R[m - 1], P[i], R[m - 2]) <= eps) m--;
        R[m++] = P[i];
    }

    k = m;
    for(int i = n - 2; i >= 0; i--) {
        while(m > k && cross(R[m - 1], P[i], R[m - 2]) <= eps) m--;
        R[m++] = P[i];
    }
    if(n > 1) m--;
    return m;
}

int n, rear;
Point p;

double solve() {
    double ret = INF;
    for(int i = 0; i < n + 3; i++) {
        if(fabs(P[i].x) > eps && fabs(P[i].y) > eps){
            ret = min(ret, max(p.x / P[i].x, p.y / P[i].y));
        }
    }
    for(int i = 1; i < rear - 1; i++) {
        double a = R[i].x, b = R[i].y, c = R[i + 1].x, d = R[i + 1].y;
        double x = (a * p.y - b * p.x) / (a * d - b * c), y = (c * p.y - d * p.x) / (c * b - a * d);
        if(x < -eps || y < -eps) continue;
        ret = min(ret, x + y);
    }
    return ret;
}

int main() {
    //FIN;
    while(~scanf("%d%lf%lf", &n, &p.x, &p.y)) {
        double max1 = 0, max2 = 0;
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf", &P[i].x, &P[i].y);
            max1 = max(max1, P[i].x);
            max2 = max(max2, P[i].y);
        }
        P[n] = Point(max1, 0);
        P[n + 1] = Point(0, max2);
        P[n + 2] = Point(0, 0);
        rear = convex(n + 3);
        printf("%.15f\n", (double)solve());
    }
    return 0;
}