POJ 3111
来源:互联网 发布:有趣的书 知乎 编辑:程序博客网 时间:2024/06/07 00:51
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 21 11 21 3
Sample Output
1 2
Source
题意:
就是最大化平均值。
POINT:
二分答案。
二分搜索法模型: 条件C(x):可以挑选使得单位重量的物品价值不小于x->求满足条件的最大x->如何判断C(x) 价值和/重量和>=x 价值和-重量和*x>=0 和(价值-重量*x)>=0 可以对(价值-重量*x)的值进行贪心的选取,选取最大的k个 和>=0
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;const int maxn = 100100;typedef pair<double, int> pr;int n,k;int v[maxn];int w[maxn];int check(double x){ pr y[maxn]; for(int i=1;i<=n;i++){ y[i].first=v[i]-x*w[i]; y[i].second=i; } sort(y+1,y+1+n); double sum=0; for(int i=n;i>=n-k+1;i--){ sum+=y[i].first; } if(sum>0) return 1; return 0;}void haha(double x){ pr y[maxn]; for(int i=1;i<=n;i++){ y[i].first=v[i]-x*w[i]; y[i].second=i; } sort(y+1,y+1+n); for(int i=n;i>=n-k+1;i--){ if(i!=n) printf(" "); printf("%d",y[i].second); } printf("\n");}void solve(){ double l=0,r=100000; while(fabs(l-r)>1e-6){ double mid=(l+r)/2; if(check(mid)){ l=mid; }else{ r=mid; } } haha(l);}int main(){ while(~scanf("%d %d",&n,&k)){ for(int i=1;i<=n;i++){ scanf("%d %d",&v[i],&w[i]); } solve(); }}
- POJ 3111
- POJ-3111
- poj 3111
- POJ 3111
- poj 3111
- 【POJ】3111
- POJ 3111
- poj 3111 迭代法
- POJ 3111 K Best
- poj 3111 K Best
- POJ 3111(二分)
- poj 3111(二分)
- poj 3111 最大化平均值
- POJ-3111K Best
- poj 3111 K Best
- POJ 3111K Best
- poj 3111 best(二分)
- poj 3111 (最大化平均值)
- Java Jedis远程连接redis异常解决思路,异常描述:Could not get a resource from the pool && connect timed out
- XPath定位——基于Google Chrome和Firefox
- Navi.Soft31.WebMVC框架(含示例地址)
- python大作战之不可变序列(元组tuple)
- K
- POJ 3111
- LeetCode刷题(30)
- android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"相关问题
- 在ROS下,创建range_layer,加入超声波传感器。
- laravel 发送邮件
- 海思库解码CCITT标准g726算法编码G726音频数据
- Idea 快捷键修改
- [BFS] HDU 1241
- JAVA---基础面试题总结(1)