题意:
有n门科目。第i门科目的学分为Si,分数为Ci。根据学校的规定,最终的得分为 。求删掉k门科目后的最大得分。
题解:
二分答案,假设当前二分的答案为P,排序求出Si*(Ci-P)值前n-k大的科目判断可行性。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+10; double eps = 1e-8; int n, k; double p; double l, r; struct node { double s, c; bool operator < (const node &x) { return s*(c-p) > x.s*(x.c-p); } }a[N]; bool check(double x) { p = x; sort(a+1, a+n+1); double res = 0; for(int i = 1; i <= n-k; i++) res += a[i].s*(a[i].c-x); if(res >= eps) return 1; return 0; } int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) scanf("%lf", &a[i].s); for(int i = 1; i <= n; i++) { scanf("%lf", &a[i].c); r = max(r, a[i].c); } for(int i = 1; i <= 100; i++) { double mid = (l+r)/2.0; if(check(mid)) l = mid+eps; else r = mid-eps; } printf("%.11lf", l); }