Kattis mobilization —— 凸包+三分

This way

題意:

有n種士兵,每個士兵有一個花費和血量和效力,你現在有b塊錢買士兵,(甚至是小數個人)你得到的價值是所有士兵的血量和乘上所有士兵的效力和。問你最大價值是多少。

題解:

我沒想到凸包。。然後就一直不過
可以將其轉化爲二維平面的問題:
首先我們將所有的花費都變爲b,這樣統一花費之後就是算取的比率的問題了
在這裏插入圖片描述
如果3在1,2的連線及以下,那麼1,2能夠組成的矩形的情況一定比1,3的大(因爲他們能夠組成的情況就是這條連線)。那麼如果有一個點4在1,2連線的上面,那麼1,2聯線必定不如1,4和4,2連線。所以做出凸包,然後三分算比率即可。

#include<bits/stdc++.h>
using namespace std;
#define ld long double
const int N=3e4+5;
const double eps=1e-8;
int desc(ld a,ld b){
    ld d=a-b;
    if(-eps<=d&&d<=eps)
        return 0;
    if(d<-eps)
        return -1;
    return 1;
}
struct node{
    ld h,w;
    bool operator< (const node& a)const
    {
        if(desc(w,a.w)==0)
            return h>a.h;
        return w<a.w;
    }
}p[N];
ld check2(ld r,int x,int y){
    return (r*p[x].h+(1-r)*p[y].h)*(r*p[x].w+(1-r)*p[y].w);
}
ld check1(int x,int y){
    ld l=0,r=1,lm,rm,ans;
    while(r-l>=eps){
        lm=(l*2+r)/3;
        rm=(l+r*2)/3;
        if(check2(lm,x,y)<check2(rm,x,y))
            l=lm;
        else
            r=rm;
    }
    return check2(lm,x,y);
}
int dcmp(ld x){return fabs(x)<1e-8?0:(x>0?1:-1); }
bool In(node a,node b,node c){
    node A={c.h-b.h,c.w-b.w},B={a.h-b.h,a.w-b.w};
    return dcmp(B.h*A.w-A.h*B.w)>=0;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    ld b,c;
    cin>>n>>b;
    for(int i=1;i<=n;i++){
        cin>>c>>p[i].h>>p[i].w;
        p[i].h=p[i].h*b/c;
        p[i].w=p[i].w*b/c;
    }
    sort(p+1,p+1+n);
    int all=0;
    for(int i=1;i<=n;i++){
        while(all>=2&&In(p[all-1],p[all],p[i])){
            all--;
        }
        p[++all]=p[i];
    }
    ld ans=0;
    for(int i=1;i<all;i++){
        ans=max(ans,check1(i,i+1));
    }
    cout<<fixed<<setprecision(10)<<ans<<endl;
    return 0;
}
/*
3 100
100 1 0.1
100 0.8 0.2
100 0.5 0.5

3 10
1 0 0
2 0 0
3 0 0

*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章