K Best [二分]

这题与http://blog.youkuaiyun.com/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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值