POJ 3111:K Best(思维+二分)
2017-07-26 20:34
288 查看
Gourmet and Banquet
Time limit8000 ms Case time limit 2000 ms Memory limit65536 kBProblem 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.The gourmet can instantly switch between the dishes but only at integer moments in time. It is allowed to return to a dish after eating any other dishes. Also in every moment in time he can eat no more than one dish.
Sample Input
3 21 1
1 2
1 3
Sample Output
1 2题意:
Demy有N件珠宝,价值和重量分别为 Vi 和 Wi,现在他想在其中选择K件,使得s(S)最大,问要选择那几件。解题思路:
一开始以为是贪心,但事实上由ab>cd>ef 并不能得到 a+cb+d>c+ed+f ,所以这种方法是错误的。对s(S)进行二分,设s(S)为x,用
∑vi∑wi≥x
作为判断x可行的条件,若满足,继续找大的,否则找小的。
根据这个条件可以得到
∑(vi−wi∗x)≥0
因此,我们可以根据 yi=∑(vi−wi∗x) 的值对珠宝排序,取前K个的和,若不小于0,则条件成立。
Code:
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; const int maxn=100000+5; const double EPS=1e-8; int w[maxn]; int v[maxn]; int n,k; struct Node { int no; double y; }a[maxn]; bool cmp(Node a,Node b) { return a.y>b.y; } bool f(double x) { for(int i=0;i<n;i++) { a[i].no=i+1; a[i].y=v[i]-x*w[i]; } sort(a,a+n,cmp); double sum=0; for(int i=0;i<k;i++) sum+=a[i].y; if(sum>=0) return true; return false; } int bs() { double lo=0,hi=1e7,ans=0; while(hi-lo>EPS)//注意精度 { double mid=(hi+lo)/2.0; if(f(mid)) { ans=mid; lo=mid; } else hi=mid; } return ans; } int main() { while(scanf("%d%d",&n,&k)!=EOF) { for(int i=0;i<n;i++) { a[i].no=i+1; scanf("%d%d",&v[i],&w[i]); } bs(); for(int i=0;i<k;i++) { printf("%d",a[i].no); if(i==k-1) printf("\n"); else printf(" "); } } return 0; }
相关文章推荐
- POJ 3111 K Best(二分——最大化平均值)
- poj 3111 best(二分)
- POJ - 3111 K Best(二分)
- POJ 3111 K Best(二分答案)
- POJ - 3111 K Best (二分 + 01分数规划)
- poj 3111 K Best 二分
- poj 3111 K Best(二分)
- poj 3111 K Best(二分-最大化平均值)
- poj 3111 K Best 【0-1分数规划】 【二分 or Dinkelbach迭代】
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- POJ_3111_K_Best_(二分,最大化平均值)
- poj 3111 K Best (二分搜索之最大化平均值之01分数规划)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- POJ 3111 K Best <最大化平均,二分>
- POJ 3111 K Best 二分(最小化平均值)
- POJ 3111 K Best (01分数规划+二分)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- poj3111- POJ - 3111 K Best (二分 + 01分数规划)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)