POJ_3111_K_Best_(二分,最大化平均值)

描述


 

http://poj.org/problem?id=3111

n个珠宝,第i个有价值v[i]和重量w[i],要从总选k个,使得这k个的(价值之和)/(重量之和)即平均价值最大,输出选中的珠宝编号.

 

K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 8189   Accepted: 2087
Case 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 = {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 ≤ kn ≤ 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

Source

Northeastern Europe 2005, Northern Subregion

分析


二分.

最大化平均值(同POJ 2976 Dropping Tests).

不等式变形做就可以了.

注意:

1.算c的值的时候不用担心算爆,int会先被提升为double再做运算,所以r取INF也没关系,不会爆(最多是INF*INF).

2.但是r取INF精度就不够了,所以还是乖乖取max(a[i]/b[i])把...

3.排序的时候直接排成从大到小的,方便.

4.这题二分次数少了进度不够,次数多了超时. 

 1 #include<cstdio>
 2 #include<algorithm>
 3 using std :: sort;
 4 using std :: max;
 5 
 6 const int maxn=100005,INF=0x7fffffff;
 7 int n,k;
 8 int ans[maxn];
 9 struct point
10 {
11     int a,b,num;
12     double c;
13 }j[maxn];
14 double x,y;
15 
16 bool comp(point x,point y) { return x.c>y.c; }
17 
18 bool C(double x)
19 {
20     for(int i=1;i<=n;i++) j[i].c=j[i].a-j[i].b*x;
21     sort(j+1,j+n+1,comp);
22     double sum=0;
23     for(int i=1;i<=k;i++) sum+=j[i].c;
24     return sum>=0;
25 }
26 
27 void solve()
28 {
29     for(int i=0;i<25;i++)
30     {
31         double m=x+(y-x)/2;
32         if(C(m))
33         {
34             x=m;
35             for(int i=1;i<=k;i++) ans[i]=j[i].num;
36         }
37         else y=m;
38     }
39     for(int i=1;i<k;i++) printf("%d ",ans[i]);
40     printf("%d",ans[k]);
41 }
42 
43 void init()
44 {
45     scanf("%d%d",&n,&k);
46     for(int i=1;i<=n;i++) 
47     {
48         scanf("%d%d",&j[i].a,&j[i].b);
49         j[i].num=i;
50         y=max(y,(double)j[i].a/(double)j[i].b);
51     }
52 }
53 
54 int main()
55 {
56     freopen("k.in","r",stdin);
57     freopen("k.out","w",stdout);
58     init();
59     solve();
60     fclose(stdin);
61     fclose(stdout);
62     return 0;
63 }
View Code

 

posted @ 2016-04-23 08:49  晴歌。  阅读(149)  评论(0编辑  收藏  举报