POJ 3111:K Best(思维+二分)
来源:互联网 发布:仙侠世界cos捏脸数据 编辑:程序博客网 时间:2024/05/19 20:59
Gourmet and Banquet
Problem 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 2
1 1
1 2
1 3
Sample Output
1 2
题意:
Demy有N件珠宝,价值和重量分别为 Vi 和 Wi,现在他想在其中选择K件,使得s(S)最大,问要选择那几件。
解题思路:
一开始以为是贪心,但事实上由
对s(S)进行二分,设s(S)为x,用
作为判断x可行的条件,若满足,继续找大的,否则找小的。
根据这个条件可以得到
因此,我们可以根据
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 K Best(二分)
- poj 3111 K Best 二分
- POJ 2976 Dropping tests、3111 K Best (二分搜索)
- POJ 3111 K Best 二分(最小化平均值)
- poj 3111 K Best(二分-最大化平均值)
- K Best POJ 3111 (二分,最大化平均値)
- POJ 3111 - K Best(二分搜索)
- POJ 3111 K Best <最大化平均,二分>
- POJ 3111 K Best 【二分:最大化平均值】
- POJ 3111 K Best 二分 最大化平均值
- K Best poj 3111 (01分数规划---二分搜索)
- poj 3111 K Best ,二分,牛顿迭代
- POJ 3111 K Best (01分数规划+二分)
- POJ 3111 K Best(二分搜索,最大化平均值)
- POJ - 3111 K Best (二分 + 01分数规划)
- poj3111- POJ - 3111 K Best (二分 + 01分数规划)
- poj 3111 K Best 二分搜索 最大化平均值
- Activity退出程序时候,怎样清空Activity
- HDU3240 Counting Binary Trees (卡特兰数,拓欧求逆元)
- PAT 1066图像过滤
- parseXML 与 parseJson
- JAVA-EE过滤器Filter的简单使用
- POJ 3111:K Best(思维+二分)
- BZOJ 3261 浅谈可持久化TRIE树最大连续异或和
- 【NYOJ
- C++设计模式:策略模式
- css盒子模型
- GIF文件格式详解
- Android获得json和XML
- 算法第四版 练习 1.3.34
- AI将带我们去何方?(上-前言篇)