POJ3111 K Best —— 01分数规划 二分法

题目链接:http://poj.org/problem?id=3111

 

K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 11380   Accepted: 2935
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 = {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 2
1 1
1 2
1 3

Sample Output

1 2

Source

Northeastern Europe 2005, Northern Subregion
 
 
 
 
题解:
 
 
代码一:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 1e5+10;
19 
20 struct node
21 {
22     double d;
23     int a, b, id;
24     bool operator<(const node x)const{ 
25         return d>x.d;
26     }
27 }q[MAXN];
28 int n, k;
29 
30 bool test(double L)
31 {
32     for(int i = 1; i<=n; i++)
33         q[i].d = 1.0*q[i].a - L*q[i].b;
34 
35     sort(q+1, q+1+n);
36     double sum = 0;
37     for(int i = 1; i<=k; i++)   //取前k大的数
38         sum += q[i].d;
39     return sum>=0;
40 }
41 
42 int main()
43 {
44     while(scanf("%d%d", &n, &k)!=EOF)
45     {
46          //一次性把所有信息都录入结构体中,当排序时,即使打乱了顺序,仍然还记得初始下标。
47         for(int i = 1; i<=n; i++)  
48         {
49             scanf("%d%d", &q[i].a, &q[i].b);
50             q[i].id = i;    
51         }
52 
53         double l = 0, r = 1e7;
54         while(l+EPS<=r)
55         {
56             double mid = (l+r)/2;
57             if(test(mid))
58                 l = mid + EPS;
59             else
60                 r = mid - EPS;
61         }
62 
63         for(int i = 1; i<=k; i++)
64             printf("%d ", q[i].id);
65         printf("\n");
66     }
67 }
View Code
 
代码二:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 1e5+10;
19 
20 struct node
21 {
22     double d;
23     int id;
24     bool operator<(const node x)const{
25         return d>x.d;
26     }
27 }q[MAXN];
28 
29 int n, k;
30 int a[MAXN], b[MAXN];
31 
32 bool test(double L)
33 {
34     for(int i = 1; i<=n; i++)   //每一次q[i]都重新更新,与a[i],b[i]独立开来
35     {
36         q[i].id = i;
37         q[i].d = 1.0*a[i] - L*b[i];
38     }
39     sort(q+1, q+1+n);
40     double sum = 0;
41     for(int i = 1; i<=k; i++)
42         sum += q[i].d;
43     return sum>=0;
44 }
45 
46 int main()
47 {
48     while(scanf("%d%d", &n, &k)!=EOF)
49     {
50         for(int i = 1; i<=n; i++)
51             scanf("%d%d", &a[i], &b[i]);
52 
53         double l = 0, r = 1e7;
54         while(l+EPS<=r)
55         {
56             double mid = (l+r)/2;
57             if(test(mid))
58                 l = mid + EPS;
59             else
60                 r = mid - EPS;
61         }
62 
63         for(int i = 1; i<=k; i++)
64             printf("%d ", q[i].id);
65         printf("\n");
66     }
67 }
View Code

 

错误代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 1e5+10;
19 
20 struct node
21 {
22     double d;
23     int id;
24     bool operator<(const node x)const{
25         return d>x.d;
26     }
27 }q[MAXN];
28 
29 int n, k;
30 int a[MAXN], b[MAXN], ans[MAXN];
31 
32 bool test(double L)
33 {
34     //经过一次排序后,q[i].id不再等于i,所以出错。应该同时更新q[i].id
35     for(int i = 1; i<=n; i++)
36         q[i].d = 1.0*a[i] - L*b[i];
37 
38     sort(q+1, q+1+n);
39     double sum = 0;
40     for(int i = 1; i<=k; i++)
41         sum += q[i].d;
42     return sum>0;
43 }
44 
45 int main()
46 {
47     while(scanf("%d%d", &n, &k)!=EOF)
48     {
49         for(int i = 1; i<=n; i++)
50         {
51             scanf("%d%d", &a[i], &b[i]);
52             q[i].id = i;       
53         }
54 
55         double l = 0, r = 1e7;
56         while(l+EPS<=r)
57         {
58             double mid = (l+r)/2;
59             if(test(mid))
60                 l = mid + EPS;
61             else
62                 r = mid - EPS;
63         }
64 
65         for(int i = 1; i<=k; i++)
66             printf("%d ", q[i].id);
67         printf("\n");
68     }
69 }
View Code

 

 
 

posted on 2017-09-21 21:33  h_z_cong  阅读(319)  评论(0编辑  收藏  举报

导航