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

题目描述

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 

Now she can delete at most k courses and she want to know what the highest final score that can get.

输入描述:

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

输入

3 1
1 2 3
3 2 1

输出

2.33333333333

01分数规划模板题:https://blog.csdn.net/jaihk662/article/details/77505318

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef struct Res
{
	double a, b;
	double d;
	bool operator < (const Res &b) const
	{
		if(d>b.d)
			return 1;
		return 0;
	}
}Res;
Res s[100005];
int main(void)
{
	int i, n, k;
	double cnt, sa, sb;
	scanf("%d%d", &n, &k);
	k = n-k;
	for(i=1;i<=n;i++)
		scanf("%lf", &s[i].b);
	for(i=1;i<=n;i++)
	{
		scanf("%lf", &s[i].a);
		s[i].a *= s[i].b;
	}
	cnt = 0;
	while(1)
	{
		for(i=1;i<=n;i++)
			s[i].d = s[i].a-cnt*s[i].b;
		sort(s+1, s+n+1);
		sa = sb = 0;
		for(i=1;i<=k;i++)
		{
			sa += s[i].a;
			sb += s[i].b;
		}
		if(fabs(sa/sb-cnt)<0.00001)
			break;
		cnt = sa/sb;
	}
	printf("%.10f\n", cnt);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jaihk662/article/details/81365677