POJ3111 K Best —— 01分数规划 二分法
Posted h_z_cong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3111 K Best —— 01分数规划 二分法相关的知识,希望对你有一定的参考价值。
题目链接:http://poj.org/problem?id=3111
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 = {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
Source
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 29 int n, k; 30 31 bool test(double L) 32 { 33 for(int i = 1; i<=n; i++) 34 q[i].d = 1.0*q[i].a - L*q[i].b; 35 36 sort(q+1, q+1+n); 37 double sum = 0; 38 for(int i = 1; i<=k; i++) 39 sum += q[i].d; 40 return sum>0; 41 } 42 43 int main() 44 { 45 while(scanf("%d%d", &n, &k)!=EOF) 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 }
以上是关于POJ3111 K Best —— 01分数规划 二分法的主要内容,如果未能解决你的问题,请参考以下文章