K Best 【01规划入门】(附知识点链接)

不知道什么是01规划? 点我

题目连接
Demy has n jewels. Each of her jewels has some value vi and weight wi.

Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewels S = {i1, i2, …, ik} as

Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so.

Input
The first line of the input file contains n — the number of jewels Demy got, and k — the number of jewels she would like to keep (1 ≤ k ≤ n ≤ 100 000).

The following n lines contain two integer numbers each — vi and wi (0 ≤ vi ≤ 106, 1 ≤ wi ≤ 106, both the sum of all vi and the sum of all wi do not exceed 107).

Output
Output k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.

Sample Input
3 2
1 1
1 2
1 3
Sample Output
1 2
.
入门题目,模版
代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define LL long long
using namespace std;
const int MAXN = 1e6;
const int MAXM = 3000000;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8 ;
/*------------------------------*/
struct node{
    int id;
    int a,b;
    double val;
}arr[MAXN];
int out[MAXN]; // 保存输出的顺序
int n,k;
bool cmp(node a,node b){return a.val>b.val;}

bool f(double x){
    for(int i=0;i<n;i++)
        arr[i].val=arr[i].a-x*arr[i].b;
    sort(arr,arr+n,cmp);
    double ans=0;
    for(int i=0;i<k;i++){
        ans+=arr[i].val;
        out[i]=arr[i].id;
    }
    return ans>=0.0;
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d%d",&arr[i].a,&arr[i].b);
        arr[i].id=i+1;
    }

    double le=0.0,ri=inf*1.0;
    while(ri-le>eps){   // 单调递减
        double mid=(le+ri)/2;
        if(f(mid)) le=mid;
        else ri=mid;
    }
    int have=0;// 控制空格的
    for(int i=0;i<k;i++){
        if(have++) putchar(' ');
        printf("%d",out[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值