hdu 4957 贪心破木桶接水大trick

http://acm.hdu.edu.cn/showproblem.php?pid=4957

拿n只破的木桶去接水,每只木桶漏水速度为a[i],最后要得到b[i]单位的水,自来水的出水速度为V,木桶里只要有水就会漏水,每次只能接一只木桶。问最短需要多少时间,每只木桶里的水为b[i]
ans+=(p[i].b+ans*p[i].a)/(v-p[i].a) => ans+=p[i].b/(v-p[i].a)+(ans*p[i].a)/(v-p[i].a)
得到(ans*p[i].a)/(v-p[i].a)的值要由小到大排序
然后依次搞就行了

需要注意的是b==0的情况,需要的时间为0,bc上此题直接0AC...。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include<set>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
#define eps 1e-9
typedef long long LL;
struct node{
    int a,b;
}s[60];
bool cmp(node x,node y)
{
    return x.a*y.b > y.a*x.b;
}
void work()
{
    int n,v;
    RD2(n,v);
    for(int i = 0;i < n;++i){
        scanf("%d",&s[i].a);
        //cout<<s[i].a;
    }
    for(int i = 0;i < n;++i){
        scanf("%d",&s[i].b);
    }
    for(int i = 0;i < n;++i)
        if(s[i].b&&v<=s[i].a){
            puts("-1");
            return;
        }
    sort(s,s+n,cmp);
    double ans = 0.0;
    for(int i = 0;i < n;++i){
        if(s[i].b==0)
            continue;
        ans += (ans*(double)s[i].a + (double)s[i].b)/((double)v - (double)s[i].a);
    }
    printf("%.0lf
",ans);
}
int main() {
    int nn;
    RD(nn);
    while(nn--){
        work();
    }
    return 0;
}


原文地址:https://www.cnblogs.com/zibaohun/p/4046824.html