K Best [二分]

来源:互联网 发布:zookeeper mysql 实战 编辑:程序博客网 时间:2024/06/05 04:30

这题与http://blog.csdn.net/qer_computerscience/article/details/77601173
一模一样。

#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<bitset>#include<algorithm>#include<map>#include<set>#include<queue>#include<vector>#include<cstdlib>#include<list>#include<stack>#include<cmath>#include<iomanip>using namespace std;//#pragma comment(linker, "/STACK:1024000000,1024000000")typedef long long LL;void debug() {cout << "ok running!" << endl;}int n, k;struct node{    int v, w, id;    double x;    bool operator < (node& b)    {        return x > b.x;    }} a[100005];bool check(double mid){    for(int i = 0; i < n; ++i)        a[i].x = a[i].v - mid*a[i].w;    sort(a, a+n);    double sum = 0;    for(int i = 0; i < k; ++i)        sum += a[i].x;    if(sum >= 0)        return 1;    else return 0;}int main(){    ios::sync_with_stdio(false);    #ifndef ONLINE_JUDGE    freopen("input.txt", "r", stdin);    #endif // ONLINE_JUDGE    while(cin >> n >> k)    {        for(int i = 0; i < n; ++i)        {            int x, y;            cin >> x >> y;            a[i].id = i+1;            a[i].v = x, a[i].w = y;        }        double l = 0.0, r = 999999999.0;        while(r - l > 1e-4)        {            double mid = (l+r) / 2;            //cout << mid << endl;            if(check(mid))                l = mid;            else r = mid;        }        for(int i = 0; i < k-1; ++i)            cout << a[i].id << " ";        cout << a[k-1].id << endl;    }    return 0;}
原创粉丝点击