wyh's items

Senior wyh now has n items in his hand, and tells you the weight and value of these n items, and then let you select k from them, and ask you what is the maximum unit value of all possible options (unit value Is the ratio of the total value of the selected k items to the total weight)

Input description:
Enter an integer T in the first line (1<=T<=10) and
then there are T groups of test data. For each group of test data, enter two numbers n and k in the first line (1<=k<= n<=100000)
There are next n rows, each of which contains two a and b, representing the weight and value of this item

Output description:
For each set of test data, output the corresponding answer, with two decimal places for the result.
Example 1
Input
1
3 2
2 2
5 3
2 1
Output
0.75
.
For the sample, we choose the first item and the third item , To achieve the best goal

//238ms
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const double eps = 1e-6;
struct node
{
    
    
    double w, v, f;
    bool operator < (const node &tmp) const
	{
    
    
        return f < tmp.f;
    }
}use[maxn];
int main()
{
    
    
    int t;
	cin>>t;
    while(t--)
	{
    
    
        int n, k;
		cin >> n >> k;
        for(int i = 0;i < n; ++i) cin >> use[i].w >> use[i].v;
        double res = rand(), res_cal = res + 2;
        while(fabs(res_cal - res) > eps)
		{
    
    
            res = res_cal;
            for(int i = 0; i < n; ++i)
                use[i].f = res * use[i].w - use[i].v;
            sort(use, use + n);
            double w_use = 0, v_use = 0;
            for(int i = 0; i < k; ++i)
                w_use += use[i].w, v_use += use[i].v;
            res_cal = 1.0 * v_use / w_use;
        }
        cout << fixed << setprecision(2) << res << endl;
    }
    return 0;
}
//387ms
#include<bits/stdc++.h>
using namespace std;
int c[100005], v[100005], n, k;
double p[100005];
bool check(double x)
{
    
    
	for(int i = 0; i < n; i++)
		p[i] = v[i] - x * c[i];
	sort(p, p + n);
	double sum = 0;
	for(int i = n - 1; i >= n - k; i--)
		sum += p[i];
	return sum >= -0.00001;
}
int main()
{
    
    
	int T;
	scanf("%d", &T);
	while(T--)
	{
    
    
		scanf("%d%d", &n, &k);
		for(int i = 0; i < n; i++)
			scanf("%d%d", &c[i], &v[i]);
		double l = 0, r = 200, mid;
		while(l <= r)
		{
    
    
			mid = (l + r) / 2;
			if(fabs(r - l) < 0.001) break;
			if(check(mid)) l = mid;
			else r = mid;
		}
		printf("%.2f\n", mid);
	}
	return 0;
}
//173ms
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const double eps = 1e-6;
struct node
{
    
    
    double w, v, f;
    bool operator < (const node &tmp) const
	{
    
    
        return f < tmp.f;
    }
}use[maxn];
int main()
{
    
    
    int t, n, k;
    scanf("%d", &t);
    while(t--)
	{
    
    
        scanf("%d%d", &n, &k);
        for(int i = 0;i < n; ++i) scanf("%lf%lf", &use[i].w, &use[i].v);
        double res = rand(), res_cal = res + 2;
        while(fabs(res_cal - res) > eps)
		{
    
    
            res = res_cal;
            for(int i = 0; i < n; ++i)
                use[i].f = res * use[i].w - use[i].v;
            sort(use, use + n);
            double w_use = 0, v_use = 0;
            for(int i = 0; i < k; ++i)
                w_use += use[i].w, v_use += use[i].v;
            res_cal = 1.0 * v_use / w_use;
        }
        cout << fixed << setprecision(2) << res << "\n";
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/114005242