01 scores planning --POJ3111

Topic Link

Title meaning

Find k diamond, such sigmaVi / sigmaWi maximum, and then outputs the number of these diamonds

Topic analysis

Ruoshi sigmaVi / sigmaWi obtain the maximum value, so take any k diamond answer ans

ans <= sigmaVi / sigmaWi

What is the conversion sigmaVi-sigmaWi * ans> = 0

Then convert what is the sigma (Vi-Wi * ans)> = 0

ans come with diethyl points, if they meet the above formula low = mid

Otherwise, high = mid

As the output number, structure I and the vector store each binary number

Topic Code

Note that the upper bound of half open bigger

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e5+7;
const double eps=1e-7;
struct Node{
    int id;
    double v,w,val;
}node[maxn];
bool cmp(Node a,Node b){
    return a.val>b.val;
}
int n,k;
double r,l,mid,sum;
vector<int>v;
int main(){
    scanf("%d%d",&n,&k);
    l=0.0;r=3000;
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&node[i].v,&node[i].w);
        node[i].id=i;
        r=max(r,node[i].v/node[i].w);
    }
    while(r-l>eps){
        mid=(l+r)/2;
        for(int i=1;i<=n;i++)
            node[i].val=node[i].v-node[i].w*mid;
        sum=0;
        v.clear();
        sort(node+1,node+1+n,cmp);
        for(int i=1;i<=k;i++){
            sum+=node[i].val;
            v.push_back(node[i].id);
        }
        if(sum>=0)l=mid;
        else r=mid;
    }
//    sort(v.begin(),v.end());
    for(int i=0;i<v.size();i++){
        if(!i)printf("%d",v[i]);
        else printf(" %d",v[i]);
    }
    printf("\n");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/helman/p/11332076.html