Problem Description
Kanade selected n courses in the university. The academic credit of the i-th course is s[i] and the
score of the i-th course is c[i].
At the university where she attended, the final score of her is
∑s[i]c[i]∑s[i]
∑
s
[
i
]
c
[
i
]
∑
s
[
i
]
Now she can delete at most k courses and she want to know what the highest final score that can
get.
Input
The first line has two positive integers n,k
The second line has n positive integers s[i]
The third line has n positive integers c[i]
Output
Output the highest final score, your answer is correct if and only if the absolute error with the
standard answer is no more than
10−5
10
−
5
.
Note
1≤ n≤ 105
0≤ k < n
1≤ s[i],c[i] ≤ 103
Sample Input
3 1
1 2 3
3 2 1
Sample Output
2.33333333333
Hint
Delete the third course and the final score is
2∗2+3∗12+1=73
2
∗
2
+
3
∗
1
2
+
1
=
7
3
.
题目大意:有n个学生,给出每个学生的学分和绩点,求从中去掉k个学生后剩下学生的“总绩点”的最大值。
分析: 01分数规划裸题。设“总绩点”最大值为x, ∑s[i]∗c[i]∑s[i]=x ∑ s [ i ] ∗ c [ i ] ∑ s [ i ] = x → ∑(s[i]∗c[i]−x∗c[i])=0 ∑ ( s [ i ] ∗ c [ i ] − x ∗ c [ i ] ) = 0 。因为[0-x)间一定有 ∑(s[i]∗c[i]−x∗c[i])>0 ∑ ( s [ i ] ∗ c [ i ] − x ∗ c [ i ] ) > 0 ,(x, max_right]间一定有 ∑(s[i]∗c[i]−x∗c[i])<0 ∑ ( s [ i ] ∗ c [ i ] − x ∗ c [ i ] ) < 0 (具体证明暂时不会)。故可二分x得到答案。复杂度为O( nlog2n∗log2(1000∗1e6) n l o g 2 n ∗ l o g 2 ( 1000 ∗ 1 e 6 ) )。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5;
int n, k;
struct fct{int n, d;} s[N];
double val;
bool cmp(fct a, fct b)
{
return a.n - a.d * val > b.n - b.d * val;
}
int main()
{
cin >> n >> k;
k = n - k;
for (int i = 0; i < n; i++)
cin >> s[i].d;
for (int i = 0; i < n; i++)
{
cin >> s[i].n;
s[i].n *= s[i].d;
}
double l = 0, r = 1000;
while (r - l > 1e-6)
{
val = (l + r) / 2;
sort(s, s + n, cmp);
double ans = 0;
for (int i = 0; i < k; i++)
ans += s[i].n - val * s[i].d;
if (ans >= 0)
l = val;
else
r = val;
}
cout << setprecision(5) << fixed << val << endl;
return 0;
}