poj3111 K Best【最大化平均值】

来源:互联网 发布:中韩贸易逆差数据 编辑:程序博客网 时间:2024/05/16 06:18

K Best
Time Limit: 8000MS Memory Limit: 65536KTotal Submissions: 10485 Accepted: 2698Case Time Limit: 2000MS Special Judge

Description

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 = {i1i2, …, 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 21 11 21 3

Sample Output

1 2
思路:二分解决最大化平均值,玄学的是C++超时,G++过了;

二分的是最后的平均值,将题目中式子变化一下就是 s[i].d = s[i].a - mid * s[i].b ;用out数组记录 i 就可以了;

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define max_n 100010using namespace std;typedef long long LL;double in[max_n];int out[max_n];int n,k;struct node{double a;double b;int c;double d;}s[max_n];bool cmp(node x,node y){return x.d>y.d;}bool judge(double mid){for(int i=0;i<n;i++){s[i].d=s[i].a-mid*s[i].b;}sort(s,s+n,cmp);double sum=0.0;for(int i=0;i<k;i++){sum+=s[i].d;out[i]=s[i].c;}return sum>=0.0;}int main(){while(~scanf("%d %d",&n,&k)){memset(s,0,sizeof(s));memset(out,0,sizeof(out));for(int i=0;i<n;i++){scanf("%lf %lf",&s[i].a,&s[i].b);s[i].c=i+1;}double l=0.0,r=10000000.0,mid;while(r-l>1e-6){mid=(l+r)/2.0;if(judge(mid)) l=mid;else r=mid;}//printf("%lf\n",l);for(int i=0;i<k;i++){if(i>0) printf(" ");printf("%d",out[i]); }  printf("\n");}return 0;}


原创粉丝点击