- 题目描述: Kanade selected n courses in the university. The academic credit of the i-th course is s[i] and the score of
Now she can delete at most k courses and she want to know what the highest final score that can get.
the i-th course is c[i].At the university where she attended, the final score of her is
- 输入描述:
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 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
- 备注
1≤ n≤ 10 5
0≤ k < n
1≤ s[i],c[i] ≤ 10 3
- 示例1:
输入
3 1
1 2 3
3 2 1
输出
2.33333333333
说明
Delete the third course and the final score is
题意:给定一个n和k,然后输入s[i]和c[i],求在n组数据中
的最大值,可以删除最多k组数。
解析:主要使用到了01分数规划的相关算法,具体请参考另一位博主的博客01分数规划的问题模型1
由题意可得出函数 f(r)=∑s[i]*c[i]-r*∑s[i];要使f(r)达到最大,可以采用二分法的模板。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define eps 1e-11//精确度
using namespace std;
typedef struct node
{
int a,b;
double r;
} node;
bool op(node a,node b)
{
return a.r>b.r;
}
node p[100010];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++) scanf("%d",&p[i].a);
for (int i=1;i<=n;i++) scanf("%d",&p[i].b);
int m=n-k;
double left=0,right=10000;
while (left+eps<right)
{
double mid=(left+right)/2;
for (int i=1;i<=n;i++) p[i].r=p[i].a*p[i].b-mid*p[i].a;
//根据不同的公式改写此处为(r=a[i]*b[i]/a[i]);
//即f(r)=∑ai*bi-r*∑bi,求出各个fr
//mid用来测试最大的横截距
sort(&p[1],&p[n+1],op);//按照从大到小的顺序排了之后累加
double temp=0;
for (int i=1;i<=m;i++) temp+=p[i].r;//删除k个之后的最大值
for(int i=m+1;i<=n;i++)//题目没要求一定删满k个值可以再加上其他值
if(p[i].r>0)
temp+=p[i].r;
if (temp>0) left=mid;
else right=mid;
}
printf("%.11f\n",left);
return 0;
}