AcWing 234. abandon the test (01 points plan) punch

Topic: https://www.acwing.com/problem/content/236/

Meaning of the questions: to give you an equation, there are k can not vote, requires optimal

Ideas: I looked at this equation that this is the template title 01 scores planning, it may choose the k do not, in fact, is the sort of time you can choose the best of nk

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <limits>
#include <set>
#include <map>
using namespace std;
int n,k,a[2333],b[2333];
double ps[2333];
bool ok(double x)
{
    for(int i=1;i<=n;i++) ps[i]=a[i]-x*b[i];
    sort(ps+1,ps+1+n);
    double ans=0;
    for(int i=n;i>=k+1;i--) ans+=ps[i];
    return ans>=0;
}
void sol()
{
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    for(int i=1;i<=n;i++) scanf("%d",b+i);
    double l=0,r=1;
    while(r-l>1e-6)
    {
        double mid=(l+r)/2;
        if(ok(mid)) l=mid; else r=mid;
    }
    printf("%.0lf\n",l*100);
}
int main()
{
    while(scanf("%d%d",&n,&k),n|k) sol();
}

 

Guess you like

Origin www.cnblogs.com/Lis-/p/11299741.html