牛客网暑期ACM多校训练营(第五场)A gpa【分数规划】

版权声明:学习,哪有那么多条条框框~ https://blog.csdn.net/Irish_Moonshine/article/details/81592975

https://www.nowcoder.com/acm/contest/143/A

典型的分数规划问题,而二分排序双lg解决。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
const int maxn = 1e5 + 12;
const double eps = 1e-8;
int n, k;
struct node {
    int s, c; double d;
}a[maxn];
double del[maxn];
int cmp(node a, node b) {
    return a.d < b.d;
}
int cal(double x) {
    for (int i = 1; i <= n; i++) {
        a[i].d = 1.0*(1.0*a[i].c - x)*a[i].s;
        //cout << a[i].d << endl;
    }
    //cout << endl;
    sort(a + 1, a + 1 + n, cmp);
    double tmp = 0.0;
    for (int i = k + 1; i <= n; i++) {
        tmp += a[i].d;
    }
    if (tmp > eps) return 1;
    else return 0;
}
int main()
{
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i].s);

    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i].c);
    }
    double L = 0.0, R = 10000.0; double ans = 0;
    //cout << cal(2.7) << endl;
    while (R - L > eps) {
        double M = (L + R) / 2;
        if (cal(M)) ans = M, L = M;
        else R = M;
    }
    printf("%.10f\n", ans);
    return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页